Skip to content

Commit 1da48a3

Browse files
authored
Unrolled build for #144226
Rollup merge of #144226 - cjgillot:known-panics-panics, r=oli-obk Do not assert layout in KnownPanicsLint. Fixes #121176 Fixes #129109 Fixes #130970 Fixes #131347 Fixes #139872 Fixes #140332
2 parents 86ef320 + 8817572 commit 1da48a3

31 files changed

+159
-229
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,8 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
767767
DefKind::Static { .. } => {
768768
check_static_inhabited(tcx, def_id);
769769
check_static_linkage(tcx, def_id);
770-
res = res.and(wfcheck::check_static_item(tcx, def_id));
770+
let ty = tcx.type_of(def_id).instantiate_identity();
771+
res = res.and(wfcheck::check_static_item(tcx, def_id, ty, true));
771772
}
772773
DefKind::Const => res = res.and(wfcheck::check_const_item(tcx, def_id)),
773774
_ => unreachable!(),

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,12 +1180,13 @@ fn check_item_fn(
11801180
}
11811181

11821182
#[instrument(level = "debug", skip(tcx))]
1183-
pub(super) fn check_static_item(
1184-
tcx: TyCtxt<'_>,
1183+
pub(crate) fn check_static_item<'tcx>(
1184+
tcx: TyCtxt<'tcx>,
11851185
item_id: LocalDefId,
1186+
ty: Ty<'tcx>,
1187+
should_check_for_sync: bool,
11861188
) -> Result<(), ErrorGuaranteed> {
11871189
enter_wf_checking_ctxt(tcx, item_id, |wfcx| {
1188-
let ty = tcx.type_of(item_id).instantiate_identity();
11891190
let span = tcx.ty_span(item_id);
11901191
let item_ty = wfcx.deeply_normalize(span, Some(WellFormedLoc::Ty(item_id)), ty);
11911192

@@ -1212,9 +1213,9 @@ pub(super) fn check_static_item(
12121213
}
12131214

12141215
// Ensure that the end result is `Sync` in a non-thread local `static`.
1215-
let should_check_for_sync = tcx.static_mutability(item_id.to_def_id())
1216-
== Some(hir::Mutability::Not)
1216+
let should_check_for_sync = should_check_for_sync
12171217
&& !is_foreign_item
1218+
&& tcx.static_mutability(item_id.to_def_id()) == Some(hir::Mutability::Not)
12181219
&& !tcx.is_thread_local_static(item_id.to_def_id());
12191220

12201221
if should_check_for_sync {

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_middle::{bug, span_bug};
1414
use rustc_span::{DUMMY_SP, Ident, Span};
1515

1616
use super::{HirPlaceholderCollector, ItemCtxt, bad_placeholder};
17+
use crate::check::wfcheck::check_static_item;
1718
use crate::errors::TypeofReservedKeywordUsed;
1819
use crate::hir_ty_lowering::HirTyLowerer;
1920

@@ -217,7 +218,13 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
217218
"static variable",
218219
)
219220
} else {
220-
icx.lower_ty(ty)
221+
let ty = icx.lower_ty(ty);
222+
// MIR relies on references to statics being scalars.
223+
// Verify that here to avoid ill-formed MIR.
224+
match check_static_item(tcx, def_id, ty, false) {
225+
Ok(()) => ty,
226+
Err(guar) => Ty::new_error(tcx, guar),
227+
}
221228
}
222229
}
223230
ItemKind::Const(ident, _, ty, body_id) => {
@@ -275,7 +282,15 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
275282
let args = ty::GenericArgs::identity_for_item(tcx, def_id);
276283
Ty::new_fn_def(tcx, def_id.to_def_id(), args)
277284
}
278-
ForeignItemKind::Static(t, _, _) => icx.lower_ty(t),
285+
ForeignItemKind::Static(ty, _, _) => {
286+
let ty = icx.lower_ty(ty);
287+
// MIR relies on references to statics being scalars.
288+
// Verify that here to avoid ill-formed MIR.
289+
match check_static_item(tcx, def_id, ty, false) {
290+
Ok(()) => ty,
291+
Err(guar) => Ty::new_error(tcx, guar),
292+
}
293+
}
279294
ForeignItemKind::Type => Ty::new_foreign(tcx, def_id.to_def_id()),
280295
},
281296

