@@ -2387,18 +2387,30 @@ fn try_index_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
2387
2387
base_expr : & ast:: Expr ,
2388
2388
adjusted_ty : Ty < ' tcx > ,
2389
2389
adjustment : ty:: AutoDerefRef < ' tcx > ,
2390
- lvalue_pref : LvaluePreference )
2390
+ lvalue_pref : LvaluePreference ,
2391
+ index_ty : Ty < ' tcx > )
2391
2392
-> Option < ( /*index type*/ Ty < ' tcx > , /*element type*/ Ty < ' tcx > ) >
2392
2393
{
2393
2394
let tcx = fcx. tcx ( ) ;
2394
- debug ! ( "try_index_step(expr={}, base_expr.id={}, adjusted_ty={}, adjustment={})" ,
2395
+ debug ! ( "try_index_step(expr={}, base_expr.id={}, adjusted_ty={}, adjustment={}, index_ty={} )" ,
2395
2396
expr. repr( tcx) ,
2396
2397
base_expr. repr( tcx) ,
2397
2398
adjusted_ty. repr( tcx) ,
2398
- adjustment) ;
2399
+ adjustment,
2400
+ index_ty. repr( tcx) ) ;
2399
2401
2400
2402
let input_ty = fcx. infcx ( ) . next_ty_var ( ) ;
2401
2403
2404
+ // First, try built-in indexing.
2405
+ match ( ty:: index ( adjusted_ty) , & index_ty. sty ) {
2406
+ ( Some ( ty) , & ty:: ty_uint( ast:: TyU ) ) | ( Some ( ty) , & ty:: ty_infer( ty:: IntVar ( _) ) ) => {
2407
+ debug ! ( "try_index_step: success, using built-in indexing" ) ;
2408
+ fcx. write_adjustment ( base_expr. id , base_expr. span , ty:: AdjustDerefRef ( adjustment) ) ;
2409
+ return Some ( ( tcx. types . uint , ty) ) ;
2410
+ }
2411
+ _ => { }
2412
+ }
2413
+
2402
2414
// Try `IndexMut` first, if preferred.
2403
2415
let method = match ( lvalue_pref, tcx. lang_items . index_mut_trait ( ) ) {
2404
2416
( PreferMutLvalue , Some ( trait_did) ) => {
@@ -2429,18 +2441,6 @@ fn try_index_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
2429
2441
( method, _) => method,
2430
2442
} ;
2431
2443
2432
- if method. is_none ( ) {
2433
- // If there are no overridden index impls, use built-in indexing.
2434
- match ty:: index ( adjusted_ty) {
2435
- Some ( ty) => {
2436
- debug ! ( "try_index_step: success, using built-in indexing" ) ;
2437
- fcx. write_adjustment ( base_expr. id , base_expr. span , ty:: AdjustDerefRef ( adjustment) ) ;
2438
- return Some ( ( tcx. types . uint , ty) ) ;
2439
- }
2440
- None => { }
2441
- }
2442
- }
2443
-
2444
2444
// If some lookup succeeds, write callee into table and extract index/element
2445
2445
// type from the method signature.
2446
2446
// If some lookup succeeded, install method in table
@@ -4205,11 +4205,14 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
4205
4205
& * * base,
4206
4206
adj_ty,
4207
4207
adj,
4208
- lvalue_pref)
4208
+ lvalue_pref,
4209
+ idx_t)
4209
4210
} ) ;
4210
4211
4211
4212
match result {
4212
4213
Some ( ( index_ty, element_ty) ) => {
4214
+ // FIXME: we've already checked idx above, we should
4215
+ // probably just demand subtype or something here.
4213
4216
check_expr_has_type ( fcx, & * * idx, index_ty) ;
4214
4217
fcx. write_ty ( id, element_ty) ;
4215
4218
}
0 commit comments