Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7527550

Browse files
committedFeb 20, 2025
Auto merge of rust-lang#137298 - compiler-errors:mir-wf, r=<try>
Check signature WF when lowering MIR body Alternative to rust-lang#137233. rust-lang#137233 (comment) Fixes rust-lang#137186 We do this check in `mir_drops_elaborated_and_const_checked` and not during `mir_promoted` because that may result in borrowck cycles if WF requires looking into an opaque hidden type. This causes some TAIT tests to fail unnecessarily. r? lcnr try-job: test-various
2 parents f04bbc6 + bb7b912 commit 7527550

20 files changed

+86
-10
lines changed
 

‎compiler/rustc_hir_analysis/src/check/wfcheck.rs

+12-4
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() {
@@ -1108,7 +1108,13 @@ fn check_associated_item(
11081108
let ty = tcx.type_of(item.def_id).instantiate_identity();
11091109
let ty = wfcx.normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
11101110
wfcx.register_wf_obligation(span, loc, ty.into());
1111-
check_sized_if_body(wfcx, item.def_id.expect_local(), ty, Some(span));
1111+
check_sized_if_body(
1112+
wfcx,
1113+
item.def_id.expect_local(),
1114+
ty,
1115+
Some(span),
1116+
ObligationCauseCode::SizedConstOrStatic,
1117+
);
11121118
Ok(())
11131119
}
11141120
ty::AssocKind::Fn => {
@@ -1354,7 +1360,7 @@ fn check_item_type(
13541360
traits::ObligationCause::new(
13551361
ty_span,
13561362
wfcx.body_def_id,
1357-
ObligationCauseCode::WellFormed(None),
1363+
ObligationCauseCode::SizedConstOrStatic,
13581364
),
13591365
wfcx.param_env,
13601366
item_ty,
@@ -1698,6 +1704,7 @@ fn check_fn_or_method<'tcx>(
16981704
hir::FnRetTy::Return(ty) => Some(ty.span),
16991705
hir::FnRetTy::DefaultReturn(_) => None,
17001706
},
1707+
ObligationCauseCode::SizedReturnType,
17011708
);
17021709
}
17031710

@@ -1706,13 +1713,14 @@ fn check_sized_if_body<'tcx>(
17061713
def_id: LocalDefId,
17071714
ty: Ty<'tcx>,
17081715
maybe_span: Option<Span>,
1716+
code: ObligationCauseCode<'tcx>,
17091717
) {
17101718
let tcx = wfcx.tcx();
17111719
if let Some(body) = tcx.hir_maybe_body_owned_by(def_id) {
17121720
let span = maybe_span.unwrap_or(body.value.span);
17131721

17141722
wfcx.register_bound(
1715-
ObligationCause::new(span, def_id, traits::ObligationCauseCode::SizedReturnType),
1723+
ObligationCause::new(span, def_id, code),
17161724
wfcx.param_env,
17171725
ty,
17181726
tcx.require_lang_item(LangItem::Sized, Some(span)),

‎compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18101810
crate::GatherLocalsVisitor::new(&fcx).visit_body(body);
18111811

18121812
let ty = fcx.check_expr_with_expectation(body.value, expected);
1813-
fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::ConstSized);
1813+
fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::SizedConstOrStatic);
18141814
fcx.write_ty(block.hir_id, ty);
18151815
ty
18161816
}

‎compiler/rustc_middle/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ pub enum ObligationCauseCode<'tcx> {
273273
},
274274

275275
/// Constant expressions must be sized.
276-
ConstSized,
276+
SizedConstOrStatic,
277277

278278
/// `static` items must have `Sync` type.
279279
SharedStatic,

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

