about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNoah Lev <camelidcamel@gmail.com>2024-07-31 20:33:48 -0700
committerNoah Lev <camelidcamel@gmail.com>2024-08-05 17:37:11 -0700
commit18906754ccee76cebf92803c487942465c7f79a4 (patch)
tree6b3191de57edf1664e61cd8318cc4a5bbe1a3d27
parente57f3090aec33cdbf66063c866afaa5e1e78b9bb (diff)
downloadrust-18906754ccee76cebf92803c487942465c7f79a4.tar.gz
rust-18906754ccee76cebf92803c487942465c7f79a4.zip
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.
-rw-r--r--tests/ui/structs/field-implied-unsizing-wfcheck.rs10
-rw-r--r--tests/ui/structs/field-implied-unsizing-wfcheck.stderr23
2 files changed, 33 insertions, 0 deletions
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<dyn std::fmt::Debug>,
+    //~^ ERROR the size for values of type `(dyn Debug + 'static)` cannot be known at compilation time
+}
+
+struct Bar<T>(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<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/field-implied-unsizing-wfcheck.rs:6: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/field-implied-unsizing-wfcheck.rs:6:12
+   |
+LL | struct Bar<T>(T);
+   |            ^  - ...if indirection were used here: `Box<T>`
+   |            |
+   |            this could be changed to `T: ?Sized`...
+
+error: aborting due to 1 previous error
+
+For more information about this error, try `rustc --explain E0277`.