Skip to content

Commit c3e8ba3

Browse files
Check signature WF when lowering MIR body
1 parent 8e88547 commit c3e8ba3

File tree

9 files changed

+60
-3
lines changed

9 files changed

+60
-3
lines changed

Diff for: compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn check_well_formed(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), ErrorGua
201201
hir::Node::ImplItem(item) => check_impl_item(tcx, item),
202202
hir::Node::ForeignItem(item) => check_foreign_item(tcx, item),
203203
hir::Node::OpaqueTy(_) => Ok(crate::check::check::check_item_type(tcx, def_id)),
204-
_ => unreachable!(),
204+
_ => unreachable!("{node:?}"),
205205
};
206206

207207
if let Some(generics) = node.generics() {

Diff for: compiler/rustc_mir_transform/src/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,24 @@ fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &
513513
body.tainted_by_errors = Some(error_reported);
514514
}
515515

516+
// Also taint the body if it's within a top-level item that is not well formed.
517+
//
518+
// We do this check here and not during `mir_promoted` because that may result
519+
// in borrowck cycles if WF requires looking into an opaque hidden type.
520+
let root = tcx.typeck_root_def_id(def.to_def_id());
521+
match tcx.def_kind(root) {
522+
DefKind::Fn
523+
| DefKind::AssocFn
524+
| DefKind::Static { .. }
525+
| DefKind::Const
526+
| DefKind::AssocConst => {
527+
if let Err(guar) = tcx.check_well_formed(root.expect_local()) {
528+
body.tainted_by_errors = Some(guar);
529+
}
530+
}
531+
_ => {}
532+
}
533+
516534
run_analysis_to_runtime_passes(tcx, &mut body);
517535

518536
tcx.alloc_steal_mir(body)

Diff for: tests/crashes/129095.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
//@ known-bug: rust-lang/rust#129095
22
//@ compile-flags: -Zmir-enable-passes=+GVN -Zmir-enable-passes=+Inline -Zvalidate-mir
33

4+
#![feature(adt_const_params, unsized_const_params)]
5+
#![allow(incomplete_features)]
6+
47
pub fn function_with_bytes<const BYTES: &'static [u8; 4]>() -> &'static [u8] {
58
BYTES
69
}
710

811
pub fn main() {
9-
assert_eq!(function_with_bytes::<b"AAAAb">(), &[0x41, 0x41, 0x41, 0x41]);
12+
assert_eq!(function_with_bytes::<b"AAAAA">(), &[0x41, 0x41, 0x41, 0x41]);
1013
}

Diff for: tests/crashes/132960.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@ known-bug: #132960
22

3-
#![feature(adt_const_params, const_ptr_read, generic_const_exprs)]
3+
#![feature(adt_const_params, const_ptr_read, generic_const_exprs, unsized_const_params)]
44

55
const fn concat_strs<const A: &'static str, const B: &'static str>() -> &'static str
66
where

Diff for: tests/crashes/134654.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
//@ compile-flags: -Zmir-enable-passes=+GVN -Zmir-enable-passes=+Inline -Zvalidate-mir
33
//@ only-x86_64
44

5+
#![feature(adt_const_params, unsized_const_params)]
6+
#![allow(incomplete_features)]
7+
58
fn function_with_bytes<const BYTES:
69
&'static [u8; 0xa9008fb6c9d81e42_0e25730562a601c8_u128]>() -> &'static [u8] {
710
BYTES

Diff for: tests/crashes/135128.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//@ known-bug: #135128
22
//@ compile-flags: -Copt-level=1 --edition=2021
33

4+
#![feature(trivial_bounds)]
5+
46
async fn return_str() -> str
57
where
68
str: Sized,

Diff for: tests/crashes/135570.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
//@compile-flags: -Zvalidate-mir -Zmir-enable-passes=+Inline -Copt-level=0 -Zmir-enable-passes=+GVN
33
//@ only-x86_64
44

5+
#![feature(adt_const_params, unsized_const_params)]
6+
#![allow(incomplete_features)]
7+
58
fn function_with_bytes<const BYTES: &'static [u8; 0xc7b889180b67b07d_bc1a3c88783d35b5_u128]>(
69
) -> &'static [u8] {
710
BYTES

Diff for: tests/ui/consts/dont-ctfe-unsized-initializer.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
static S: str = todo!();
2+
//~^ ERROR the size for values of type `str` cannot be known at compilation time
3+
4+
const C: str = todo!();
5+
//~^ ERROR the size for values of type `str` cannot be known at compilation time
6+
7+
fn main() {}

Diff for: tests/ui/consts/dont-ctfe-unsized-initializer.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0277]: the size for values of type `str` cannot be known at compilation time
2+
--> $DIR/dont-ctfe-unsized-initializer.rs:1:11
3+
|
4+
LL | static S: str = todo!();
5+
| ^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `str`
8+
= note: statics and constants must have a statically known size
9+
10+
error[E0277]: the size for values of type `str` cannot be known at compilation time
11+
--> $DIR/dont-ctfe-unsized-initializer.rs:4:10
12+
|
13+
LL | const C: str = todo!();
14+
| ^^^ doesn't have a size known at compile-time
15+
|
16+
= help: the trait `Sized` is not implemented for `str`
17+
= note: statics and constants must have a statically known size
18+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)