Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Check signature WF when lowering MIR body
  • Loading branch information
compiler-errors committed Mar 3, 2025
commit 9d3d5a7fbb9d28d91e2d19d2b0bf5bc5af5b038c
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
18 changes: 18 additions & 0 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion tests/crashes/129095.rs
Original file line number Diff line number Diff line change
@@ -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]);
}
2 changes: 1 addition & 1 deletion tests/crashes/132960.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions tests/crashes/134654.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/crashes/135128.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 3 additions & 0 deletions tests/crashes/135570.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/consts/dont-ctfe-unsized-initializer.rs
Original file line number Diff line number Diff line change
@@ -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() {}
21 changes: 21 additions & 0 deletions tests/ui/consts/dont-ctfe-unsized-initializer.stderr
Original file line number Diff line number Diff line change
@@ -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`.