Skip to content

Commit

Permalink
Rollup merge of rust-lang#97780 - compiler-errors:field-wfcheck-befor…
Browse files Browse the repository at this point in the history
…e-sized, r=jackh726

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

Fixes rust-lang#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.
  • Loading branch information
JohnTitor committed Jun 26, 2022
2 parents c80c4b8 + c1f4f98 commit b3e6146
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 34 deletions.
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 @@ -406,7 +406,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 @@ -990,6 +990,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 @@ -1006,6 +1015,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 @@ -1030,15 +1040,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`.

0 comments on commit b3e6146

Please sign in to comment.