]> git.lizzy.rs Git - rust.git/commitdiff
address review comments
authorAriel Ben-Yehuda <ariel.byd@gmail.com>
Fri, 22 Jul 2016 20:52:53 +0000 (23:52 +0300)
committerAriel Ben-Yehuda <ariel.byd@gmail.com>
Fri, 22 Jul 2016 21:03:19 +0000 (00:03 +0300)
I split the RFC1592 commit out

12 files changed:
src/librustc/infer/mod.rs
src/librustc_const_eval/eval.rs
src/librustc_typeck/check/demand.rs
src/librustc_typeck/check/wfcheck.rs
src/test/compile-fail/const-eval-overflow-4b.rs
src/test/compile-fail/discrim-ill-typed.rs
src/test/compile-fail/explicit-self-lifetime-mismatch.rs
src/test/compile-fail/issue-17740.rs
src/test/compile-fail/issue-26194.rs
src/test/compile-fail/issue-8761.rs
src/test/compile-fail/repeat_count.rs
src/test/compile-fail/ufcs-explicit-self-bad.rs

index 1df06e8a00736c471111b7592a7252a110174c1c..87882c5528ec163cd60fce350bc9071066f9c639 100644 (file)
@@ -196,12 +196,6 @@ pub enum TypeOrigin {
     // FIXME(eddyb) #11161 is the original Expr required?
     ExprAssignable(Span),
 
-    // Relating trait refs when resolving vtables
-    RelateTraitRefs(Span),
-
-    // Relating self types when resolving vtables
-    RelateSelfType(Span),
-
     // Relating trait type parameters to those found in impl etc
     RelateOutputImplTypes(Span),
 
@@ -228,16 +222,17 @@ pub enum TypeOrigin {
 
     // intrinsic has wrong type
     IntrinsicType(Span),
+
+    // method receiver
+    MethodReceiver(Span),
 }
 
 impl TypeOrigin {
     fn as_failure_str(&self) -> &'static str {
         match self {
             &TypeOrigin::Misc(_) |
-            &TypeOrigin::RelateSelfType(_) |
             &TypeOrigin::RelateOutputImplTypes(_) |
             &TypeOrigin::ExprAssignable(_) => "mismatched types",
-            &TypeOrigin::RelateTraitRefs(_) => "mismatched traits",
             &TypeOrigin::MethodCompatCheck(_) => "method not compatible with trait",
             &TypeOrigin::MatchExpressionArm(_, _, source) => match source {
                 hir::MatchSource::IfLetDesugar{..} => "`if let` arms have incompatible types",
@@ -250,6 +245,7 @@ fn as_failure_str(&self) -> &'static str {
             &TypeOrigin::MainFunctionType(_) => "main function has wrong type",
             &TypeOrigin::StartFunctionType(_) => "start function has wrong type",
             &TypeOrigin::IntrinsicType(_) => "intrinsic has wrong type",
+            &TypeOrigin::MethodReceiver(_) => "mismatched method receiver",
         }
     }
 
@@ -258,8 +254,6 @@ fn as_requirement_str(&self) -> &'static str {
             &TypeOrigin::Misc(_) => "types are compatible",
             &TypeOrigin::MethodCompatCheck(_) => "method type is compatible with trait",
             &TypeOrigin::ExprAssignable(_) => "expression is assignable",
-            &TypeOrigin::RelateTraitRefs(_) => "traits are compatible",
-            &TypeOrigin::RelateSelfType(_) => "self type matches impl self type",
             &TypeOrigin::RelateOutputImplTypes(_) => {
                 "trait type parameters matches those specified on the impl"
             }
@@ -271,6 +265,7 @@ fn as_requirement_str(&self) -> &'static str {
             &TypeOrigin::MainFunctionType(_) => "`main` function has the correct type",
             &TypeOrigin::StartFunctionType(_) => "`start` function has the correct type",
             &TypeOrigin::IntrinsicType(_) => "intrinsic has the correct type",
+            &TypeOrigin::MethodReceiver(_) => "method receiver has the correct type",
         }
     }
 }
