Skip to content

Commit c1f4f98

Browse files
Do wfcheck on ADT field before Sized check
1 parent 760237f commit c1f4f98

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
@@ -403,7 +403,7 @@ pub enum ObligationCauseCode<'tcx> {
403403
QuestionMark,
404404

405405
/// Well-formed checking. If a `WellFormedLoc` is provided,
406-
/// then it will be used to eprform HIR-based wf checking
406+
/// then it will be used to perform HIR-based wf checking
407407
/// after an error occurs, in order to generate a more precise error span.
408408
/// This is purely for diagnostic purposes - it is always
409409
/// 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
@@ -1008,6 +1008,15 @@ fn check_type_defn<'tcx, F>(
10081008
let packed = tcx.adt_def(item.def_id).repr().packed();
10091009

10101010
for variant in &variants {
1011+
// All field types must be well-formed.
1012+
for field in &variant.fields {
1013+
fcx.register_wf_obligation(
1014+
field.ty.into(),
1015+
field.span,
1016+
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(field.def_id))),
1017+
)
1018+
}
1019+
10111020
// For DST, or when drop needs to copy things around, all
10121021
// intermediate types must be sized.
10131022
let needs_drop_copy = || {
@@ -1024,6 +1033,7 @@ fn check_type_defn<'tcx, F>(
10241033
}
10251034
}
10261035
};
1036+
// All fields (except for possibly the last) should be sized.
10271037
let all_sized = all_sized || variant.fields.is_empty() || needs_drop_copy();
10281038
let unsized_len = if all_sized { 0 } else { 1 };
10291039
for (idx, field) in
@@ -1048,15 +1058,6 @@ fn check_type_defn<'tcx, F>(
10481058
);
10491059
}
10501060

1051-
// All field types must be well-formed.
1052-
for field in &variant.fields {
1053-
fcx.register_wf_obligation(
1054-
field.ty.into(),
1055-
field.span,
1056-
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(field.def_id))),
1057-
)
1058-
}
1059-
10601061
// Explicit `enum` discriminant values must const-evaluate successfully.
10611062
if let Some(discr_def_id) = variant.explicit_discr {
10621063
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)