about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2025-02-20 02:21:58 +0000
committerMichael Goulet <michael@errs.io>2025-03-03 23:09:42 +0000
commit9d3d5a7fbb9d28d91e2d19d2b0bf5bc5af5b038c (patch)
tree51cf69163e6eeb73ffd67a9d5a69273ef5b3670b
parentc566318a782030a33f370d7490d7bdac9d8bfca4 (diff)
downloadrust-9d3d5a7fbb9d28d91e2d19d2b0bf5bc5af5b038c.tar.gz
rust-9d3d5a7fbb9d28d91e2d19d2b0bf5bc5af5b038c.zip
Check signature WF when lowering MIR body
-rw-r--r--compiler/rustc_hir_analysis/src/check/wfcheck.rs2
-rw-r--r--compiler/rustc_mir_transform/src/lib.rs18
-rw-r--r--tests/crashes/129095.rs5
-rw-r--r--tests/crashes/132960.rs2
-rw-r--r--tests/crashes/134654.rs3
-rw-r--r--tests/crashes/135128.rs2
-rw-r--r--tests/crashes/135570.rs3
-rw-r--r--tests/ui/consts/dont-ctfe-unsized-initializer.rs7
-rw-r--r--tests/ui/consts/dont-ctfe-unsized-initializer.stderr21
9 files changed, 60 insertions, 3 deletions
diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs
index 8199585ee61..54aaf889b40 100644
--- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs
+++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs
@@ -201,7 +201,7 @@ fn check_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua
         hir::Node::ImplItem(item) => check_impl_item(tcx, item),
         hir::Node::ForeignItem(item) => check_foreign_item(tcx, item),
         hir::Node::OpaqueTy(_) => Ok(crate::check::check::check_item_type(tcx, def_id)),
-        _ => unreachable!(),
+        _ => unreachable!("{node:?}"),
     };
 
     if let Some(generics) = node.generics() {
diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs
index 04c9375b831..5df12ac4d8b 100644
--- a/compiler/rustc_mir_transform/src/lib.rs
+++ b/compiler/rustc_mir_transform/src/lib.rs
@@ -513,6 +513,24 @@ fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &
         body.tainted_by_errors = Some(error_reported);
     }
 
+    // Also taint the body if it's within a top-level item that is not well formed.
+    //
+    // We do this check here and not during `mir_promoted` because that may result
+    // in borrowck cycles if WF requires looking into an opaque hidden type.
+    let root = tcx.typeck_root_def_id(def.to_def_id());
+    match tcx.def_kind(root) {
+        DefKind::Fn
+        | DefKind::AssocFn
+        | DefKind::Static { .. }
+        | DefKind::Const
+        | DefKind::AssocConst => {
+            if let Err(guar) = tcx.check_well_formed(root.expect_local()) {
+                body.tainted_by_errors = Some(guar);
+            }
+        }
+        _ => {}
+    }
+
     run_analysis_to_runtime_passes(tcx, &mut body);
 
     tcx.alloc_steal_mir(body)
diff --git a/tests/crashes/129095.rs b/tests/crashes/129095.rs
index d82474e18e7..b1bb74708c2 100644
--- a/tests/crashes/129095.rs
+++ b/tests/crashes/129095.rs
@@ -1,10 +1,13 @@
 //@ known-bug: rust-lang/rust#129095
 //@ compile-flags: -Zmir-enable-passes=+GVN -Zmir-enable-passes=+Inline -Zvalidate-mir
 
+#![feature(adt_const_params, unsized_const_params)]
+#![allow(incomplete_features)]
+
 pub fn function_with_bytes<const BYTES: &'static [u8; 4]>() -> &'static [u8] {
     BYTES
 }
 
 pub fn main() {
-    assert_eq!(function_with_bytes::<b"AAAAb">(), &[0x41, 0x41, 0x41, 0x41]);
+    assert_eq!(function_with_bytes::<b"AAAAA">(), &[0x41, 0x41, 0x41, 0x41]);
 }
