Skip to content

Commit

Permalink
Always type-check the index of an IndexExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
arielb1 committed Apr 15, 2015
1 parent 9c1dfed commit fd8c592
Showing 1 changed file with 24 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3515,34 +3515,34 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
}
ast::ExprIndex(ref base, ref idx) => {
check_expr_with_lvalue_pref(fcx, &**base, lvalue_pref);
check_expr(fcx, &**idx);

let base_t = fcx.expr_ty(&**base);
let idx_t = fcx.expr_ty(&**idx);

if ty::type_is_error(base_t) {
fcx.write_ty(id, base_t);
} else if ty::type_is_error(idx_t) {
fcx.write_ty(id, idx_t);
} else {
check_expr(fcx, &**idx);
let idx_t = fcx.expr_ty(&**idx);
if ty::type_is_error(idx_t) {
fcx.write_ty(id, idx_t);
} else {
let base_t = structurally_resolved_type(fcx, expr.span, base_t);
match lookup_indexing(fcx, expr, base, base_t, idx_t, lvalue_pref) {
Some((index_ty, element_ty)) => {
let idx_expr_ty = fcx.expr_ty(idx);
demand::eqtype(fcx, expr.span, index_ty, idx_expr_ty);
fcx.write_ty(id, element_ty);
}
None => {
check_expr_has_type(fcx, &**idx, fcx.tcx().types.err);
fcx.type_error_message(
expr.span,
|actual| {
format!("cannot index a value of type `{}`",
actual)
},
base_t,
None);
fcx.write_ty(id, fcx.tcx().types.err);
}
let base_t = structurally_resolved_type(fcx, expr.span, base_t);
match lookup_indexing(fcx, expr, base, base_t, idx_t, lvalue_pref) {
Some((index_ty, element_ty)) => {
let idx_expr_ty = fcx.expr_ty(idx);
demand::eqtype(fcx, expr.span, index_ty, idx_expr_ty);
fcx.write_ty(id, element_ty);
}
None => {
check_expr_has_type(fcx, &**idx, fcx.tcx().types.err);
fcx.type_error_message(
expr.span,
|actual| {
format!("cannot index a value of type `{}`",
actual)
},
base_t,
None);
fcx.write_ty(id, fcx.tcx().types.err);
}
}
}
Expand Down

0 comments on commit fd8c592

Please sign in to comment.