Skip to content

Commit 5df9a9f

Browse files
authored
Rollup merge of #137298 - compiler-errors:mir-wf, r=lcnr
Check signature WF when lowering MIR body Alternative to #137233. #137233 (comment) Fixes #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 4f1a047 + 0baee24 commit 5df9a9f

33 files changed

+174
-74
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -745,14 +745,10 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
745745
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
746746
match tcx.def_kind(def_id) {
747747
DefKind::Static { .. } => {
748-
tcx.ensure_ok().typeck(def_id);
749-
maybe_check_static_with_link_section(tcx, def_id);
750748
check_static_inhabited(tcx, def_id);
751749
check_static_linkage(tcx, def_id);
752750
}
753-
DefKind::Const => {
754-
tcx.ensure_ok().typeck(def_id);
755-
}
751+
DefKind::Const => {}
756752
DefKind::Enum => {
757753
check_enum(tcx, def_id);
758754
}
@@ -766,7 +762,6 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
766762
ExternAbi::Rust,
767763
)
768764
}
769-
// Everything else is checked entirely within check_item_body
770765
}
771766
DefKind::Impl { of_trait } => {
772767
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {

compiler/rustc_hir_analysis/src/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub fn forbid_intrinsic_abi(tcx: TyCtxt<'_>, sp: Span, abi: ExternAbi) {
145145
}
146146
}
147147

148-
fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId) {
148+
pub(super) fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId) {
149149
// Only restricted on wasm target for now
150150
if !tcx.sess.target.is_like_wasm {
151151
return;

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_analysis/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,10 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
212212
tcx.par_hir_body_owners(|item_def_id| {
213213
let def_kind = tcx.def_kind(item_def_id);
214214
match def_kind {
215-
DefKind::Static { .. } => tcx.ensure_ok().eval_static_initializer(item_def_id),
215+
DefKind::Static { .. } => {
216+
tcx.ensure_ok().eval_static_initializer(item_def_id);
217+
check::maybe_check_static_with_link_section(tcx, item_def_id);
218+
}
216219
DefKind::Const if tcx.generics_of(item_def_id).is_empty() => {
217220
let instance = ty::Instance::new(item_def_id.into(), ty::GenericArgs::empty());
218221
let cid = GlobalId { instance, promoted: None };
@@ -223,12 +226,9 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
223226
}
224227
});
225228

226-
// FIXME: Remove this when we implement creating `DefId`s
227-
// for anon constants during their parents' typeck.
228-
// Typeck all body owners in parallel will produce queries
229-
// cycle errors because it may typeck on anon constants directly.
230229
tcx.par_hir_body_owners(|item_def_id| {
231230
let def_kind = tcx.def_kind(item_def_id);
231+
// Skip `AnonConst`s because we feed their `type_of`.
232232
if !matches!(def_kind, DefKind::AnonConst) {
233233
tcx.ensure_ok().typeck(item_def_id);
234234
}

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1806,7 +1806,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18061806
crate::GatherLocalsVisitor::new(&fcx).visit_body(body);
18071807

18081808
let ty = fcx.check_expr_with_expectation(body.value, expected);
1809-
fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::ConstSized);
1809+
fcx.require_type_is_sized(ty, body.value.span, ObligationCauseCode::SizedConstOrStatic);
18101810
fcx.write_ty(block.hir_id, ty);
18111811
ty
18121812
}

compiler/rustc_middle/src/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ pub enum ObligationCauseCode<'tcx> {
266266
},
267267

268268
/// Constant expressions must be sized.
269-
ConstSized,
269+
SizedConstOrStatic,
270270

271271
/// `static` items must have `Sync` type.
272272
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
@@ -3126,8 +3126,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
31263126
Applicability::MachineApplicable,
31273127
);
31283128
}
3129-
ObligationCauseCode::ConstSized => {
3130-
err.note("constant expressions must have a statically known size");
3129+
ObligationCauseCode::SizedConstOrStatic => {
3130+
err.note("statics and constants must have a statically known size");
31313131
}
31323132
ObligationCauseCode::InlineAsmSized => {
31333133
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

0 commit comments

Comments
 (0)