]> git.lizzy.rs Git - rust.git/commitdiff
Only use built-in indexing for uint indexes
authorNick Cameron <ncameron@mozilla.com>
Sun, 4 Jan 2015 03:08:18 +0000 (16:08 +1300)
committerNick Cameron <ncameron@mozilla.com>
Tue, 6 Jan 2015 21:49:00 +0000 (10:49 +1300)
src/librustc/middle/check_const.rs
src/librustc_typeck/check/method/confirm.rs
src/librustc_typeck/check/mod.rs

index ac53bdbefcf10a4e2d63c37efa7f4a4a9ae83474..d8d7c5afb10669082fa5003ad7541d47f10be673 100644 (file)
@@ -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(..) => {}
index a9018c8985a2f24b4a6c30792f5991f48b6c9166..d43850989fe7998277380c0fc1a3f490ac507df1 100644 (file)
@@ -562,7 +562,8 @@ fn fixup_derefs_on_method_receiver_if_necessary(&self,
                             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),
@@ -570,10 +571,10 @@ fn fixup_derefs_on_method_receiver_if_necessary(&self,
                             &**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);
index 24d2907e953d78f66a443f8e820b09ef46cb6f05..50f73312c770bc35faf26fb317e49f174f057551 100644 (file)
@@ -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)) => {
@@ -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
@@ -4205,11 +4205,14 @@ fn check_struct_fields_on_error(fcx: &FnCtxt,
                                          &**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);
                       }