about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/librustc_typeck/check/writeback.rs25
1 files changed, 14 insertions, 11 deletions
diff --git a/src/librustc_typeck/check/writeback.rs b/src/librustc_typeck/check/writeback.rs
index 0dbf4dd09bd..efdcdf4e7d0 100644
--- a/src/librustc_typeck/check/writeback.rs
+++ b/src/librustc_typeck/check/writeback.rs
@@ -192,20 +192,23 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
             // 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));
-                return;
             }
             if let Some(ty::Ref(_, base_ty, _)) = base_ty {
-                let index_ty = match tables.expr_ty_adjusted_opt(&index) {
-                    Some(t) => t,
-                    None => {
-                        self.tcx().sess.delay_span_bug(
-                            e.span,
-                            &format!("bad index {:?} for base: `{:?}`", index, base),
-                        );
-                        self.fcx.tcx.types.err
-                    }
-                };
+                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 {