Skip to content

Commit

Permalink
Only use built-in indexing for uint indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
nrc committed Jan 6, 2015
1 parent 77ed497 commit 480374a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/librustc/middle/check_const.rs
Expand Up @@ -165,7 +165,6 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &ast::Expr) {
ast::ExprParen(..) |
ast::ExprField(..) |
ast::ExprTupField(..) |
ast::ExprIndex(..) |
ast::ExprTup(..) |
ast::ExprRepeat(..) |
ast::ExprStruct(..) => {}
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_typeck/check/method/confirm.rs
Expand Up @@ -562,18 +562,19 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
self.fcx.adjust_expr_ty(
&**base_expr,
Some(&ty::AdjustDerefRef(base_adjustment.clone())));

let index_expr_ty = self.fcx.expr_ty(&**index_expr);

let result = check::try_index_step(
self.fcx,
MethodCall::expr(expr.id),
*expr,
&**base_expr,
adjusted_base_ty,
base_adjustment,
PreferMutLvalue);
PreferMutLvalue,
index_expr_ty);

if let Some((input_ty, return_ty)) = result {
let index_expr_ty = self.fcx.expr_ty(&**index_expr);
demand::suptype(self.fcx, index_expr.span, input_ty, index_expr_ty);

let expr_ty = self.fcx.expr_ty(&**expr);
Expand Down
35 changes: 19 additions & 16 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -2387,18 +2387,30 @@ fn try_index_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
base_expr: &ast::Expr,
adjusted_ty: Ty<'tcx>,
adjustment: ty::AutoDerefRef<'tcx>,
lvalue_pref: LvaluePreference)
lvalue_pref: LvaluePreference,
index_ty: Ty<'tcx>)
-> Option<(/*index type*/ Ty<'tcx>, /*element type*/ Ty<'tcx>)>
{
let tcx = fcx.tcx();
debug!("try_index_step(expr={}, base_expr.id={}, adjusted_ty={}, adjustment={})",
debug!("try_index_step(expr={}, base_expr.id={}, adjusted_ty={}, adjustment={}, index_ty={})",
expr.repr(tcx),
base_expr.repr(tcx),
adjusted_ty.repr(tcx),
adjustment);
adjustment,
index_ty.repr(tcx));

let input_ty = fcx.infcx().next_ty_var();

// First, try built-in indexing.
match (ty::index(adjusted_ty), &index_ty.sty) {
(Some(ty), &ty::ty_uint(ast::TyU)) | (Some(ty), &ty::ty_infer(ty::IntVar(_))) => {
debug!("try_index_step: success, using built-in indexing");
fcx.write_adjustment(base_expr.id, base_expr.span, ty::AdjustDerefRef(adjustment));
return Some((tcx.types.uint, ty));
}
_ => {}
}

// Try `IndexMut` first, if preferred.
let method = match (lvalue_pref, tcx.lang_items.index_mut_trait()) {
(PreferMutLvalue, Some(trait_did)) => {
Expand Down Expand Up @@ -2429,18 +2441,6 @@ fn try_index_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
(method, _) => method,
};

if method.is_none() {
// If there are no overridden index impls, use built-in indexing.
match ty::index(adjusted_ty) {
Some(ty) => {
debug!("try_index_step: success, using built-in indexing");
fcx.write_adjustment(base_expr.id, base_expr.span, ty::AdjustDerefRef(adjustment));
return Some((tcx.types.uint, ty));
}
None => {}
}
}

// If some lookup succeeds, write callee into table and extract index/element
// type from the method signature.
// If some lookup succeeded, install method in table
Expand Down Expand Up @@ -4205,11 +4205,14 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
&**base,
adj_ty,
adj,
lvalue_pref)
lvalue_pref,
idx_t)
});

match result {
Some((index_ty, element_ty)) => {
// FIXME: we've already checked idx above, we should
// probably just demand subtype or something here.
check_expr_has_type(fcx, &**idx, index_ty);
fcx.write_ty(id, element_ty);
}
Expand Down

0 comments on commit 480374a

Please sign in to comment.