Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid ICE on return outside of fn with literal array #64649

Merged
merged 2 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/librustc_typeck/check/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,26 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
if let hir::ExprKind::Index(ref base, ref index) = e.kind {
let mut tables = self.fcx.tables.borrow_mut();

// All valid indexing looks like this; might encounter non-valid indexes at this point
if let ty::Ref(_, base_ty, _) = tables.expr_ty_adjusted(&base).kind {
let index_ty = tables.expr_ty_adjusted(&index);
// All valid indexing looks like this; might encounter non-valid indexes at this point.
let base_ty = tables.expr_ty_adjusted_opt(&base).map(|t| &t.kind);
if base_ty.is_none() {
// When encountering `return [0][0]` outside of a `fn` body we can encounter a base
// that isn't in the type table. We assume more relevant errors have already been
// emitted, so we delay an ICE if none have. (#64638)
self.tcx().sess.delay_span_bug(e.span, &format!("bad base: `{:?}`", base));
}
if let Some(ty::Ref(_, base_ty, _)) = base_ty {
let index_ty = tables.expr_ty_adjusted_opt(&index).unwrap_or_else(|| {
// When encountering `return [0][0]` outside of a `fn` body we would attempt
// to access an unexistend index. We assume that more relevant errors will
// already have been emitted, so we only gate on this with an ICE if no
// error has been emitted. (#64638)
self.tcx().sess.delay_span_bug(
e.span,
&format!("bad index {:?} for base: `{:?}`", index, base),
);
self.fcx.tcx.types.err
});
let index_ty = self.fcx.resolve_vars_if_possible(&index_ty);

if base_ty.builtin_index().is_some() && index_ty == self.fcx.tcx.types.usize {
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/issues/issue-64620.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
enum Bug {
V1 = return [0][0] //~ERROR return statement outside of function body
}

fn main() {}
9 changes: 9 additions & 0 deletions src/test/ui/issues/issue-64620.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0572]: return statement outside of function body
--> $DIR/issue-64620.rs:2:10
|
LL | V1 = return [0][0]
| ^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0572`.