Check signature WF when lowering MIR body

This commit is contained in:
Michael Goulet 2025-02-20 02:21:58 +00:00
parent c566318a78
commit 9d3d5a7fbb
9 changed files with 60 additions and 3 deletions

View File

@ -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() {

View File

@ -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)

View File

@ -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]);
}

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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() {}

View File

@ -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`.