Skip to content

Commit 2ae87ff

Browse files
committedSep 30, 2019
Avoid ICE on return outside of fn with literal array
Do not ICE when encountering `enum E { A = return [0][0] }`.
1 parent 22bc9e1 commit 2ae87ff

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed
 

‎src/librustc_typeck/check/writeback.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,23 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
189189
if let hir::ExprKind::Index(ref base, ref index) = e.kind {
190190
let mut tables = self.fcx.tables.borrow_mut();
191191

192-
// All valid indexing looks like this; might encounter non-valid indexes at this point
193-
if let ty::Ref(_, base_ty, _) = tables.expr_ty_adjusted(&base).kind {
194-
let index_ty = tables.expr_ty_adjusted(&index);
192+
// All valid indexing looks like this; might encounter non-valid indexes at this point.
193+
let base_ty = tables.expr_ty_adjusted_opt(&base).map(|t| &t.kind);
194+
if base_ty.is_none() {
195+
self.tcx().sess.delay_span_bug(e.span, &format!("bad base: `{:?}`", base));
196+
return;
197+
}
198+
if let Some(ty::Ref(_, base_ty, _)) = base_ty {
199+
let index_ty = match tables.expr_ty_adjusted_opt(&index) {
200+
Some(t) => t,
201+
None => {
202+
self.tcx().sess.delay_span_bug(
203+
e.span,
204+
&format!("bad index {:?} for base: `{:?}`", index, base),
205+
);
206+
self.fcx.tcx.types.err
207+
}
208+
};
195209
let index_ty = self.fcx.resolve_vars_if_possible(&index_ty);
196210

197211
if base_ty.builtin_index().is_some() && index_ty == self.fcx.tcx.types.usize {

‎src/test/ui/issues/issue-64620.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
enum Bug {
2+
V1 = return [0][0] //~ERROR return statement outside of function body
3+
}
4+
5+
fn main() {}

‎src/test/ui/issues/issue-64620.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0572]: return statement outside of function body
2+
--> $DIR/issue-64620.rs:2:10
3+
|
4+
LL | V1 = return [0][0]
5+
| ^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0572`.

0 commit comments

Comments
 (0)
Please sign in to comment.