Skip to content

Commit 734de5c

Browse files
authored
Rollup merge of #157562 - sjwang05:fix-138660-enum-discr-layout, r=Kivooeo
Avoid computing layout of enums with non-int discriminants Currently, enums with explicit non-int discriminants, such as `1..=10`, correctly error with E0308 and E0732 during typeck. However, `layout_of` never sees this error, since `AdtDef::discriminants()` falls back to a default value when `eval_explicit_discr` returns an `Err`, causing the layout of the enum to be broken. If this enum then appears in a promoted, we expect CTFE to fail; however, since `layout_of` technically "succeeded," just with a broken layout, our enum isn't in `allowed_in_infallible`, so CTFE expects it to succeed. CTFE failing then causes us to ICE with `"interpret const eval failure of...which is not in required_consts"`. This PR modifies `layout_of_uncached` in `compiler/rustc_ty_utils/src/layout.rs` to typeck all explicit enum discriminants before computing layout, and early-returns with a `LayoutError::ReferncesError` if eval fails. CTFE then sees this `LayoutError` as `allowed_in_infallible`, avoiding the ICE. fixes #138660
2 parents 784a449 + 88d314a commit 734de5c

5 files changed

Lines changed: 66 additions & 7 deletions

File tree

‎compiler/rustc_ty_utils/src/layout.rs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,18 @@ fn layout_of_uncached<'tcx>(
719719
abi::Integer::discr_range_of_repr(tcx, ty, &def.repr(), min.start, max.last)
720720
};
721721

722+
// We shouldn't be computing the layout of discrs that fail typeck
723+
if def.is_enum() {
724+
for v in def.variants() {
725+
if let ty::VariantDiscr::Explicit(def_id) = v.discr
726+
&& let Some(local_did) = def_id.as_local()
727+
&& let Some(guar) = tcx.typeck(local_did).tainted_by_errors
728+
{
729+
return Err(error(cx, LayoutError::ReferencesError(guar)));
730+
}
731+
}
732+
}
733+
722734
let discriminants_iter = || {
723735
def.is_enum()
724736
.then(|| def.discriminants(tcx).map(|(v, d)| (v, d.val)))

‎tests/crashes/138660.rs‎

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Previously, enums with non-int discrs ICEd during CTFE of promoteds.
2+
3+
enum A {
4+
//~^ ERROR `#[repr(inttype)]` must be specified
5+
V1(isize) = 1..=10,
6+
//~^ ERROR mismatched types
7+
V0 = 1..=10,
8+
//~^ ERROR mismatched types
9+
}
10+
11+
const B: &'static [A] = &[A::V0, A::V1(111)];
12+
13+
fn main() {}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/non-int-discriminant-promoted-ice-138660.rs:5:17
3+
|
4+
LL | V1(isize) = 1..=10,
5+
| ^^^^^^ expected `isize`, found `RangeInclusive<{integer}>`
6+
|
7+
= note: expected type `isize`
8+
found struct `std::ops::RangeInclusive<{integer}>`
9+
= note: enum variant discriminant can only be of a primitive type compatible with the enum's `repr`
10+
11+
error[E0308]: mismatched types
12+
--> $DIR/non-int-discriminant-promoted-ice-138660.rs:7:10
13+
|
14+
LL | V0 = 1..=10,
15+
| ^^^^^^ expected `isize`, found `RangeInclusive<{integer}>`
16+
|
17+
= note: expected type `isize`
18+
found struct `std::ops::RangeInclusive<{integer}>`
19+
= note: enum variant discriminant can only be of a primitive type compatible with the enum's `repr`
20+
21+
error[E0732]: `#[repr(inttype)]` must be specified for enums with explicit discriminants and non-unit variants
22+
--> $DIR/non-int-discriminant-promoted-ice-138660.rs:3:1
23+
|
24+
LL | enum A {
25+
| ^^^^^^
26+
LL |
27+
LL | V1(isize) = 1..=10,
28+
| ------ explicit discriminant on non-unit variant specified here
29+
30+
error: aborting due to 3 previous errors
31+
32+
Some errors have detailed explanations: E0308, E0732.
33+
For more information about an error, try `rustc --explain E0308`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ check-pass
2+
// Ensure that `size_of::<Thing>()` as a discriminant does not cause a cycle error.
3+
4+
enum Thing {
5+
Variant = size_of::<Thing>() as isize,
6+
}
7+
8+
fn main() {}

0 commit comments

Comments
 (0)