Skip to content

Commit fe7feec

Browse files
authored
Unrolled build for #149433
Rollup merge of #149433 - scottmcm:delay-layout-ICEs, r=jdonszelmann Use a delayed bug for this layout ICE Fixes #144501 cc `@matthiaskrgr`
2 parents a463b0e + 673be1b commit fe7feec

File tree

5 files changed

+118
-5
lines changed

5 files changed

+118
-5
lines changed

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ impl<'a> DiagCtxtHandle<'a> {
13661366
self.create_err(err).emit()
13671367
}
13681368

1369-
/// Ensures that an error is printed. See `Level::DelayedBug`.
1369+
/// Ensures that an error is printed. See [`Level::DelayedBug`].
13701370
//
13711371
// No `#[rustc_lint_diagnostics]` and no `impl Into<DiagMessage>` because bug messages aren't
13721372
// user-facing.

compiler/rustc_ty_utils/src/layout/invariant.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,16 @@ pub(super) fn layout_sanity_check<'tcx>(cx: &LayoutCx<'tcx>, layout: &TyAndLayou
281281
}
282282

283283
// Ensure that for niche encoded tags the discriminant coincides with the variant index.
284-
assert_eq!(
285-
layout.ty.discriminant_for_variant(tcx, idx).unwrap().val,
286-
u128::from(idx.as_u32()),
287-
);
284+
let val = layout.ty.discriminant_for_variant(tcx, idx).unwrap().val;
285+
if val != u128::from(idx.as_u32()) {
286+
let adt_def = layout.ty.ty_adt_def().unwrap();
287+
cx.tcx().dcx().span_delayed_bug(
288+
cx.tcx().def_span(adt_def.did()),
289+
format!(
290+
"variant {idx:?} has discriminant {val:?} in niche-encoded type"
291+
),
292+
);
293+
}
288294
}
289295
}
290296
for variant in variants.iter() {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0732]: `#[repr(inttype)]` must be specified for enums with explicit discriminants and non-unit variants
2+
--> $DIR/invalid-niche-discriminant.rs:11:1
3+
|
4+
LL | enum E {
5+
| ^^^^^^
6+
...
7+
LL | S0 {
8+
| -- non-unit discriminant declared here
9+
...
10+
LL | Bar = {
11+
| ___________-
12+
LL | | let x = 1;
13+
LL | | 3
14+
LL | | },
15+
| |_____- explicit discriminant specified here
16+
17+
error[E0599]: no variant named `S1` found for enum `E`
18+
--> $DIR/invalid-niche-discriminant.rs:23:18
19+
|
20+
LL | enum E {
21+
| ------ variant `S1` not found here
22+
...
23+
LL | static C: E = E::S1 { u: 23 };
24+
| ^^
25+
|
26+
help: there is a variant with a similar name
27+
|
28+
LL - static C: E = E::S1 { u: 23 };
29+
LL + static C: E = E::S0 { u: 23 };
30+
|
31+
32+
error: aborting due to 2 previous errors
33+
34+
Some errors have detailed explanations: E0599, E0732.
35+
For more information about an error, try `rustc --explain E0599`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//@ needs-rustc-debug-assertions
2+
//@ revisions: normal with_delayed
3+
//@ [with_delayed] compile-flags: -Z eagerly-emit-delayed-bugs
4+
5+
#![crate_type = "lib"]
6+
7+
// Repro for <https://github.com/rust-lang/rust/issues/144501>
8+
// which ICEd because the calculated layout is invalid
9+
// but which we needn't care about as the discriminant already was.
10+
11+
enum E {
12+
//~^ ERROR must be specified
13+
//[with_delayed]~| ERROR variant 1 has discriminant 3
14+
S0 {
15+
s: String,
16+
},
17+
Bar = {
18+
let x = 1;
19+
3
20+
},
21+
}
22+
23+
static C: E = E::S1 { u: 23 };
24+
//~^ ERROR no variant named
25+
//[with_delayed]~| ERROR but no error emitted
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error[E0732]: `#[repr(inttype)]` must be specified for enums with explicit discriminants and non-unit variants
2+
--> $DIR/invalid-niche-discriminant.rs:11:1
3+
|
4+
LL | enum E {
5+
| ^^^^^^
6+
...
7+
LL | S0 {
8+
| -- non-unit discriminant declared here
9+
...
10+
LL | Bar = {
11+
| ___________-
12+
LL | | let x = 1;
13+
LL | | 3
14+
LL | | },
15+
| |_____- explicit discriminant specified here
16+
17+
error: variant 1 has discriminant 3 in niche-encoded type
18+
--> $DIR/invalid-niche-discriminant.rs:11:1
19+
|
20+
LL | enum E {
21+
| ^^^^^^
22+
23+
error[E0599]: no variant named `S1` found for enum `E`
24+
--> $DIR/invalid-niche-discriminant.rs:23:18
25+
|
26+
LL | enum E {
27+
| ------ variant `S1` not found here
28+
...
29+
LL | static C: E = E::S1 { u: 23 };
30+
| ^^
31+
|
32+
help: there is a variant with a similar name
33+
|
34+
LL - static C: E = E::S1 { u: 23 };
35+
LL + static C: E = E::S0 { u: 23 };
36+
|
37+
38+
error: `Res::Err` but no error emitted
39+
--> $DIR/invalid-niche-discriminant.rs:23:15
40+
|
41+
LL | static C: E = E::S1 { u: 23 };
42+
| ^^^^^
43+
44+
error: aborting due to 4 previous errors
45+
46+
Some errors have detailed explanations: E0599, E0732.
47+
For more information about an error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)