Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check ADT field is well-formed before checking it is sized #97780

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ pub enum ObligationCauseCode<'tcx> {
QuestionMark,

/// Well-formed checking. If a `WellFormedLoc` is provided,
/// then it will be used to eprform HIR-based wf checking
/// then it will be used to perform HIR-based wf checking
/// after an error occurs, in order to generate a more precise error span.
/// This is purely for diagnostic purposes - it is always
/// correct to use `MiscObligation` instead, or to specify
Expand Down
19 changes: 10 additions & 9 deletions compiler/rustc_typeck/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,15 @@ fn check_type_defn<'tcx, F>(
let packed = tcx.adt_def(item.def_id).repr().packed();

for variant in &variants {
// All field types must be well-formed.
for field in &variant.fields {
fcx.register_wf_obligation(
field.ty.into(),
field.span,
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(field.def_id))),
)
}

// For DST, or when drop needs to copy things around, all
// intermediate types must be sized.
let needs_drop_copy = || {
Expand All @@ -1024,6 +1033,7 @@ fn check_type_defn<'tcx, F>(
}
}
};
// All fields (except for possibly the last) should be sized.
let all_sized = all_sized || variant.fields.is_empty() || needs_drop_copy();
let unsized_len = if all_sized { 0 } else { 1 };
for (idx, field) in
Expand All @@ -1048,15 +1058,6 @@ fn check_type_defn<'tcx, F>(
);
}

// All field types must be well-formed.
for field in &variant.fields {
fcx.register_wf_obligation(
field.ty.into(),
field.span,
ObligationCauseCode::WellFormed(Some(WellFormedLoc::Ty(field.def_id))),
)
}

// Explicit `enum` discriminant values must const-evaluate successfully.
if let Some(discr_def_id) = variant.explicit_discr {
let discr_substs = InternalSubsts::identity_for_item(tcx, discr_def_id.to_def_id());
Expand Down
13 changes: 4 additions & 9 deletions src/test/ui/generic-associated-types/bugs/issue-80626.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ error[E0275]: overflow evaluating the requirement `LinkedList<A>: Sized`
LL | Next(A::Allocated<Self>)
| ^^^^^^^^^^^^^^^^^^
|
= note: no field of an enum variant may have a dynamically sized type
= help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
note: required by a bound in `Allocator::Allocated`
--> $DIR/issue-80626.rs:9:20
|
LL | Next(&A::Allocated<Self>)
| +
help: the `Box` type always has a statically known size and allocates its contents in the heap
|
LL | Next(Box<A::Allocated<Self>>)
| ++++ +
LL | type Allocated<T>;
| ^ required by this bound in `Allocator::Allocated`

error: aborting due to previous error

Expand Down
20 changes: 5 additions & 15 deletions src/test/ui/union/issue-81199.stderr
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
error[E0277]: the trait bound `T: Pointee` is not satisfied in `PtrComponents<T>`
error[E0277]: the trait bound `T: Pointee` is not satisfied
--> $DIR/issue-81199.rs:5:17
|
LL | components: PtrComponents<T>,
| ^^^^^^^^^^^^^^^^ within `PtrComponents<T>`, the trait `Pointee` is not implemented for `T`
| ^^^^^^^^^^^^^^^^ the trait `Pointee` is not implemented for `T`
|
note: required because it appears within the type `PtrComponents<T>`
--> $DIR/issue-81199.rs:10:8
note: required by a bound in `PtrComponents`
--> $DIR/issue-81199.rs:10:25
|
LL | struct PtrComponents<T: Pointee + ?Sized> {
| ^^^^^^^^^^^^^
= note: no field of a union may have a dynamically sized type
= help: change the field's type to have a statically known size
| ^^^^^^^ required by this bound in `PtrComponents`
help: consider further restricting this bound
|
LL | union PtrRepr<T: ?Sized + Pointee> {
| +++++++++
help: borrowed types always have a statically known size
|
LL | components: &PtrComponents<T>,
| +
help: the `Box` type always has a statically known size and allocates its contents in the heap
|
LL | components: Box<PtrComponents<T>>,
| ++++ +

error: aborting due to previous error

Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/wf/issue-96810.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
struct S<T: Tr>(T::Assoc);

trait Tr {
type Assoc;
}

struct Hoge<K> {
s: S<K>, //~ ERROR the trait bound `K: Tr` is not satisfied
a: u32,
}

fn main() {}
19 changes: 19 additions & 0 deletions src/test/ui/wf/issue-96810.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error[E0277]: the trait bound `K: Tr` is not satisfied
--> $DIR/issue-96810.rs:8:8
|
LL | s: S<K>,
| ^^^^ the trait `Tr` is not implemented for `K`
|
note: required by a bound in `S`
--> $DIR/issue-96810.rs:1:13
|
LL | struct S<T: Tr>(T::Assoc);
| ^^ required by this bound in `S`
help: consider restricting type parameter `K`
|
LL | struct Hoge<K: Tr> {
| ++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.