Skip to content

Commit

Permalink
Auto merge of rust-lang#128714 - camelid:wf-struct-exprs, r=BoxyUwU
Browse files Browse the repository at this point in the history
WF-check struct field types at construction site

Fixes rust-lang#126272.
Fixes rust-lang#127299.

Rustc of course already WF-checked the field types at the definition
site, but for error tainting of consts to work properly, there needs to
be an error emitted at the use site. Previously, with no use-site error,
we proceeded with CTFE and ran into ICEs since we are running code with
type errors.

Emitting use-site errors also brings struct-like constructors more in
line with fn-like constructors since they already emit use-site errors
for WF issues.

r? `@BoxyUwU`
  • Loading branch information
bors committed Aug 10, 2024
2 parents 69b380d + 2f4603b commit 19469cb
Show file tree
Hide file tree
Showing 17 changed files with 560 additions and 56 deletions.
9 changes: 9 additions & 0 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1736,6 +1736,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Ty::new_error(tcx, guar)
};

// Check that the expected field type is WF. Otherwise, we emit no use-site error
// in the case of coercions for non-WF fields, which leads to incorrect error
// tainting. See issue #126272.
self.register_wf_obligation(
field_type.into(),
field.expr.span,
ObligationCauseCode::WellFormed(None),
);

// Make sure to give a type to the field even if there's
// an error, so we can continue type-checking.
let ty = self.check_expr_with_hint(field.expr, field_type);
Expand Down
28 changes: 0 additions & 28 deletions tests/crashes/126272.rs

This file was deleted.

12 changes: 0 additions & 12 deletions tests/crashes/127299.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// This ensures we don't ICE in situations like rust-lang/rust#126272.

#![feature(adt_const_params)]
#![allow(incomplete_features)]

use std::marker::ConstParamTy;

#[derive(Debug, PartialEq, Eq, ConstParamTy)]
//~^ ERROR the trait `ConstParamTy_`
//~| ERROR the trait `ConstParamTy_`
struct Foo {
nested: &'static Bar<dyn std::fmt::Debug>,
//~^ ERROR the size for values
//~| ERROR the size for values
//~| ERROR binary operation `==` cannot
//~| ERROR the trait bound `dyn Debug: Eq`
//~| ERROR the size for values
}

#[derive(Debug, PartialEq, Eq, ConstParamTy)]
struct Bar<T>(T);

struct Test<const F: Foo>;

fn main() {
let x: Test<{ Foo { nested: &Bar(4) } }> = Test;
//~^ ERROR the size for values
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
error[E0277]: the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
--> $DIR/unsizing-wfcheck-issue-126272.rs:12:13
|
LL | nested: &'static Bar<dyn std::fmt::Debug>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Debug + 'static)`
note: required by an implicit `Sized` bound in `Bar`
--> $DIR/unsizing-wfcheck-issue-126272.rs:21:12
|
LL | struct Bar<T>(T);
| ^ required by the implicit `Sized` requirement on this type parameter in `Bar`
help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box<T>`
--> $DIR/unsizing-wfcheck-issue-126272.rs:21:12
|
LL | struct Bar<T>(T);
| ^ - ...if indirection were used here: `Box<T>`
| |
| this could be changed to `T: ?Sized`...