@@ -1806,8 +1801,6 @@ pub fn span(&self) -> Span {
             TypeOrigin::MethodCompatCheck(span) => span,
             TypeOrigin::ExprAssignable(span) => span,
             TypeOrigin::Misc(span) => span,
-            TypeOrigin::RelateTraitRefs(span) => span,
-            TypeOrigin::RelateSelfType(span) => span,
             TypeOrigin::RelateOutputImplTypes(span) => span,
             TypeOrigin::MatchExpressionArm(match_span, _, _) => match_span,
             TypeOrigin::IfExpression(span) => span,
@@ -1817,6 +1810,7 @@ pub fn span(&self) -> Span {
             TypeOrigin::MainFunctionType(span) => span,
             TypeOrigin::StartFunctionType(span) => span,
             TypeOrigin::IntrinsicType(span) => span,
+            TypeOrigin::MethodReceiver(span) => span,
         }
     }
 }
index 03d2f596e216544a0fb8835585125de039b3b153..2eb08cab1aa9f647ccce04ea3e218510213831b4 100644 (file)
@@ -384,15 +384,6 @@ pub fn note_const_eval_err<'a, 'tcx>(
                 diag.span_label(err.span, &message);
             }
         }
-        ConstEvalErrDescription::ExpectedFound { error, expected, found } => {
-            if check_old_school() {
-                diag.note(&error);
-            } else {
-                diag.span_label(err.span, &error);
-            }
-            diag.note(&format!("expected `{}`", expected));
-            diag.note(&format!("found `{}`", found));
-        }
     }
 
     if !primary_span.contains(err.span) {
@@ -477,11 +468,6 @@ fn from(err: ConstMathErr) -> ErrKind {
 #[derive(Clone, Debug)]
 pub enum ConstEvalErrDescription<'a> {
     Simple(Cow<'a, str>),
-    ExpectedFound {
-        error: Cow<'a, str>,
-        expected: Cow<'a, str>,
-        found: Cow<'a, str>
-    }
 }
 
 impl<'a> ConstEvalErrDescription<'a> {
@@ -489,14 +475,6 @@ impl<'a> ConstEvalErrDescription<'a> {
     pub fn into_oneline(self) -> Cow<'a, str> {
         match self {
             ConstEvalErrDescription::Simple(simple) => simple,
-            ConstEvalErrDescription::ExpectedFound {
-                error,
-                expected,
-                found
-            } => {
-                format!("{}: expected `{}`, found `{}`", error, expected, found)
-                    .into_cow()
-            }
         }
     }
 }
@@ -554,11 +532,7 @@ macro_rules! simple {
                  the constant evaluator"),
 
             TypeMismatch(ref expected, ref got) => {
-                ExpectedFound {
-                    error: "mismatched types".into_cow(),
-                    expected: <&str>::into_cow(expected),
-                    found: got.description().into_cow()
-                }
+                simple!("expected {}, found {}", expected, got.description())
             },
             BadType(ref i) => simple!("value of wrong type: {:?}", i),
             ErroneousReferencedConstant(_) => simple!("could not evaluate referenced constant"),
index c1f415b3c028a36cb51b171759c4606636b1330c..1f3a83ebc1d567da7a2ae7174213e62feaafd451 100644 (file)
@@ -33,7 +33,14 @@ pub fn demand_suptype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
     }
 
     pub fn demand_eqtype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
-        let origin = TypeOrigin::Misc(sp);
+        self.demand_eqtype_with_origin(TypeOrigin::Misc(sp), expected, actual);
+    }
+
+    pub fn demand_eqtype_with_origin(&self,
+                                     origin: TypeOrigin,
+                                     expected: Ty<'tcx>,
+                                     actual: Ty<'tcx>)
+    {
         match self.eq_types(false, origin, actual, expected) {
             Ok(InferOk { obligations, .. }) => {
                 // FIXME(#32730) propagate obligations
index 2d44a85f9af4bc7ba077d7b01f8d2e13a91cd4d1..907cb734c2ff9f00128641761e20eb9543b323fe 100644 (file)
@@ -13,6 +13,7 @@
 use CrateCtxt;
 use hir::def_id::DefId;
 use middle::region::{CodeExtent};
+use rustc::infer::TypeOrigin;
 use rustc::ty::subst::{self, TypeSpace, FnSpace, ParamSpace, SelfSpace};
 use rustc::traits;
 use rustc::ty::{self, Ty, TyCtxt};
@@ -157,7 +158,10 @@ fn check_item_well_formed(&mut self, item: &hir::Item) {
         }
     }
 
-    fn check_trait_or_impl_item(&mut self, item_id: ast::NodeId, span: Span) {
+    fn check_trait_or_impl_item(&mut self,
+                                item_id: ast::NodeId,
+                                span: Span,
+                                sig_if_method: Option<&hir::MethodSig>) {
         let code = self.code.clone();
         self.for_id(item_id, span).with_fcx(|fcx, this| {
             let free_substs = &fcx.parameter_environment.free_substs;
@@ -182,7 +186,8 @@ fn check_trait_or_impl_item(&mut self, item_id: ast::NodeId, span: Span) {
                     let predicates = fcx.instantiate_bounds(span, free_substs, &method.predicates);
                     this.check_fn_or_method(fcx, span, &method_ty, &predicates,
                                             free_id_outlive, &mut implied_bounds);
-                    this.check_method_receiver(fcx, span, &method,
+                    let sig_if_method = sig_if_method.expect("bad signature for method");
+                    this.check_method_receiver(fcx, sig_if_method, &method,
                                                free_id_outlive, self_ty);
                 }
                 ty::TypeTraitItem(assoc_type) => {
@@ -405,20 +410,15 @@ fn check_fn_or_method<'fcx, 'tcx>(&mut self,
 
     fn check_method_receiver<'fcx, 'tcx>(&mut self,
                                          fcx: &FnCtxt<'fcx, 'gcx, 'tcx>,
-                                         span: Span,
+                                         method_sig: &hir::MethodSig,
                                          method: &ty::Method<'tcx>,
                                          free_id_outlive: CodeExtent,
                                          self_ty: ty::Ty<'tcx>)
     {
         // check that the type of the method's receiver matches the
         // method's first parameter.
-
-        let free_substs = &fcx.parameter_environment.free_substs;
-        let fty = fcx.instantiate_type_scheme(span, free_substs, &method.fty);
-        let sig = fcx.tcx.liberate_late_bound_regions(free_id_outlive, &fty.sig);
-
-        debug!("check_method_receiver({:?},cat={:?},self_ty={:?},sig={:?})",
-               method.name, method.explicit_self, self_ty, sig);
+        debug!("check_method_receiver({:?},cat={:?},self_ty={:?})",
+               method.name, method.explicit_self, self_ty);
 
         let rcvr_ty = match method.explicit_self {
             ty::ExplicitSelfCategory::Static => return,
@@ -431,13 +431,23 @@ fn check_method_receiver<'fcx, 'tcx>(&mut self,
             }
             ty::ExplicitSelfCategory::ByBox => fcx.tcx.mk_box(self_ty)
         };
+
+        let span = method_sig.decl.inputs[0].pat.span;
+
+        let free_substs = &fcx.parameter_environment.free_substs;
+        let fty = fcx.instantiate_type_scheme(span, free_substs, &method.fty);
+        let sig = fcx.tcx.liberate_late_bound_regions(free_id_outlive, &fty.sig);
+
+        debug!("check_method_receiver: sig={:?}", sig);
+
         let rcvr_ty = fcx.instantiate_type_scheme(span, free_substs, &rcvr_ty);
         let rcvr_ty = fcx.tcx.liberate_late_bound_regions(free_id_outlive,
                                                           &ty::Binder(rcvr_ty));
 
         debug!("check_method_receiver: receiver ty = {:?}", rcvr_ty);
 
-        fcx.demand_eqtype(span, rcvr_ty, sig.inputs[0]);
+        let origin = TypeOrigin::MethodReceiver(span);
+        fcx.demand_eqtype_with_origin(origin, rcvr_ty, sig.inputs[0]);
     }
 
     fn check_variances_for_type_defn(&self,
@@ -552,13 +562,21 @@ fn visit_item(&mut self, i: &hir::Item) {
 
     fn visit_trait_item(&mut self, trait_item: &'v hir::TraitItem) {
         debug!("visit_trait_item: {:?}", trait_item);
-        self.check_trait_or_impl_item(trait_item.id, trait_item.span);
+        let method_sig = match trait_item.node {
+            hir::TraitItem_::MethodTraitItem(ref sig, _) => Some(sig),
+            _ => None
+        };
+        self.check_trait_or_impl_item(trait_item.id, trait_item.span, method_sig);
         intravisit::walk_trait_item(self, trait_item)
     }
 
     fn visit_impl_item(&mut self, impl_item: &'v hir::ImplItem) {
         debug!("visit_impl_item: {:?}", impl_item);
-        self.check_trait_or_impl_item(impl_item.id, impl_item.span);
+        let method_sig = match impl_item.node {
+            hir::ImplItemKind::Method(ref sig, _) => Some(sig),
+            _ => None
+        };
+        self.check_trait_or_impl_item(impl_item.id, impl_item.span, method_sig);
         intravisit::walk_impl_item(self, impl_item)
     }
 }
index e7639a4ff70ab86c90cd8080007118dda94d2cff..9e7a5ecae105a2b123a51519f20a2944b26bfe6a 100644 (file)
@@ -21,9 +21,7 @@
 const A_I8_T
     : [u32; (i8::MAX as i8 + 1u8) as usize]
     //~^ ERROR constant evaluation error [E0080]
-    //~| mismatched types
-    //~| expected `i8`
-    //~| found `u8`
+    //~| expected i8, found u8
     = [0; (i8::MAX as usize) + 1];
 
 
index 5af889cb23999ca2b01f0ce788c374021f06edc5..c73b7e831b3217051eb1eef9b64372af92abda81 100644 (file)
@@ -26,7 +26,7 @@ enum A {
         Ok2,
         OhNo = 0_u8,
         //~^ ERROR E0080
-        //~| mismatched types
+        //~| expected i8, found u8
     }
 
     let x = A::Ok;
@@ -39,7 +39,7 @@ enum A {
         Ok2,
         OhNo = 0_i8,
         //~^ ERROR E0080
-        //~| mismatched types
+        //~| expected u8, found i8
     }
 
     let x = A::Ok;
@@ -52,7 +52,7 @@ enum A {
         Ok2,
         OhNo = 0_u16,
         //~^ ERROR E0080
-        //~| mismatched types
+        //~| expected i16, found u16
     }
 
     let x = A::Ok;
@@ -65,7 +65,7 @@ enum A {
         Ok2,
         OhNo = 0_i16,
         //~^ ERROR E0080
-        //~| mismatched types
+        //~| expected u16, found i16
     }
 
     let x = A::Ok;
@@ -78,7 +78,7 @@ enum A {
         Ok2,
         OhNo = 0_u32,
         //~^ ERROR E0080
-        //~| mismatched types
+        //~| expected i32, found u32
     }
 
     let x = A::Ok;
@@ -91,7 +91,7 @@ enum A {
         Ok2,
         OhNo = 0_i32,
         //~^ ERROR E0080
-        //~| mismatched types
+        //~| expected u32, found i32
     }
 
     let x = A::Ok;
@@ -104,7 +104,7 @@ enum A {
         Ok2,
         OhNo = 0_u64,
         //~^ ERROR E0080
-        //~| mismatched types
+        //~| expected i64, found u64
     }
 
     let x = A::Ok;
@@ -117,7 +117,7 @@ enum A {
         Ok2,
         OhNo = 0_i64,
         //~^ ERROR E0080
-        //~| mismatched types
+        //~| expected u64, found i64
     }
 
     let x = A::Ok;
index b5432fafb1b855f0b09cb6a1a2634b847673acc8..f8aa1ea95f0f66d51039d8bb09d1c11502bf1ec2 100644 (file)
@@ -14,15 +14,17 @@ struct Foo<'a,'b> {
 }
 
 impl<'a,'b> Foo<'a,'b> {
-    fn bar(self: Foo<'b,'a>) {}
-    //~^ ERROR mismatched types
+    fn bar(
+        self
+    //~^ ERROR mismatched method receiver
     //~| expected type `Foo<'a, 'b>`
     //~| found type `Foo<'b, 'a>`
     //~| lifetime mismatch
-    //~| ERROR mismatched types
+    //~| ERROR mismatched method receiver
     //~| expected type `Foo<'a, 'b>`
     //~| found type `Foo<'b, 'a>`
     //~| lifetime mismatch
+            : Foo<'b,'a>) {}
 }
 
 fn main() {}
index 6b9294b2038f18fff4070765df96e8d574a31605..664d62e87ae61cf2f61a5f45d308275ce6f60856 100644 (file)
@@ -14,11 +14,11 @@ struct Foo<'a> {
 
 impl <'a> Foo<'a>{
     fn bar(self: &mut Foo) {
-    //~^ mismatched types
+    //~^ mismatched method receiver
     //~| expected type `&mut Foo<'a>`
     //~| found type `&mut Foo<'_>`
     //~| lifetime mismatch
-    //~| mismatched types
+    //~| mismatched method receiver
     //~| expected type `&mut Foo<'a>`
     //~| found type `&mut Foo<'_>`
     //~| lifetime mismatch
index 1bc0a4f9652197dd65c03c84ab6776e678f30a4a..ef91188c5d166bd1d8de2e11c18a3a82517627f2 100644 (file)
@@ -12,7 +12,7 @@
 
 impl S {
     fn f(self: *mut S) -> String { self.0 }
-    //~^ ERROR mismatched types
+    //~^ ERROR mismatched method receiver
 }
 
 fn main() { S("".to_owned()).f(); }
index 6352f4f6a0447d76b2bb91274869290fe6025abb..91a07dd9ba6dd68ede4d5bf9129a180f1d579d89 100644 (file)
 enum Foo {
     A = 1i64,
     //~^ ERROR constant evaluation error
-    //~| expected `isize`
-    //~| found `i64`
+    //~| expected isize, found i64
     B = 2u8
     //~^ ERROR constant evaluation error
-    //~| expected `isize`
-    //~| found `u8`
+    //~| expected isize, found u8
 }
 
 fn main() {}
index d68df97364169c40804d89e547631cdb67e8fd9d..3a7e9cc4191ec2af4de80bacf0579705aefe9bd8 100644 (file)
@@ -39,14 +39,12 @@ fn main() {
     //~| ERROR expected usize for repeat count, found string literal [E0306]
     let f = [0; -4_isize];
     //~^ ERROR constant evaluation error
-    //~| expected `usize`
-    //~| found `isize`
+    //~| expected usize, found isize
     //~| ERROR mismatched types
     //~| expected usize, found isize
     let f = [0_usize; -1_isize];
     //~^ ERROR constant evaluation error
-    //~| expected `usize`
-    //~| found `isize`
+    //~| expected usize, found isize
     //~| ERROR mismatched types
     //~| expected usize, found isize
     struct G {
index e997cf47c733373780c1d9de67ce9982526f7687..a98b7cd43090f3768723cdc4fdb175eb4a91d410 100644 (file)
@@ -15,7 +15,7 @@ struct Foo {
 }
 
 impl Foo {
-    fn foo(self: isize, x: isize) -> isize {  //~ ERROR mismatched types
+    fn foo(self: isize, x: isize) -> isize {  //~ ERROR mismatched method receiver
         self.f + x
     }
 }
@@ -25,10 +25,10 @@ struct Bar<T> {
 }
 
 impl<T> Bar<T> {
-    fn foo(self: Bar<isize>, x: isize) -> isize { //~ ERROR mismatched types
+    fn foo(self: Bar<isize>, x: isize) -> isize { //~ ERROR mismatched method receiver
         x
     }
-    fn bar(self: &Bar<usize>, x: isize) -> isize {   //~ ERROR mismatched types
+    fn bar(self: &Bar<usize>, x: isize) -> isize {   //~ ERROR mismatched method receiver
         x
     }
 }
@@ -41,14 +41,14 @@ trait SomeTrait {
 
 impl<'a, T> SomeTrait for &'a Bar<T> {
     fn dummy1(self: &&'a Bar<T>) { }
-    fn dummy2(self: &Bar<T>) {} //~ ERROR mismatched types
-    //~^ ERROR mismatched types
+    fn dummy2(self: &Bar<T>) {} //~ ERROR mismatched method receiver
+    //~^ ERROR mismatched method receiver
     fn dummy3(self: &&Bar<T>) {}
-    //~^ ERROR mismatched types
+    //~^ ERROR mismatched method receiver
     //~| expected type `&&'a Bar<T>`
     //~| found type `&&Bar<T>`
     //~| lifetime mismatch
-    //~| ERROR mismatched types
+    //~| ERROR mismatched method receiver
     //~| expected type `&&'a Bar<T>`
     //~| found type `&&Bar<T>`
     //~| lifetime mismatch