Skip to content

Commit 53ae5c6

Browse files
committed
Do not assert layout in KnownPanicsLint.
1 parent 0cb4b0d commit 53ae5c6

File tree

12 files changed

+146
-31
lines changed

12 files changed

+146
-31
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,17 @@ impl<'tcx, Prov: Provenance> std::ops::Deref for ImmTy<'tcx, Prov> {
243243
}
244244

245245
impl<'tcx, Prov: Provenance> ImmTy<'tcx, Prov> {
246+
#[inline(always)]
247+
pub fn try_from_immediate(imm: Immediate<Prov>, layout: TyAndLayout<'tcx>) -> Option<Self> {
248+
let matches_abi = match (imm, layout.backend_repr) {
249+
(Immediate::Scalar(..), BackendRepr::Scalar(..)) => true,
250+
(Immediate::ScalarPair(..), BackendRepr::ScalarPair(..)) => true,
251+
(Immediate::Uninit, _) => layout.is_sized(),
252+
_ => false,
253+
};
254+
if matches_abi { Some(ImmTy { imm, layout }) } else { None }
255+
}
256+
246257
#[inline]
247258
pub fn from_scalar(val: Scalar<Prov>, layout: TyAndLayout<'tcx>) -> Self {
248259
debug_assert!(layout.backend_repr.is_scalar(), "`ImmTy::from_scalar` on non-scalar layout");

compiler/rustc_const_eval/src/interpret/place.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,14 @@ impl<'tcx, Prov: Provenance> Projectable<'tcx, Prov> for PlaceTy<'tcx, Prov> {
313313

314314
// These are defined here because they produce a place.
315315
impl<'tcx, Prov: Provenance> OpTy<'tcx, Prov> {
316+
#[inline(always)]
317+
pub fn try_as_immediate(&self) -> Option<ImmTy<'tcx, Prov>> {
318+
match self.op() {
319+
Operand::Indirect(_) => None,
320+
Operand::Immediate(imm) => ImmTy::try_from_immediate(*imm, self.layout),
321+
}
322+
}
323+
316324
#[inline(always)]
317325
pub fn as_mplace_or_imm(&self) -> Either<MPlaceTy<'tcx, Prov>, ImmTy<'tcx, Prov>> {
318326
match self.op() {

compiler/rustc_mir_transform/src/known_panics_lint.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
263263
// manually normalized.
264264
let val = self.tcx.try_normalize_erasing_regions(self.typing_env, c.const_).ok()?;
265265

266-
self.use_ecx(|this| this.ecx.eval_mir_constant(&val, c.span, None))?
267-
.as_mplace_or_imm()
268-
.right()
266+
self.use_ecx(|this| this.ecx.eval_mir_constant(&val, c.span, None))?.try_as_immediate()
269267
}
270268

271269
/// Returns the value, if any, of evaluating `place`.

tests/crashes/121176.rs

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

tests/crashes/139872.rs

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

tests/crashes/140332.rs

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

tests/ui/mir/static-by-value-dyn.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Regression test for #121176
2+
//! KnownPanicsLint used to assert ABI compatibility in the interpreter,
3+
//! which ICEs with unsized statics.
4+
//@ needs-rustc-debug-assertions
5+
6+
use std::fmt::Debug;
7+
8+
static STATIC_1: dyn Debug + Sync = *();
9+
//~^ ERROR the size for values of type `(dyn Debug + Sync + 'static)` cannot be known
10+
//~| ERROR type `()` cannot be dereferenced
11+
12+
fn main() {
13+
println!("{:?}", &STATIC_1);
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0277]: the size for values of type `(dyn Debug + Sync + 'static)` cannot be known at compilation time
2+
--> $DIR/static-by-value-dyn.rs:8:1
3+
|
4+
LL | static STATIC_1: dyn Debug + Sync = *();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `(dyn Debug + Sync + 'static)`
8+
= note: statics and constants must have a statically known size
9+
10+
error[E0614]: type `()` cannot be dereferenced
11+
--> $DIR/static-by-value-dyn.rs:8:37
12+
|
13+
LL | static STATIC_1: dyn Debug + Sync = *();
14+
| ^^^ can't be dereferenced
15+
16+
error: aborting due to 2 previous errors
17+
18+
Some errors have detailed explanations: E0277, E0614.
19+
For more information about an error, try `rustc --explain E0277`.

tests/ui/mir/static-by-value-slice.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//! Regression test for #140332
2+
//! KnownPanicsLint used to assert ABI compatibility in the interpreter,
3+
//! which ICEs with unsized statics.
4+
5+
static mut S: [i8] = ["Some thing"; 1];
6+
//~^ ERROR the size for values of type `[i8]` cannot be known
7+
//~| ERROR mismatched types
8+
//~| ERROR mismatched types
9+
10+
fn main() {
11+
assert_eq!(S, [0; 1]);
12+
//~^ ERROR use of mutable static is unsafe
13+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0277]: the size for values of type `[i8]` cannot be known at compilation time
2+
--> $DIR/static-by-value-slice.rs:5:1
3+
|
4+
LL | static mut S: [i8] = ["Some thing"; 1];
5+
| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
6+
|
7+
= help: the trait `Sized` is not implemented for `[i8]`
8+
= note: statics and constants must have a statically known size
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/static-by-value-slice.rs:5:23
12+
|
13+
LL | static mut S: [i8] = ["Some thing"; 1];
14+
| ^^^^^^^^^^^^ expected `i8`, found `&str`
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/static-by-value-slice.rs:5:22
18+
|
19+
LL | static mut S: [i8] = ["Some thing"; 1];
20+
| ^^^^^^^^^^^^^^^^^ expected `[i8]`, found `[i8; 1]`
21+
22+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
23+
--> $DIR/static-by-value-slice.rs:11:16
24+
|
25+
LL | assert_eq!(S, [0; 1]);
26+
| ^ use of mutable static
27+
|
28+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
29+
30+
error: aborting due to 4 previous errors
31+
32+
Some errors have detailed explanations: E0133, E0277, E0308.
33+
For more information about an error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)