From 18906754ccee76cebf92803c487942465c7f79a4 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Wed, 31 Jul 2024 20:33:48 -0700 Subject: [PATCH] Add test for WF check of implied unsizing in struct fields Note that the test output is currently *incorrect*. We should be emitting an error at the use site too, not just at the definition. This is partly for UI reasons, but mainly to fix a related ICE where a const generic body is not tainted with an error since no usage error is reported. --- .../structs/field-implied-unsizing-wfcheck.rs | 10 ++++++++ .../field-implied-unsizing-wfcheck.stderr | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 tests/ui/structs/field-implied-unsizing-wfcheck.rs create mode 100644 tests/ui/structs/field-implied-unsizing-wfcheck.stderr diff --git a/tests/ui/structs/field-implied-unsizing-wfcheck.rs b/tests/ui/structs/field-implied-unsizing-wfcheck.rs new file mode 100644 index 00000000000..24da4b5e89d --- /dev/null +++ b/tests/ui/structs/field-implied-unsizing-wfcheck.rs @@ -0,0 +1,10 @@ +struct Foo { + nested: &'static Bar, + //~^ ERROR the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time +} + +struct Bar(T); + +fn main() { + let x = Foo { nested: &Bar(4) }; +} diff --git a/tests/ui/structs/field-implied-unsizing-wfcheck.stderr b/tests/ui/structs/field-implied-unsizing-wfcheck.stderr new file mode 100644 index 00000000000..c47918c7d16 --- /dev/null +++ b/tests/ui/structs/field-implied-unsizing-wfcheck.stderr @@ -0,0 +1,23 @@ +error[E0277]: the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time + --> $DIR/field-implied-unsizing-wfcheck.rs:2:13 + | +LL | nested: &'static Bar, + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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/field-implied-unsizing-wfcheck.rs:6:12 + | +LL | struct Bar(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` + --> $DIR/field-implied-unsizing-wfcheck.rs:6:12 + | +LL | struct Bar(T); + | ^ - ...if indirection were used here: `Box` + | | + | this could be changed to `T: ?Sized`... + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0277`.