about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-04-09 10:57:41 +0000
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>2024-04-16 10:43:41 +0000
commit801413ecd18f8f1180cc74ca93f9279f3e95c862 (patch)
treeddd37d71e83020d996cabb152de087f79118b32b
parent63f70b3d104e20289a1a0df82747066c3d85b9a1 (diff)
downloadrust-801413ecd18f8f1180cc74ca93f9279f3e95c862.tar.gz
rust-801413ecd18f8f1180cc74ca93f9279f3e95c862.zip
Taint const qualifs if a static is referenced that didn't pass wfcheck
-rw-r--r--compiler/rustc_const_eval/src/transform/check_consts/check.rs5
-rw-r--r--tests/crashes/123153.rs17
-rw-r--r--tests/ui/statics/unsized_type2.rs21
-rw-r--r--tests/ui/statics/unsized_type2.stderr37
4 files changed, 63 insertions, 17 deletions
diff --git a/compiler/rustc_const_eval/src/transform/check_consts/check.rs b/compiler/rustc_const_eval/src/transform/check_consts/check.rs
index 543996c86ba..eb7f9385730 100644
--- a/compiler/rustc_const_eval/src/transform/check_consts/check.rs
+++ b/compiler/rustc_const_eval/src/transform/check_consts/check.rs
@@ -331,6 +331,11 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
         if self.tcx.is_thread_local_static(def_id) {
             self.tcx.dcx().span_bug(span, "tls access is checked in `Rvalue::ThreadLocalRef`");
         }
+        if let Some(def_id) = def_id.as_local()
+            && let Err(guar) = self.tcx.at(span).check_well_formed(hir::OwnerId { def_id })
+        {
+            self.error_emitted = Some(guar);
+        }
         self.check_op_spanned(ops::StaticAccess, span)
     }
 
diff --git a/tests/crashes/123153.rs b/tests/crashes/123153.rs
deleted file mode 100644
index d2c32ecd73e..00000000000
--- a/tests/crashes/123153.rs
+++ /dev/null
@@ -1,17 +0,0 @@
-//@ known-bug: #123153
-pub struct wl_interface {
-    pub version: str,
-}
-
-pub struct Interface {
-    pub other_interfaces: &'static [&'static Interface],
-    pub c_ptr: Option<&'static wl_interface>,
-}
-
-pub static mut wl_callback_interface: wl_interface = wl_interface { version: 0 };
-
-pub static WL_CALLBACK_INTERFACE: Interface =
-    Interface { other_interfaces: &[], c_ptr: Some(unsafe { &wl_callback_interface }) };
-
-
-fn main() {}
diff --git a/tests/ui/statics/unsized_type2.rs b/tests/ui/statics/unsized_type2.rs
new file mode 100644
index 00000000000..303926849e6
--- /dev/null
+++ b/tests/ui/statics/unsized_type2.rs
@@ -0,0 +1,21 @@
+//! This test used to actually start evaluating the static even though
+//! there were errors in typeck.
+//! issue: rust-lang/rust#123153
+
+pub struct Foo {
+    pub version: str,
+}
+
+pub struct Bar {
+    pub ok: &'static [&'static Bar],
+    pub bad: &'static Foo,
+}
+
+pub static WITH_ERROR: Foo = Foo { version: 0 };
+//~^ ERROR the size for values of type `str` cannot be known at compilation time
+//~| ERROR the size for values of type `str` cannot be known at compilation time
+//~| ERROR mismatched types
+
+pub static USE_WITH_ERROR: Bar = Bar { ok: &[], bad: &WITH_ERROR };
+
+fn main() {}
diff --git a/tests/ui/statics/unsized_type2.stderr b/tests/ui/statics/unsized_type2.stderr
new file mode 100644
index 00000000000..4e47b37afdc
--- /dev/null
+++ b/tests/ui/statics/unsized_type2.stderr
@@ -0,0 +1,37 @@
+error[E0277]: the size for values of type `str` cannot be known at compilation time
+  --> $DIR/unsized_type2.rs:14:24
+   |
+LL | pub static WITH_ERROR: Foo = Foo { version: 0 };
+   |                        ^^^ doesn't have a size known at compile-time
+   |
+   = help: within `Foo`, the trait `Sized` is not implemented for `str`, which is required by `Foo: Sized`
+note: required because it appears within the type `Foo`
+  --> $DIR/unsized_type2.rs:5:12
+   |
+LL | pub struct Foo {
+   |            ^^^
+
+error[E0277]: the size for values of type `str` cannot be known at compilation time
+  --> $DIR/unsized_type2.rs:14:30
+   |
+LL | pub static WITH_ERROR: Foo = Foo { version: 0 };
+   |                              ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
+   |
+   = help: within `Foo`, the trait `Sized` is not implemented for `str`, which is required by `Foo: Sized`
+note: required because it appears within the type `Foo`
+  --> $DIR/unsized_type2.rs:5:12
+   |
+LL | pub struct Foo {
+   |            ^^^
+   = note: constant expressions must have a statically known size
+
+error[E0308]: mismatched types
+  --> $DIR/unsized_type2.rs:14:45
+   |
+LL | pub static WITH_ERROR: Foo = Foo { version: 0 };
+   |                                             ^ expected `str`, found integer
+
+error: aborting due to 3 previous errors
+
+Some errors have detailed explanations: E0277, E0308.
+For more information about an error, try `rustc --explain E0277`.