@@ -2388,18 +2388,30 @@ fn try_index_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
2388
2388
base_expr : & ast:: Expr ,
2389
2389
adjusted_ty : Ty < ' tcx > ,
2390
2390
adjustment : ty:: AutoDerefRef < ' tcx > ,
2391
- lvalue_pref : LvaluePreference )
2391
+ lvalue_pref : LvaluePreference ,
2392
+ index_ty : Ty < ' tcx > )
2392
2393
-> Option < ( /*index type*/ Ty < ' tcx > , /*element type*/ Ty < ' tcx > ) >
2393
2394
{
2394
2395
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={} )" ,
2396
2397
expr. repr( tcx) ,
2397
2398
base_expr. repr( tcx) ,
2398
2399
adjusted_ty. repr( tcx) ,
2399
- adjustment) ;
2400
+ adjustment,
2401
+ index_ty. repr( tcx) ) ;
2400
2402
2401
2403
let input_ty = fcx. infcx ( ) . next_ty_var ( ) ;
2402
2404
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
+
2403
2415
// Try `IndexMut` first, if preferred.
2404
2416
let method = match ( lvalue_pref, tcx. lang_items . index_mut_trait ( ) ) {
2405
2417
( PreferMutLvalue , Some ( trait_did) ) => {
@@ -2430,18 +2442,6 @@ fn try_index_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
2430
2442
( method, _) => method,
2431
2443
} ;
2432
2444
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
-
2445
2445
// If some lookup succeeds, write callee into table and extract index/element
2446
2446
// type from the method signature.
2447
2447
// If some lookup succeeded, install method in table
@@ -4206,11 +4206,14 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
4206
4206
& * * base,
4207
4207
adj_ty,
4208
4208
adj,
4209
- lvalue_pref)
4209
+ lvalue_pref,
4210
+ idx_t)
4210
4211
} ) ;
4211
4212
4212
4213
match result {
4213
4214
Some ( ( index_ty, element_ty) ) => {
4215
+ // FIXME: we've already checked idx above, we should
4216
+ // probably just demand subtype or something here.
4214
4217
check_expr_has_type ( fcx, & * * idx, index_ty) ;
4215
4218
fcx. write_ty ( id, element_ty) ;
4216
4219
}
0 commit comments