Skip to content

Commit 0f7c74d

Browse files
authored
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 try-job: test-various
2 parents 868db44 + 0e9a6cc commit 0f7c74d

29 files changed

+166
-67
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -743,14 +743,10 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
743743
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
744744
match tcx.def_kind(def_id) {
745745
DefKind::Static { .. } => {
746-
tcx.ensure_ok().typeck(def_id);
747-
maybe_check_static_with_link_section(tcx, def_id);
748746
check_static_inhabited(tcx, def_id);
749747
check_static_linkage(tcx, def_id);
750748
}
751-
DefKind::Const => {
752-
tcx.ensure_ok().typeck(def_id);
753-
}
749+
DefKind::Const => {}
754750
DefKind::Enum => {
755751
check_enum(tcx, def_id);
756752
}
@@ -764,7 +760,6 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
764760
ExternAbi::Rust,
765761
)
766762
}
767-
// Everything else is checked entirely within check_item_body
768763
}
769764
DefKind::Impl { of_trait } => {
770765
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {

compiler/rustc_hir_analysis/src/check/mod.rs

+1-1
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

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

+5-5
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

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

+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
@@ -3123,8 +3123,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
31233123
Applicability::MachineApplicable,
31243124
);
31253125
}
3126-
ObligationCauseCode::ConstSized => {
3127-
err.note("constant expressions must have a statically known size");
3126+
ObligationCauseCode::SizedConstOrStatic => {
3127+
err.note("statics and constants must have a statically known size");
31283128
}
31293129
ObligationCauseCode::InlineAsmSized => {
31303130
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/const-generics/generic_const_exprs/unevaluated-const-ice-119731.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ warning: type `v17` should have an upper camel case name
5454
LL | pub struct v17<const v10: usize, const v7: v11> {
5555
| ^^^ help: convert the identifier to upper camel case (notice the capitalization): `V17`
5656

57-
error[E0425]: cannot find function `v6` in this scope
58-
--> $DIR/unevaluated-const-ice-119731.rs:13:35
59-
|
60-
LL | const v0: [[usize; v4]; v4] = v6(v8);
61-
| ^^ not found in this scope
62-
6357
error: `[[usize; v4]; v4]` is forbidden as the type of a const generic parameter
6458
--> $DIR/unevaluated-const-ice-119731.rs:16:48
6559
|
@@ -72,6 +66,12 @@ help: add `#![feature(adt_const_params)]` to the crate attributes to enable more
7266
LL + #![feature(adt_const_params)]
7367
|
7468

69+
error[E0425]: cannot find function `v6` in this scope
70+
--> $DIR/unevaluated-const-ice-119731.rs:13:35
71+
|
72+
LL | const v0: [[usize; v4]; v4] = v6(v8);
73+
| ^^ not found in this scope
74+
7575
error: maximum number of nodes exceeded in constant v20::v17::<v10, v2>::{constant#0}
7676
--> $DIR/unevaluated-const-ice-119731.rs:28:37
7777
|

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/consts/issue-39974.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-39974.rs:5:19
3+
|
4+
LL | f: [[f64; 2]; LENGTH],
5+
| ^^^^^^ expected `usize`, found `f64`
6+
17
error[E0308]: mismatched types
28
--> $DIR/issue-39974.rs:1:21
39
|
@@ -9,12 +15,6 @@ help: use a float literal
915
LL | const LENGTH: f64 = 2.0;
1016
| ++
1117

12-
error[E0308]: mismatched types
13-
--> $DIR/issue-39974.rs:5:19
14-
|
15-
LL | f: [[f64; 2]; LENGTH],
16-
| ^^^^^^ expected `usize`, found `f64`
17-
1818
error: aborting due to 2 previous errors
1919

2020
For more information about this error, try `rustc --explain E0308`.

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/layout/issue-84108.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ LL | const BAR: (&Path, [u8], usize) = ("hello", [], 42);
2929
= help: the trait `Sized` is not implemented for `[u8]`
3030
= note: only the last element of a tuple may have a dynamically sized type
3131

32+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
33+
--> $DIR/issue-84108.rs:15:13
34+
|
35+
LL | static BAZ: ([u8], usize) = ([], 0);
36+
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
37+
|
38+
= help: the trait `Sized` is not implemented for `[u8]`
39+
= note: only the last element of a tuple may have a dynamically sized type
40+
3241
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
3342
--> $DIR/issue-84108.rs:9:12
3443
|
@@ -48,15 +57,6 @@ LL | const BAR: (&Path, [u8], usize) = ("hello", [], 42);
4857
= note: expected slice `[u8]`
4958
found array `[_; 0]`
5059

51-
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
52-
--> $DIR/issue-84108.rs:15:13
53-
|
54-
LL | static BAZ: ([u8], usize) = ([], 0);
55-
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
56-
|
57-
= help: the trait `Sized` is not implemented for `[u8]`
58-
= note: only the last element of a tuple may have a dynamically sized type
59-
6060
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
6161
--> $DIR/issue-84108.rs:15:13
6262
|

0 commit comments

Comments
 (0)