@@ -190,9 +190,26 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
190190 if let hir:: ExprKind :: Index ( ref base, ref index) = e. kind {
191191 let mut tables = self . fcx . tables . borrow_mut ( ) ;
192192
193- // All valid indexing looks like this; might encounter non-valid indexes at this point
194- if let ty:: Ref ( _, base_ty, _) = tables. expr_ty_adjusted ( & base) . kind {
195- let index_ty = tables. expr_ty_adjusted ( & index) ;
193+ // All valid indexing looks like this; might encounter non-valid indexes at this point.
194+ let base_ty = tables. expr_ty_adjusted_opt ( & base) . map ( |t| & t. kind ) ;
195+ if base_ty. is_none ( ) {
196+ // When encountering `return [0][0]` outside of a `fn` body we can encounter a base
197+ // that isn't in the type table. We assume more relevant errors have already been
198+ // emitted, so we delay an ICE if none have. (#64638)
199+ self . tcx ( ) . sess . delay_span_bug ( e. span , & format ! ( "bad base: `{:?}`" , base) ) ;
200+ }
201+ if let Some ( ty:: Ref ( _, base_ty, _) ) = base_ty {
202+ let index_ty = tables. expr_ty_adjusted_opt ( & index) . unwrap_or_else ( || {
203+ // When encountering `return [0][0]` outside of a `fn` body we would attempt
204+ // to access an unexistend index. We assume that more relevant errors will
205+ // already have been emitted, so we only gate on this with an ICE if no
206+ // error has been emitted. (#64638)
207+ self . tcx ( ) . sess . delay_span_bug (
208+ e. span ,
209+ & format ! ( "bad index {:?} for base: `{:?}`" , index, base) ,
210+ ) ;
211+ self . fcx . tcx . types . err
212+ } ) ;
196213 let index_ty = self . fcx . resolve_vars_if_possible ( & index_ty) ;
197214
198215 if base_ty. builtin_index ( ) . is_some ( ) && index_ty == self . fcx . tcx . types . usize {
0 commit comments