Skip to content

Commit fd8c592

Browse files
committed
Always type-check the index of an IndexExpr
Fixes #24363.
1 parent 9c1dfed commit fd8c592

File tree

1 file changed

+24
-24
lines changed
  • src/librustc_typeck/check

1 file changed

+24
-24
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3515,34 +3515,34 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
35153515
}
35163516
ast::ExprIndex(ref base, ref idx) => {
35173517
check_expr_with_lvalue_pref(fcx, &**base, lvalue_pref);
3518+
check_expr(fcx, &**idx);
3519+
35183520
let base_t = fcx.expr_ty(&**base);
3521+
let idx_t = fcx.expr_ty(&**idx);
3522+
35193523
if ty::type_is_error(base_t) {
35203524
fcx.write_ty(id, base_t);
3525+
} else if ty::type_is_error(idx_t) {
3526+
fcx.write_ty(id, idx_t);
35213527
} else {
3522-
check_expr(fcx, &**idx);
3523-
let idx_t = fcx.expr_ty(&**idx);
3524-
if ty::type_is_error(idx_t) {
3525-
fcx.write_ty(id, idx_t);
3526-
} else {
3527-
let base_t = structurally_resolved_type(fcx, expr.span, base_t);
3528-
match lookup_indexing(fcx, expr, base, base_t, idx_t, lvalue_pref) {
3529-
Some((index_ty, element_ty)) => {
3530-
let idx_expr_ty = fcx.expr_ty(idx);
3531-
demand::eqtype(fcx, expr.span, index_ty, idx_expr_ty);
3532-
fcx.write_ty(id, element_ty);
3533-
}
3534-
None => {
3535-
check_expr_has_type(fcx, &**idx, fcx.tcx().types.err);
3536-
fcx.type_error_message(
3537-
expr.span,
3538-
|actual| {
3539-
format!("cannot index a value of type `{}`",
3540-
actual)
3541-
},
3542-
base_t,
3543-
None);
3544-
fcx.write_ty(id, fcx.tcx().types.err);
3545-
}
3528+
let base_t = structurally_resolved_type(fcx, expr.span, base_t);
3529+
match lookup_indexing(fcx, expr, base, base_t, idx_t, lvalue_pref) {
3530+
Some((index_ty, element_ty)) => {
3531+
let idx_expr_ty = fcx.expr_ty(idx);
3532+
demand::eqtype(fcx, expr.span, index_ty, idx_expr_ty);
3533+
fcx.write_ty(id, element_ty);
3534+
}
3535+
None => {
3536+
check_expr_has_type(fcx, &**idx, fcx.tcx().types.err);
3537+
fcx.type_error_message(
3538+
expr.span,
3539+
|actual| {
3540+
format!("cannot index a value of type `{}`",
3541+
actual)
3542+
},
3543+
base_t,
3544+
None);
3545+
fcx.write_ty(id, fcx.tcx().types.err);
35463546
}
35473547
}
35483548
}

0 commit comments

Comments
 (0)