Skip to content

Commit

Permalink
Always check both operands of an indexing expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Ariel Ben-Yehuda committed Apr 14, 2015
1 parent 33120de commit ee6666d
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3589,53 +3589,53 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
check_tup_field(fcx, expr, lvalue_pref, &**base, idx);
}
ast::ExprIndex(ref base, ref idx) => {
check_expr_with_lvalue_pref(fcx, &**base, lvalue_pref);
let base_t = fcx.expr_ty(&**base);
if ty::type_is_error(base_t) {
fcx.write_ty(id, base_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);

let result =
autoderef_for_index(fcx, &**base, base_t, lvalue_pref, |adj_ty, adj| {
try_index_step(fcx,
MethodCall::expr(expr.id),
expr,
&**base,
adj_ty,
adj,
lvalue_pref,
idx_t)
});
check_expr_with_lvalue_pref(fcx, &**base, lvalue_pref);
check_expr(fcx, &**idx);

match result {
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);
}
_ => {
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);
}
}
}
}
}
ast::ExprRange(ref start, ref end) => {
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 {
let base_t = structurally_resolved_type(fcx, expr.span, base_t);

let result =
autoderef_for_index(fcx, &**base, base_t, lvalue_pref, |adj_ty, adj| {
try_index_step(fcx,
MethodCall::expr(expr.id),
expr,
&**base,
adj_ty,
adj,
lvalue_pref,
idx_t)
});

match result {
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);
}
_ => {
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);
}
}
}
}
ast::ExprRange(ref start, ref end) => {
let t_start = start.as_ref().map(|e| {
check_expr(fcx, &**e);
fcx.expr_ty(&**e)
Expand Down

0 comments on commit ee6666d

Please sign in to comment.