tests/crashes/121176.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/crashes/129109.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/crashes/130970.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/crashes/131347.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/ui/consts/const-unsized.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ const CONST_FOO: str = *"foo";
1010

1111
static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
1212
//~^ ERROR the size for values of type
13-
//~| ERROR cannot move out of a shared reference
1413

1514
static STATIC_BAR: str = *"bar";
1615
//~^ ERROR the size for values of type
17-
//~| ERROR cannot move out of a shared reference
1816

1917
fn main() {
2018
println!("{:?} {:?} {:?} {:?}", &CONST_0, &CONST_FOO, &STATIC_1, &STATIC_BAR);
21-
//~^ ERROR: cannot move a value of type `str`
22-
//~| ERROR: cannot move a value of type `dyn Debug + Sync`
2319
}

tests/ui/consts/const-unsized.stderr

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
2626
= note: statics and constants must have a statically known size
2727

2828
error[E0277]: the size for values of type `str` cannot be known at compilation time
29-
--> $DIR/const-unsized.rs:15:1
29+
--> $DIR/const-unsized.rs:14:1
3030
|
3131
LL | static STATIC_BAR: str = *"bar";
3232
| ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
@@ -46,31 +46,7 @@ error[E0507]: cannot move out of a shared reference
4646
LL | const CONST_FOO: str = *"foo";
4747
| ^^^^^^ move occurs because value has type `str`, which does not implement the `Copy` trait
4848

49-
error[E0507]: cannot move out of a shared reference
50-
--> $DIR/const-unsized.rs:11:37
51-
|
52-
LL | static STATIC_1: dyn Debug + Sync = *(&1 as &(dyn Debug + Sync));
53-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ move occurs because value has type `dyn Debug + Sync`, which does not implement the `Copy` trait
54-
55-
error[E0507]: cannot move out of a shared reference
56-
--> $DIR/const-unsized.rs:15:26
57-
|
58-
LL | static STATIC_BAR: str = *"bar";
59-
| ^^^^^^ move occurs because value has type `str`, which does not implement the `Copy` trait
60-
61-
error[E0161]: cannot move a value of type `dyn Debug + Sync`
62-
--> $DIR/const-unsized.rs:20:38
63-
|
64-
LL | println!("{:?} {:?} {:?} {:?}", &CONST_0, &CONST_FOO, &STATIC_1, &STATIC_BAR);
65-
| ^^^^^^^ the size of `dyn Debug + Sync` cannot be statically determined
66-
67-
error[E0161]: cannot move a value of type `str`
68-
--> $DIR/const-unsized.rs:20:48
69-
|
70-
LL | println!("{:?} {:?} {:?} {:?}", &CONST_0, &CONST_FOO, &STATIC_1, &STATIC_BAR);
71-
| ^^^^^^^^^ the size of `str` cannot be statically determined
72-
73-
error: aborting due to 10 previous errors
49+
error: aborting due to 6 previous errors
7450

75-
Some errors have detailed explanations: E0161, E0277, E0507.
76-
For more information about an error, try `rustc --explain E0161`.
51+
Some errors have detailed explanations: E0277, E0507.
52+
For more information about an error, try `rustc --explain E0277`.

tests/ui/coroutine/layout-error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl<F: Future> Task<F> {
1717
}
1818

1919
pub type F = impl Future;
20+
2021
#[define_opaque(F)]
2122
fn foo()
2223
where

0 commit comments

Comments
 (0)