From 2ae87ff59d1261d6295d82b0b29bc5440ada0b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 20 Sep 2019 19:31:51 -0700 Subject: [PATCH 1/2] Avoid ICE on return outside of fn with literal array Do not ICE when encountering `enum E { A = return [0][0] }`. --- src/librustc_typeck/check/writeback.rs | 20 +++++++++++++++++--- src/test/ui/issues/issue-64620.rs | 5 +++++ src/test/ui/issues/issue-64620.stderr | 9 +++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 src/test/ui/issues/issue-64620.rs create mode 100644 src/test/ui/issues/issue-64620.stderr diff --git a/src/librustc_typeck/check/writeback.rs b/src/librustc_typeck/check/writeback.rs index de78c1cdfaba3..0dbf4dd09bd50 100644 --- a/src/librustc_typeck/check/writeback.rs +++ b/src/librustc_typeck/check/writeback.rs @@ -189,9 +189,23 @@ 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() { + 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 = self.fcx.resolve_vars_if_possible(&index_ty); if base_ty.builtin_index().is_some() && index_ty == self.fcx.tcx.types.usize { diff --git a/src/test/ui/issues/issue-64620.rs b/src/test/ui/issues/issue-64620.rs new file mode 100644 index 0000000000000..a62e5bf8d3c62 --- /dev/null +++ b/src/test/ui/issues/issue-64620.rs @@ -0,0 +1,5 @@ +enum Bug { + V1 = return [0][0] //~ERROR return statement outside of function body +} + +fn main() {} diff --git a/src/test/ui/issues/issue-64620.stderr b/src/test/ui/issues/issue-64620.stderr new file mode 100644 index 0000000000000..f40ac4de32d59 --- /dev/null +++ b/src/test/ui/issues/issue-64620.stderr @@ -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`. From 0e6fb8e8da2f3256f9e2c2c079b8174acf80d94d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 21 Sep 2019 09:53:25 -0700 Subject: [PATCH 2/2] review comments --- src/librustc_typeck/check/writeback.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/librustc_typeck/check/writeback.rs b/src/librustc_typeck/check/writeback.rs index 0dbf4dd09bd50..efdcdf4e7d06f 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 {