Skip to content

Commit 1f34db0

Browse files
compiler: Do not ICE on impossible reprs
Invalid reprs can reach layout computation for various reasons. Do not force these to be handled immediately, but delay the bug.
1 parent dd51276 commit 1f34db0

File tree

6 files changed

+115
-6
lines changed

6 files changed

+115
-6
lines changed

compiler/rustc_abi/src/layout.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ pub enum LayoutCalculatorError<F> {
5454

5555
/// A union had no fields.
5656
EmptyUnion,
57+
58+
/// The fields or variants have irreconcilable reprs
59+
ReprConflict,
5760
}
5861

5962
type LayoutCalculatorResult<FieldIdx, VariantIdx, F> =
@@ -307,12 +310,11 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
307310
common_non_zst_abi_and_align = Err(AbiMismatch);
308311
} else {
309312
// Fields with the same non-Aggregate ABI should also
310-
// have the same alignment
311-
if !matches!(common_abi, Abi::Aggregate { .. }) {
312-
assert_eq!(
313-
common_align, field.align.abi,
314-
"non-Aggregate field with matching ABI but differing alignment"
315-
);
313+
// have the same alignment, but one may have an invalid repr
314+
if !matches!(common_abi, Abi::Aggregate { .. })
315+
&& common_align != field.align.abi
316+
{
317+
return Err(LayoutCalculatorError::ReprConflict);
316318
}
317319
}
318320
} else {

compiler/rustc_ty_utils/src/layout.rs

+5
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ fn map_error<'tcx>(
115115
cx.tcx().dcx().delayed_bug(format!("computed layout of empty union: {ty:?}"));
116116
LayoutError::Unknown(ty)
117117
}
118+
LayoutCalculatorError::ReprConflict => {
119+
// packed enums are the only known trigger of this, but others might arise
120+
cx.tcx().dcx().delayed_bug(format!("computed impossible repr (packed enum): {ty:?}"));
121+
LayoutError::Unknown(ty)
122+
}
118123
};
119124
error(cx, err)
120125
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
error[E0412]: cannot find type `Subset` in this scope
2+
--> $DIR/thaw-transmute-invalid-enum.rs:28:41
3+
|
4+
LL | assert::is_transmutable::<Superset, Subset>();
5+
| ^^^^^^ not found in this scope
6+
|
7+
help: you might be missing a type parameter
8+
|
9+
LL | fn test<Subset>() {
10+
| ++++++++
11+
12+
error[E0601]: `main` function not found in crate `thaw_transmute_invalid_enum`
13+
--> $DIR/thaw-transmute-invalid-enum.rs:29:2
14+
|
15+
LL | }
16+
| ^ consider adding a `main` function to `$DIR/thaw-transmute-invalid-enum.rs`
17+
18+
error[E0517]: attribute should be applied to a struct or union
19+
--> $DIR/thaw-transmute-invalid-enum.rs:17:11
20+
|
21+
LL | #[repr(C, packed(2))]
22+
| ^^^^^^^^^
23+
LL | / enum OxFF {
24+
LL | | V = 0xFF,
25+
LL | | }
26+
| |_- not a struct or union
27+
28+
error[E0658]: use of unstable library feature 'transmutability'
29+
--> $DIR/thaw-transmute-invalid-enum.rs:3:20
30+
|
31+
LL | use std::mem::{Assume, TransmuteFrom};
32+
| ^^^^^^
33+
|
34+
= note: see issue #99571 <https://github.com/rust-lang/rust/issues/99571> for more information
35+
= help: add `#![feature(transmutability)]` to the crate attributes to enable
36+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
37+
38+
error[E0658]: use of unstable library feature 'transmutability'
39+
--> $DIR/thaw-transmute-invalid-enum.rs:3:28
40+
|
41+
LL | use std::mem::{Assume, TransmuteFrom};
42+
| ^^^^^^^^^^^^^
43+
|
44+
= note: see issue #99571 <https://github.com/rust-lang/rust/issues/99571> for more information
45+
= help: add `#![feature(transmutability)]` to the crate attributes to enable
46+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
47+
48+
error[E0658]: use of unstable library feature 'transmutability'
49+
--> $DIR/thaw-transmute-invalid-enum.rs:7:14
50+
|
51+
LL | Dst: TransmuteFrom<Src>,
52+
| ^^^^^^^^^^^^^^^^^^
53+
|
54+
= note: see issue #99571 <https://github.com/rust-lang/rust/issues/99571> for more information
55+
= help: add `#![feature(transmutability)]` to the crate attributes to enable
56+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
57+
58+
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
59+
--> $DIR/thaw-transmute-invalid-enum.rs:24:9
60+
|
61+
LL | a: Ox00,
62+
| ^^^^^^^
63+
|
64+
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
65+
help: wrap the field type in `ManuallyDrop<...>`
66+
|
67+
LL | a: std::mem::ManuallyDrop<Ox00>,
68+
| +++++++++++++++++++++++ +
69+
70+
error: aborting due to 7 previous errors
71+
72+
Some errors have detailed explanations: E0412, E0517, E0601, E0658, E0740.
73+
For more information about an error, try `rustc --explain E0412`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0517]: attribute should be applied to a struct or union
2+
--> $DIR/thaw-validate-invalid-enum.rs:4:8
3+
|
4+
LL | #[repr(packed)]
5+
| ^^^^^^
6+
LL | #[repr(u32)]
7+
LL | / enum E {
8+
LL | | A,
9+
LL | | B,
10+
LL | | C,
11+
LL | | }
12+
| |_- not a struct or union
13+
14+
error[E0740]: field must implement `Copy` or be wrapped in `ManuallyDrop<...>` to be used in a union
15+
--> $DIR/thaw-validate-invalid-enum.rs:15:9
16+
|
17+
LL | e: E,
18+
| ^^^^
19+
|
20+
= note: union fields must not have drop side-effects, which is currently enforced via either `Copy` or `ManuallyDrop<...>`
21+
help: wrap the field type in `ManuallyDrop<...>`
22+
|
23+
LL | e: std::mem::ManuallyDrop<E>,
24+
| +++++++++++++++++++++++ +
25+
26+
error: aborting due to 2 previous errors
27+
28+
Some errors have detailed explanations: E0517, E0740.
29+
For more information about an error, try `rustc --explain E0517`.

0 commit comments

Comments
 (0)