Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a6414b1

Browse files
Rollup merge of rust-lang#137298 - compiler-errors:mir-wf, r=lcnr
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
2 parents 32330eb + bb7b912 commit a6414b1

File tree

20 files changed

+86
-10
lines changed

20 files changed

+86
-10
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 12 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 18 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 4 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

0 commit comments

Comments
 (0)