Skip to content

Commit 43dd0e2

Browse files
authored
Rollup merge of #97780 - compiler-errors:field-wfcheck-before-sized, r=jackh726
Check ADT field is well-formed before checking it is sized Fixes #96810. There is one diagnostics regression, in [`src/test/ui/generic-associated-types/bugs/issue-80626.stderr`](https://github.com/rust-lang/rust/pull/97780/files#diff-53795946378e78a0af23a10277c628ff79091c18090fdc385801ee70c1ba6963). I am not super concerned about it, since it's GAT related. We _could_ fix it, possibly by using the `FieldSized` obligation cause code instead of `BuiltinDerivedObligation`. But that would require changing `Sized` trait confirmation and the `adt_sized_constraint` query.
2 parents 3694e40 + c1f4f98 commit 43dd0e2

File tree

6 files changed

+51
-34
lines changed

6 files changed

+51
-34
lines changed

compiler/rustc_middle/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ pub enum ObligationCauseCode<'tcx> {
406406
QuestionMark,
407407

408408
/// Well-formed checking. If a `WellFormedLoc` is provided,
409-
/// then it will be used to eprform HIR-based wf checking
409+
/// then it will be used to perform HIR-based wf checking
410410
/// after an error occurs, in order to generate a more precise error span.
411411
/// This is purely for diagnostic purposes - it is always
412412
/// correct to use `MiscObligation` instead, or to specify

compiler/rustc_typeck/src/check/wfcheck.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,15 @@ fn check_type_defn<'tcx, F>(
990990
let packed = tcx.adt_def(item.def_id).repr().packed();
991991

992992
for variant in &variants {
993+
// All field types must be well-formed.
994+
for field in &variant.fields {
995+
fcx.register_wf_obligation(
996+
field.ty.into(),
997+
field.span,
998+
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(field.def_id))),
999+
)
1000+
}
1001+
9931002
// For DST, or when drop needs to copy things around, all
9941003
// intermediate types must be sized.
9951004
let needs_drop_copy = || {
@@ -1006,6 +1015,7 @@ fn check_type_defn<'tcx, F>(
10061015
}
10071016
}
10081017
};
1018+
// All fields (except for possibly the last) should be sized.
10091019
let all_sized = all_sized || variant.fields.is_empty() || needs_drop_copy();
10101020
let unsized_len = if all_sized { 0 } else { 1 };
10111021
for (idx, field) in
@@ -1030,15 +1040,6 @@ fn check_type_defn<'tcx, F>(
10301040
);
10311041
}
10321042

1033-
// All field types must be well-formed.
1034-
for field in &variant.fields {
1035-
fcx.register_wf_obligation(
1036-
field.ty.into(),
1037-
field.span,
1038-
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(field.def_id))),
1039-
)
1040-
}
1041-
10421043
// Explicit `enum` discriminant values must const-evaluate successfully.
10431044
if let Some(discr_def_id) = variant.explicit_discr {
10441045
let discr_substs = InternalSubsts::identity_for_item(tcx, discr_def_id.to_def_id());

src/test/ui/generic-associated-types/bugs/issue-80626.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ error[E0275]: overflow evaluating the requirement `LinkedList<A>: Sized`
44
LL | Next(A::Allocated<Self>)
55
| ^^^^^^^^^^^^^^^^^^
66
|
7-
= note: no field of an enum variant may have a dynamically sized type
8-
= help: change the field's type to have a statically known size
9-
help: borrowed types always have a statically known size
7+
note: required by a bound in `Allocator::Allocated`
8+
--> $DIR/issue-80626.rs:9:20
109
|
11-
LL | Next(&A::Allocated<Self>)
12-
| +
13-
help: the `Box` type always has a statically known size and allocates its contents in the heap
14-
|
15-
LL | Next(Box<A::Allocated<Self>>)
16-
| ++++ +
10+
LL | type Allocated<T>;
11+
| ^ required by this bound in `Allocator::Allocated`
1712

1813
error: aborting due to previous error
1914

src/test/ui/union/issue-81199.stderr

+5-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
1-
error[E0277]: the trait bound `T: Pointee` is not satisfied in `PtrComponents<T>`
1+
error[E0277]: the trait bound `T: Pointee` is not satisfied
22
--> $DIR/issue-81199.rs:5:17
33
|
44
LL | components: PtrComponents<T>,
5-
| ^^^^^^^^^^^^^^^^ within `PtrComponents<T>`, the trait `Pointee` is not implemented for `T`
5+
| ^^^^^^^^^^^^^^^^ the trait `Pointee` is not implemented for `T`
66
|
7-
note: required because it appears within the type `PtrComponents<T>`
8-
--> $DIR/issue-81199.rs:10:8
7+
note: required by a bound in `PtrComponents`
8+
--> $DIR/issue-81199.rs:10:25
99
|
1010
LL | struct PtrComponents<T: Pointee + ?Sized> {
11-
| ^^^^^^^^^^^^^
12-
= note: no field of a union may have a dynamically sized type
13-
= help: change the field's type to have a statically known size
11+
| ^^^^^^^ required by this bound in `PtrComponents`
1412
help: consider further restricting this bound
1513
|
1614
LL | union PtrRepr<T: ?Sized + Pointee> {
1715
| +++++++++
18-
help: borrowed types always have a statically known size
19-
|
20-
LL | components: &PtrComponents<T>,
21-
| +
22-
help: the `Box` type always has a statically known size and allocates its contents in the heap
23-
|
24-
LL | components: Box<PtrComponents<T>>,
25-
| ++++ +
2616

2717
error: aborting due to previous error
2818

src/test/ui/wf/issue-96810.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
struct S<T: Tr>(T::Assoc);
2+
3+
trait Tr {
4+
type Assoc;
5+
}
6+
7+
struct Hoge<K> {
8+
s: S<K>, //~ ERROR the trait bound `K: Tr` is not satisfied
9+
a: u32,
10+
}
11+
12+
fn main() {}

src/test/ui/wf/issue-96810.stderr

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0277]: the trait bound `K: Tr` is not satisfied
2+
--> $DIR/issue-96810.rs:8:8
3+
|
4+
LL | s: S<K>,
5+
| ^^^^ the trait `Tr` is not implemented for `K`
6+
|
7+
note: required by a bound in `S`
8+
--> $DIR/issue-96810.rs:1:13
9+
|
10+
LL | struct S<T: Tr>(T::Assoc);
11+
| ^^ required by this bound in `S`
12+
help: consider restricting type parameter `K`
13+
|
14+
LL | struct Hoge<K: Tr> {
15+
| ++++
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)