error[E0204]: the trait `ConstParamTy_` cannot be implemented for this type
--> $DIR/unsizing-wfcheck-issue-126272.rs:8:32
|
LL | #[derive(Debug, PartialEq, Eq, ConstParamTy)]
| ^^^^^^^^^^^^
...
LL | nested: &'static Bar<dyn std::fmt::Debug>,
| ----------------------------------------- this field does not implement `ConstParamTy_`
|
= note: this error originates in the derive macro `ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0204]: the trait `ConstParamTy_` cannot be implemented for this type
--> $DIR/unsizing-wfcheck-issue-126272.rs:8:32
|
LL | #[derive(Debug, PartialEq, Eq, ConstParamTy)]
| ^^^^^^^^^^^^
...
LL | nested: &'static Bar<dyn std::fmt::Debug>,
| ----------------------------------------- this field does not implement `ConstParamTy_`
|
note: the `ConstParamTy_` impl for `&'static Bar<(dyn Debug + 'static)>` requires that `(dyn Debug + 'static): Eq`
--> $DIR/unsizing-wfcheck-issue-126272.rs:12:13
|
LL | nested: &'static Bar<dyn std::fmt::Debug>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: the `ConstParamTy_` impl for `&'static Bar<(dyn Debug + 'static)>` requires that `(dyn Debug + 'static): Sized`
--> $DIR/unsizing-wfcheck-issue-126272.rs:12:13
|
LL | nested: &'static Bar<dyn std::fmt::Debug>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: the `ConstParamTy_` impl for `&'static Bar<(dyn Debug + 'static)>` requires that `(dyn Debug + 'static): UnsizedConstParamTy`
--> $DIR/unsizing-wfcheck-issue-126272.rs:12:13
|
LL | nested: &'static Bar<dyn std::fmt::Debug>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the derive macro `ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
--> $DIR/unsizing-wfcheck-issue-126272.rs:12:5
|
LL | #[derive(Debug, PartialEq, Eq, ConstParamTy)]
| ----- in this derive macro expansion
...
LL | nested: &'static Bar<dyn std::fmt::Debug>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Debug + 'static)`, which is required by `&&'static Bar<(dyn Debug + 'static)>: Debug`
= help: the trait `Debug` is implemented for `Bar<T>`
note: required for `Bar<(dyn Debug + 'static)>` to implement `Debug`
--> $DIR/unsizing-wfcheck-issue-126272.rs:20:10
|
LL | #[derive(Debug, PartialEq, Eq, ConstParamTy)]
| ^^^^^
LL | struct Bar<T>(T);
| - unsatisfied trait bound introduced in this `derive` macro
= note: 2 redundant requirements hidden
= note: required for `&&'static Bar<(dyn Debug + 'static)>` to implement `Debug`
= note: required for the cast from `&&&'static Bar<(dyn Debug + 'static)>` to `&dyn Debug`
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0369]: binary operation `==` cannot be applied to type `&Bar<dyn Debug>`
--> $DIR/unsizing-wfcheck-issue-126272.rs:12:5
|
LL | #[derive(Debug, PartialEq, Eq, ConstParamTy)]
| --------- in this derive macro expansion
...
LL | nested: &'static Bar<dyn std::fmt::Debug>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `dyn Debug: Eq` is not satisfied
--> $DIR/unsizing-wfcheck-issue-126272.rs:12:5
|
LL | #[derive(Debug, PartialEq, Eq, ConstParamTy)]
| -- in this derive macro expansion
...
LL | nested: &'static Bar<dyn std::fmt::Debug>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Eq` is not implemented for `dyn Debug`, which is required by `&'static Bar<dyn Debug>: Eq`
|
= help: the trait `Eq` is implemented for `Bar<T>`
note: required for `Bar<dyn Debug>` to implement `Eq`
--> $DIR/unsizing-wfcheck-issue-126272.rs:20:28
|
LL | #[derive(Debug, PartialEq, Eq, ConstParamTy)]
| ^^ unsatisfied trait bound introduced in this `derive` macro
= note: 1 redundant requirement hidden
= note: required for `&'static Bar<dyn Debug>` to implement `Eq`
note: required by a bound in `AssertParamIsEq`
--> $SRC_DIR/core/src/cmp.rs:LL:COL
= note: this error originates in the derive macro `Eq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the size for values of type `dyn Debug` cannot be known at compilation time
--> $DIR/unsizing-wfcheck-issue-126272.rs:12:5
|
LL | #[derive(Debug, PartialEq, Eq, ConstParamTy)]
| -- in this derive macro expansion
...
LL | nested: &'static Bar<dyn std::fmt::Debug>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `dyn Debug`
note: required by an implicit `Sized` bound in `Bar`
--> $DIR/unsizing-wfcheck-issue-126272.rs:21:12
|
LL | struct Bar<T>(T);
| ^ required by the implicit `Sized` requirement on this type parameter in `Bar`
help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box<T>`
--> $DIR/unsizing-wfcheck-issue-126272.rs:21:12
|
LL | struct Bar<T>(T);
| ^ - ...if indirection were used here: `Box<T>`
| |
| this could be changed to `T: ?Sized`...
= note: this error originates in the derive macro `Eq` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
--> $DIR/unsizing-wfcheck-issue-126272.rs:26:33
|
LL | let x: Test<{ Foo { nested: &Bar(4) } }> = Test;
| ^^^^^^^ doesn't have a size known at compile-time
|
= help: the trait `Sized` is not implemented for `(dyn Debug + 'static)`
note: required by an implicit `Sized` bound in `Bar`
--> $DIR/unsizing-wfcheck-issue-126272.rs:21:12
|
LL | struct Bar<T>(T);
| ^ required by the implicit `Sized` requirement on this type parameter in `Bar`
help: you could relax the implicit `Sized` bound on `T` if it were used through indirection like `&T` or `Box<T>`
--> $DIR/unsizing-wfcheck-issue-126272.rs:21:12
|
LL | struct Bar<T>(T);
| ^ - ...if indirection were used here: `Box<T>`
| |
| this could be changed to `T: ?Sized`...