diff --git a/tests/crashes/132960.rs b/tests/crashes/132960.rs
index 87d0ee7dede..c23a3393429 100644
--- a/tests/crashes/132960.rs
+++ b/tests/crashes/132960.rs
@@ -1,6 +1,6 @@
 //@ known-bug: #132960
 
-#![feature(adt_const_params, const_ptr_read, generic_const_exprs)]
+#![feature(adt_const_params, const_ptr_read, generic_const_exprs, unsized_const_params)]
 
 const fn concat_strs<const A: &'static str, const B: &'static str>() -> &'static str
 where
diff --git a/tests/crashes/134654.rs b/tests/crashes/134654.rs
index 8a8d18359e9..f2323fe4ecd 100644
--- a/tests/crashes/134654.rs
+++ b/tests/crashes/134654.rs
@@ -2,6 +2,9 @@
 //@ compile-flags: -Zmir-enable-passes=+GVN -Zmir-enable-passes=+Inline -Zvalidate-mir
 //@ only-x86_64
 
+#![feature(adt_const_params, unsized_const_params)]
+#![allow(incomplete_features)]
+
 fn function_with_bytes<const BYTES:
     &'static [u8; 0xa9008fb6c9d81e42_0e25730562a601c8_u128]>() -> &'static [u8] {
     BYTES
diff --git a/tests/crashes/135128.rs b/tests/crashes/135128.rs
index 2ce17df824a..a8fd1ae1ff5 100644
--- a/tests/crashes/135128.rs
+++ b/tests/crashes/135128.rs
@@ -1,6 +1,8 @@
 //@ known-bug: #135128
 //@ compile-flags: -Copt-level=1 --edition=2021
 
+#![feature(trivial_bounds)]
+
 async fn return_str() -> str
 where
     str: Sized,
diff --git a/tests/crashes/135570.rs b/tests/crashes/135570.rs
index a9eda97ef9d..7919ceb26d5 100644
--- a/tests/crashes/135570.rs
+++ b/tests/crashes/135570.rs
@@ -2,6 +2,9 @@
 //@compile-flags: -Zvalidate-mir -Zmir-enable-passes=+Inline -Copt-level=0 -Zmir-enable-passes=+GVN
 //@ only-x86_64
 
+#![feature(adt_const_params, unsized_const_params)]
+#![allow(incomplete_features)]
+
 fn function_with_bytes<const BYTES: &'static [u8; 0xc7b889180b67b07d_bc1a3c88783d35b5_u128]>(
 ) -> &'static [u8] {
     BYTES
diff --git a/tests/ui/consts/dont-ctfe-unsized-initializer.rs b/tests/ui/consts/dont-ctfe-unsized-initializer.rs
new file mode 100644
index 00000000000..cca38b760dc
--- /dev/null
+++ b/tests/ui/consts/dont-ctfe-unsized-initializer.rs
@@ -0,0 +1,7 @@
+static S: str = todo!();
+//~^ ERROR the size for values of type `str` cannot be known at compilation time
+
+const C: str = todo!();
+//~^ ERROR the size for values of type `str` cannot be known at compilation time
+
+fn main() {}
diff --git a/tests/ui/consts/dont-ctfe-unsized-initializer.stderr b/tests/ui/consts/dont-ctfe-unsized-initializer.stderr
new file mode 100644
index 00000000000..e69790fc182
--- /dev/null
+++ b/tests/ui/consts/dont-ctfe-unsized-initializer.stderr
@@ -0,0 +1,21 @@
+error[E0277]: the size for values of type `str` cannot be known at compilation time
+  --> $DIR/dont-ctfe-unsized-initializer.rs:1:11
+   |
+LL | static S: str = todo!();
+   |           ^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `str`
+   = note: statics and constants must have a statically known size
+
+error[E0277]: the size for values of type `str` cannot be known at compilation time
+  --> $DIR/dont-ctfe-unsized-initializer.rs:4:10
+   |
+LL | const C: str = todo!();
+   |          ^^^ doesn't have a size known at compile-time
+   |
+   = help: the trait `Sized` is not implemented for `str`
+   = note: statics and constants must have a statically known size
+
+error: aborting due to 2 previous errors
+
+For more information about this error, try `rustc --explain E0277`.