Skip to content

Commit c11cb25

Browse files
authored
Rollup merge of rust-lang#64649 - estebank:returnator, r=varkor
Avoid ICE on return outside of fn with literal array Do not ICE when encountering `enum E { A = return [0][0] }`. Fix rust-lang#64638.
2 parents ff191b5 + 0e6fb8e commit c11cb25

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/librustc_typeck/check/writeback.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -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 {

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)