error: aborting due to 8 previous errors

Some errors have detailed explanations: E0204, E0277, E0369.
For more information about an error, try `rustc --explain E0204`.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ impl<const N: usize> Example<N> {
a: [0.; N],
b: [0.; complex_maths(N)],
//~^ ERROR: unconstrained generic constant
//~| ERROR: unconstrained generic constant
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ help: try adding a `where` bound
LL | pub struct Example<const N: usize> where [(); complex_maths(N)]: {
| +++++++++++++++++++++++++++++

error: unconstrained generic constant
--> $DIR/no_where_clause.rs:18:10
|
LL | b: [0.; complex_maths(N)],
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: try adding a `where` bound
|
LL | pub fn new() -> Self where [(); complex_maths(N)]: {
| +++++++++++++++++++++++++++++

error: unconstrained generic constant
--> $DIR/no_where_clause.rs:18:15
|
Expand All @@ -20,5 +31,5 @@ help: try adding a `where` bound
LL | pub fn new() -> Self where [(); complex_maths(N)]: {
| +++++++++++++++++++++++++++++

error: aborting due to 2 previous errors
error: aborting due to 3 previous errors

2 changes: 1 addition & 1 deletion tests/ui/generic-associated-types/issue-71176.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ struct Holder<B> {

fn main() {
Holder {
inner: Box::new(()),
inner: Box::new(()), //~ ERROR: the trait `Provider` cannot be made into an object
};
}
18 changes: 17 additions & 1 deletion tests/ui/generic-associated-types/issue-71176.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,23 @@ LL | type A<'a>;
= help: consider moving `A` to another trait
= help: only type `()` implements the trait, consider using it directly instead

error: aborting due to 4 previous errors
error[E0038]: the trait `Provider` cannot be made into an object
--> $DIR/issue-71176.rs:19:16
|
LL | inner: Box::new(()),
| ^^^^^^^^^^^^ `Provider` cannot be made into an object
|
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/issue-71176.rs:2:10
|
LL | trait Provider {
| -------- this trait cannot be made into an object...
LL | type A<'a>;
| ^ ...because it contains the generic associated type `A`
= help: consider moving `A` to another trait
= help: only type `()` implements the trait, consider using it directly instead

error: aborting due to 5 previous errors

Some errors have detailed explanations: E0038, E0107.
For more information about an error, try `rustc --explain E0038`.
1 change: 1 addition & 0 deletions tests/ui/issues/issue-19380.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ struct Bar {
const FOO : Foo = Foo;
const BAR : Bar = Bar { foos: &[&FOO]};
//~^ ERROR E0038
//~| ERROR E0038

fn main() { }
25 changes: 24 additions & 1 deletion tests/ui/issues/issue-19380.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ help: alternatively, consider constraining `qiz` so it does not apply to trait o
LL | fn qiz() where Self: Sized;
| +++++++++++++++++

error: aborting due to 2 previous errors
error[E0038]: the trait `Qiz` cannot be made into an object
--> $DIR/issue-19380.rs:16:31
|
LL | const BAR : Bar = Bar { foos: &[&FOO]};
| ^^^^^^^ `Qiz` cannot be made into an object
|
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/issue-19380.rs:2:6
|
LL | trait Qiz {
| --- this trait cannot be made into an object...
LL | fn qiz();
| ^^^ ...because associated function `qiz` has no `self` parameter
= help: only type `Foo` implements the trait, consider using it directly instead
help: consider turning `qiz` into a method by giving it a `&self` argument
|
LL | fn qiz(&self);
| +++++
help: alternatively, consider constraining `qiz` so it does not apply to trait objects
|
LL | fn qiz() where Self: Sized;
| +++++++++++++++++

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0038`.
17 changes: 17 additions & 0 deletions tests/ui/statics/unsizing-wfcheck-issue-127299.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This ensures we don't ICE in situations like rust-lang/rust#127299.

trait Qux {
fn bar() -> i32;
}

pub struct Lint {
pub desc: &'static dyn Qux,
//~^ ERROR cannot be made into an object
}

static FOO: &Lint = &Lint { desc: "desc" };
//~^ ERROR cannot be shared between threads safely
//~| ERROR cannot be made into an object
//~| ERROR cannot be made into an object

fn main() {}
Loading

0 comments on commit 19469cb

Please sign in to comment.