Skip to content

Commit 187f270

Browse files
committed
Do not ICE in the face of invalid enum discriminant
1 parent 94d3463 commit 187f270

File tree

4 files changed

+181
-5
lines changed

4 files changed

+181
-5
lines changed

src/librustc_middle/ty/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -2430,7 +2430,11 @@ impl<'tcx> AdtDef {
24302430
None
24312431
}
24322432
Err(ErrorHandled::TooGeneric) => {
2433-
span_bug!(tcx.def_span(expr_did), "enum discriminant depends on generic arguments",)
2433+
tcx.sess.delay_span_bug(
2434+
tcx.def_span(expr_did),
2435+
"enum discriminant depends on generic arguments",
2436+
);
2437+
None
24342438
}
24352439
}
24362440
}

src/librustc_mir_build/hair/cx/expr.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::ty::adjustment::{
1212
Adjust, Adjustment, AutoBorrow, AutoBorrowMutability, PointerCast,
1313
};
1414
use rustc_middle::ty::subst::{InternalSubsts, SubstsRef};
15-
use rustc_middle::ty::{self, AdtKind, Ty};
15+
use rustc_middle::ty::{self, AdtKind, Ty, TypeFoldable};
1616
use rustc_span::Span;
1717

1818
impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr<'tcx> {
@@ -718,8 +718,7 @@ fn convert_path_expr<'a, 'tcx>(
718718

719719
Res::Def(DefKind::Ctor(_, CtorKind::Const), def_id) => {
720720
let user_provided_types = cx.tables.user_provided_types();
721-
let user_provided_type = user_provided_types.get(expr.hir_id).copied();
722-
debug!("convert_path_expr: user_provided_type={:?}", user_provided_type);
721+
let user_ty = user_provided_types.get(expr.hir_id).copied();
723722
let ty = cx.tables().node_type(expr.hir_id);
724723
match ty.kind {
725724
// A unit struct/variant which is used as a value.
@@ -728,10 +727,17 @@ fn convert_path_expr<'a, 'tcx>(
728727
adt_def,
729728
variant_index: adt_def.variant_index_with_ctor_id(def_id),
730729
substs,
731-
user_ty: user_provided_type,
730+
user_ty,
732731
fields: vec![],
733732
base: None,
734733
},
734+
_ if ty.references_error() => {
735+
// Handle degenerate input without ICE (#67377).
736+
ExprKind::Literal {
737+
literal: ty::Const::zero_sized(cx.tcx, cx.tcx.types.err),
738+
user_ty: None,
739+
}
740+
}
735741
_ => bug!("unexpected ty: {:?}", ty),
736742
}
737743
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
mod a {
2+
use std::marker::PhantomData;
3+
4+
enum Bug {
5+
V = [PhantomData; { [ () ].len() ].len() as isize,
6+
//~^ ERROR mismatched closing delimiter: `]`
7+
//~| ERROR mismatched closing delimiter: `]`
8+
//~| ERROR mismatched closing delimiter: `]`
9+
//~| ERROR mismatched closing delimiter: `]`
10+
//~| ERROR destructors cannot be evaluated at compile-time
11+
}
12+
}
13+
14+
mod b {
15+
enum Bug {
16+
V = [Vec::new; { [].len() ].len() as isize,
17+
//~^ ERROR mismatched closing delimiter: `]`
18+
//~| ERROR mismatched closing delimiter: `]`
19+
//~| ERROR mismatched closing delimiter: `]`
20+
//~| ERROR mismatched closing delimiter: `]`
21+
//~| ERROR type annotations needed
22+
}
23+
}
24+
25+
mod c {
26+
enum Bug {
27+
V = [Vec::new; { [0].len() ].len() as isize,
28+
//~^ ERROR mismatched closing delimiter: `]`
29+
//~| ERROR mismatched closing delimiter: `]`
30+
//~| ERROR mismatched closing delimiter: `]`
31+
//~| ERROR mismatched closing delimiter: `]`
32+
//~| ERROR type annotations needed
33+
}
34+
}
35+
36+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
error: mismatched closing delimiter: `]`
2+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:42
3+
|
4+
LL | V = [PhantomData; { [ () ].len() ].len() as isize,
5+
| - - ^ mismatched closing delimiter
6+
| | |
7+
| | unclosed delimiter
8+
| closing delimiter possibly meant for this
9+
10+
error: mismatched closing delimiter: `]`
11+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:16:36
12+
|
13+
LL | V = [Vec::new; { [].len() ].len() as isize,
14+
| - - ^ mismatched closing delimiter
15+
| | |
16+
| | unclosed delimiter
17+
| closing delimiter possibly meant for this
18+
19+
error: mismatched closing delimiter: `]`
20+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:27:36
21+
|
22+
LL | V = [Vec::new; { [0].len() ].len() as isize,
23+
| - - ^ mismatched closing delimiter
24+
| | |
25+
| | unclosed delimiter
26+
| closing delimiter possibly meant for this
27+
28+
error: mismatched closing delimiter: `]`
29+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:42
30+
|
31+
LL | V = [PhantomData; { [ () ].len() ].len() as isize,
32+
| - - ^ mismatched closing delimiter
33+
| | |
34+
| | unclosed delimiter
35+
| closing delimiter possibly meant for this
36+
37+
error: mismatched closing delimiter: `]`
38+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:16:36
39+
|
40+
LL | V = [Vec::new; { [].len() ].len() as isize,
41+
| - - ^ mismatched closing delimiter
42+
| | |
43+
| | unclosed delimiter
44+
| closing delimiter possibly meant for this
45+
46+
error: mismatched closing delimiter: `]`
47+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:27:36
48+
|
49+
LL | V = [Vec::new; { [0].len() ].len() as isize,
50+
| - - ^ mismatched closing delimiter
51+
| | |
52+
| | unclosed delimiter
53+
| closing delimiter possibly meant for this
54+
55+
error: mismatched closing delimiter: `]`
56+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:42
57+
|
58+
LL | V = [PhantomData; { [ () ].len() ].len() as isize,
59+
| - - ^ mismatched closing delimiter
60+
| | |
61+
| | unclosed delimiter
62+
| closing delimiter possibly meant for this
63+
64+
error: mismatched closing delimiter: `]`
65+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:16:36
66+
|
67+
LL | V = [Vec::new; { [].len() ].len() as isize,
68+
| - - ^ mismatched closing delimiter
69+
| | |
70+
| | unclosed delimiter
71+
| closing delimiter possibly meant for this
72+
73+
error: mismatched closing delimiter: `]`
74+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:27:36
75+
|
76+
LL | V = [Vec::new; { [0].len() ].len() as isize,
77+
| - - ^ mismatched closing delimiter
78+
| | |
79+
| | unclosed delimiter
80+
| closing delimiter possibly meant for this
81+
82+
error: mismatched closing delimiter: `]`
83+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:42
84+
|
85+
LL | V = [PhantomData; { [ () ].len() ].len() as isize,
86+
| - - ^ mismatched closing delimiter
87+
| | |
88+
| | unclosed delimiter
89+
| closing delimiter possibly meant for this
90+
91+
error: mismatched closing delimiter: `]`
92+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:16:36
93+
|
94+
LL | V = [Vec::new; { [].len() ].len() as isize,
95+
| - - ^ mismatched closing delimiter
96+
| | |
97+
| | unclosed delimiter
98+
| closing delimiter possibly meant for this
99+
100+
error: mismatched closing delimiter: `]`
101+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:27:36
102+
|
103+
LL | V = [Vec::new; { [0].len() ].len() as isize,
104+
| - - ^ mismatched closing delimiter
105+
| | |
106+
| | unclosed delimiter
107+
| closing delimiter possibly meant for this
108+
109+
error[E0493]: destructors cannot be evaluated at compile-time
110+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:5:13
111+
|
112+
LL | V = [PhantomData; { [ () ].len() ].len() as isize,
113+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constants cannot evaluate destructors
114+
115+
error[E0282]: type annotations needed
116+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:16:29
117+
|
118+
LL | V = [Vec::new; { [].len() ].len() as isize,
119+
| ^^^ cannot infer type for type parameter `T`
120+
121+
error[E0282]: type annotations needed
122+
--> $DIR/issue-67377-invalid-syntax-in-enum-discriminant.rs:27:14
123+
|
124+
LL | V = [Vec::new; { [0].len() ].len() as isize,
125+
| ^^^^^^^^ cannot infer type for type parameter `T`
126+
127+
error: aborting due to 15 previous errors
128+
129+
Some errors have detailed explanations: E0282, E0493.
130+
For more information about an error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)