‎compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3125,8 +3125,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
31253125
Applicability::MachineApplicable,
31263126
);
31273127
}
3128-
ObligationCauseCode::ConstSized => {
3129-
err.note("constant expressions must have a statically known size");
3128+
ObligationCauseCode::SizedConstOrStatic => {
3129+
err.note("statics and constants must have a statically known size");
31303130
}
31313131
ObligationCauseCode::InlineAsmSized => {
31323132
err.note("all inline asm arguments must have a statically known size");

‎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
}

‎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

‎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

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

‎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

‎tests/ui/consts/const-slice-array-deref.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | const ONE: [u16] = [1];
55
| ^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[u16]`
8+
= note: statics and constants must have a statically known size
89

910
error[E0308]: mismatched types
1011
--> $DIR/const-slice-array-deref.rs:1:20

‎tests/ui/consts/const-unsized.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | const CONST_0: dyn Debug + Sync = *(&0 as &(dyn Debug + Sync));
55
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
8+
= note: statics and constants must have a statically known size
89

910
error[E0277]: the size for values of type `str` cannot be known at compilation time
1011
--> $DIR/const-unsized.rs:7:18
@@ -13,6 +14,7 @@ LL | const CONST_FOO: str = *"foo";
1314
| ^^^ doesn't have a size known at compile-time
1415
|
1516
= help: the trait `Sized` is not implemented for `str`
17+
= note: statics and constants must have a statically known size
1618

1719
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
1820
--> $DIR/const-unsized.rs:11:18
@@ -21,6 +23,7 @@ LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
2123
| ^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
2224
|
2325
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
26+
= note: statics and constants must have a statically known size
2427

2528
error[E0277]: the size for values of type `str` cannot be known at compilation time
2629
--> $DIR/const-unsized.rs:15:20
@@ -29,6 +32,7 @@ LL | static STATIC_BAR: str = *"bar";
2932
| ^^^ doesn't have a size known at compile-time
3033
|
3134
= help: the trait `Sized` is not implemented for `str`
35+
= note: statics and constants must have a statically known size
3236

3337
error[E0507]: cannot move out of a shared reference
3438
--> $DIR/const-unsized.rs:3:35

‎tests/ui/consts/const_refs_to_static-ice-121413.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ LL | static FOO: Sync = AtomicUsize::new(0);
3030
| ^^^^ doesn't have a size known at compile-time
3131
|
3232
= help: the trait `Sized` is not implemented for `(dyn Sync + 'static)`
33+
= note: statics and constants must have a statically known size
3334

3435
error: aborting due to 2 previous errors; 1 warning emitted
3536

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

‎tests/ui/extern/issue-36122-accessing-externed-dst.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | static symbol: [usize];
55
| ^^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[usize]`
8+
= note: statics and constants must have a statically known size
89

910
error[E0133]: use of extern static is unsafe and requires unsafe function or block
1011
--> $DIR/issue-36122-accessing-externed-dst.rs:5:20

‎tests/ui/issues/issue-54410.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | pub static mut symbol: [i8];
55
| ^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `[i8]`
8+
= note: statics and constants must have a statically known size
89

910
error: aborting due to 1 previous error
1011

‎tests/ui/static/issue-24446.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ LL | static foo: dyn Fn() -> u32 = || -> u32 {
1414
| ^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
1515
|
1616
= help: the trait `Sized` is not implemented for `(dyn Fn() -> u32 + 'static)`
17+
= note: statics and constants must have a statically known size
1718

1819
error[E0308]: mismatched types
1920
--> $DIR/issue-24446.rs:2:35

‎tests/ui/statics/unsized_type2.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ note: required because it appears within the type `Foo`
1010
|
1111
LL | pub struct Foo {
1212
| ^^^
13+
= note: statics and constants must have a statically known size
1314

1415
error[E0308]: mismatched types
1516
--> $DIR/unsized_type2.rs:14:45

‎tests/ui/trait-bounds/ice-unsized-tuple-const-issue-121443.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ LL | const TEST: Fn = some_fn;
1111
| ^^ doesn't have a size known at compile-time
1212
|
1313
= help: the trait `Sized` is not implemented for `(dyn FnOnce() -> u8 + 'static)`
14+
= note: statics and constants must have a statically known size
1415

1516
error[E0277]: the size for values of type `(dyn FnOnce() -> u8 + 'static)` cannot be known at compilation time
1617
--> $DIR/ice-unsized-tuple-const-issue-121443.rs:11:14

0 commit comments

Comments
 (0)
Failed to load comments.