Skip to content

Commit a60f9ca

Browse files
nrcnikomatsakis
authored andcommitted
Only use built-in indexing for uint indexes
1 parent 905d192 commit a60f9ca

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

src/librustc/middle/check_const.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &ast::Expr) {
165165
ast::ExprParen(..) |
166166
ast::ExprField(..) |
167167
ast::ExprTupField(..) |
168-
ast::ExprIndex(..) |
169168
ast::ExprTup(..) |
170169
ast::ExprRepeat(..) |
171170
ast::ExprStruct(..) => {}

src/librustc_typeck/check/method/confirm.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,18 +563,19 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
563563
self.fcx.adjust_expr_ty(
564564
&**base_expr,
565565
Some(&ty::AdjustDerefRef(base_adjustment.clone())));
566-
566+
let index_expr_ty = self.fcx.expr_ty(&**index_expr);
567+
567568
let result = check::try_index_step(
568569
self.fcx,
569570
MethodCall::expr(expr.id),
570571
*expr,
571572
&**base_expr,
572573
adjusted_base_ty,
573574
base_adjustment,
574-
PreferMutLvalue);
575+
PreferMutLvalue,
576+
index_expr_ty);
575577

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

580581
let expr_ty = self.fcx.expr_ty(&**expr);

src/librustc_typeck/check/mod.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,18 +2388,30 @@ fn try_index_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
23882388
base_expr: &ast::Expr,
23892389
adjusted_ty: Ty<'tcx>,
23902390
adjustment: ty::AutoDerefRef<'tcx>,
2391-
lvalue_pref: LvaluePreference)
2391+
lvalue_pref: LvaluePreference,
2392+
index_ty: Ty<'tcx>)
23922393
-> Option<(/*index type*/ Ty<'tcx>, /*element type*/ Ty<'tcx>)>
23932394
{
23942395
let tcx = fcx.tcx();
2395-
debug!("try_index_step(expr={}, base_expr.id={}, adjusted_ty={}, adjustment={})",
2396+
debug!("try_index_step(expr={}, base_expr.id={}, adjusted_ty={}, adjustment={}, index_ty={})",
23962397
expr.repr(tcx),
23972398
base_expr.repr(tcx),
23982399
adjusted_ty.repr(tcx),
2399-
adjustment);
2400+
adjustment,
2401+
index_ty.repr(tcx));
24002402

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

2405+
// First, try built-in indexing.
2406+
match (ty::index(adjusted_ty), &index_ty.sty) {
2407+
(Some(ty), &ty::ty_uint(ast::TyU)) | (Some(ty), &ty::ty_infer(ty::IntVar(_))) => {
2408+
debug!("try_index_step: success, using built-in indexing");
2409+
fcx.write_adjustment(base_expr.id, base_expr.span, ty::AdjustDerefRef(adjustment));
2410+
return Some((tcx.types.uint, ty));
2411+
}
2412+
_ => {}
2413+
}
2414+
24032415
// Try `IndexMut` first, if preferred.
24042416
let method = match (lvalue_pref, tcx.lang_items.index_mut_trait()) {
24052417
(PreferMutLvalue, Some(trait_did)) => {
@@ -2430,18 +2442,6 @@ fn try_index_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
24302442
(method, _) => method,
24312443
};
24322444

2433-
if method.is_none() {
2434-
// If there are no overridden index impls, use built-in indexing.
2435-
match ty::index(adjusted_ty) {
2436-
Some(ty) => {
2437-
debug!("try_index_step: success, using built-in indexing");
2438-
fcx.write_adjustment(base_expr.id, base_expr.span, ty::AdjustDerefRef(adjustment));
2439-
return Some((tcx.types.uint, ty));
2440-
}
2441-
None => {}
2442-
}
2443-
}
2444-
24452445
// If some lookup succeeds, write callee into table and extract index/element
24462446
// type from the method signature.
24472447
// If some lookup succeeded, install method in table
@@ -4206,11 +4206,14 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
42064206
&**base,
42074207
adj_ty,
42084208
adj,
4209-
lvalue_pref)
4209+
lvalue_pref,
4210+
idx_t)
42104211
});
42114212

42124213
match result {
42134214
Some((index_ty, element_ty)) => {
4215+
// FIXME: we've already checked idx above, we should
4216+
// probably just demand subtype or something here.
42144217
check_expr_has_type(fcx, &**idx, index_ty);
42154218
fcx.write_ty(id, element_ty);
42164219
}

0 commit comments

Comments
 (0)