From: Ariel Ben-Yehuda Date: Fri, 22 Jul 2016 20:52:53 +0000 (+0300) Subject: address review comments X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=717e39294f635d90f8ba9e0968494f741878f37b;p=rust.git address review comments I split the RFC1592 commit out --- diff --git a/src/librustc/infer/mod.rs b/src/librustc/infer/mod.rs index 1df06e8a007..87882c5528e 100644 --- a/src/librustc/infer/mod.rs +++ b/src/librustc/infer/mod.rs @@ -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, } } } diff --git a/src/librustc_const_eval/eval.rs b/src/librustc_const_eval/eval.rs index 03d2f596e21..2eb08cab1aa 100644 --- a/src/librustc_const_eval/eval.rs +++ b/src/librustc_const_eval/eval.rs @@ -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"), diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index c1f415b3c02..1f3a83ebc1d 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -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 diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 2d44a85f9af..907cb734c2f 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -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) } } diff --git a/src/test/compile-fail/const-eval-overflow-4b.rs b/src/test/compile-fail/const-eval-overflow-4b.rs index e7639a4ff70..9e7a5ecae10 100644 --- a/src/test/compile-fail/const-eval-overflow-4b.rs +++ b/src/test/compile-fail/const-eval-overflow-4b.rs @@ -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]; diff --git a/src/test/compile-fail/discrim-ill-typed.rs b/src/test/compile-fail/discrim-ill-typed.rs index 5af889cb239..c73b7e831b3 100644 --- a/src/test/compile-fail/discrim-ill-typed.rs +++ b/src/test/compile-fail/discrim-ill-typed.rs @@ -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; diff --git a/src/test/compile-fail/explicit-self-lifetime-mismatch.rs b/src/test/compile-fail/explicit-self-lifetime-mismatch.rs index b5432fafb1b..f8aa1ea95f0 100644 --- a/src/test/compile-fail/explicit-self-lifetime-mismatch.rs +++ b/src/test/compile-fail/explicit-self-lifetime-mismatch.rs @@ -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() {} diff --git a/src/test/compile-fail/issue-17740.rs b/src/test/compile-fail/issue-17740.rs index 6b9294b2038..664d62e87ae 100644 --- a/src/test/compile-fail/issue-17740.rs +++ b/src/test/compile-fail/issue-17740.rs @@ -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 diff --git a/src/test/compile-fail/issue-26194.rs b/src/test/compile-fail/issue-26194.rs index 1bc0a4f9652..ef91188c5d1 100644 --- a/src/test/compile-fail/issue-26194.rs +++ b/src/test/compile-fail/issue-26194.rs @@ -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(); } diff --git a/src/test/compile-fail/issue-8761.rs b/src/test/compile-fail/issue-8761.rs index 6352f4f6a04..91a07dd9ba6 100644 --- a/src/test/compile-fail/issue-8761.rs +++ b/src/test/compile-fail/issue-8761.rs @@ -11,12 +11,10 @@ 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() {} diff --git a/src/test/compile-fail/repeat_count.rs b/src/test/compile-fail/repeat_count.rs index d68df973641..3a7e9cc4191 100644 --- a/src/test/compile-fail/repeat_count.rs +++ b/src/test/compile-fail/repeat_count.rs @@ -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 { diff --git a/src/test/compile-fail/ufcs-explicit-self-bad.rs b/src/test/compile-fail/ufcs-explicit-self-bad.rs index e997cf47c73..a98b7cd4309 100644 --- a/src/test/compile-fail/ufcs-explicit-self-bad.rs +++ b/src/test/compile-fail/ufcs-explicit-self-bad.rs @@ -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 { } impl Bar { - fn foo(self: Bar, x: isize) -> isize { //~ ERROR mismatched types + fn foo(self: Bar, x: isize) -> isize { //~ ERROR mismatched method receiver x } - fn bar(self: &Bar, x: isize) -> isize { //~ ERROR mismatched types + fn bar(self: &Bar, x: isize) -> isize { //~ ERROR mismatched method receiver x } } @@ -41,14 +41,14 @@ trait SomeTrait { impl<'a, T> SomeTrait for &'a Bar { fn dummy1(self: &&'a Bar) { } - fn dummy2(self: &Bar) {} //~ ERROR mismatched types - //~^ ERROR mismatched types + fn dummy2(self: &Bar) {} //~ ERROR mismatched method receiver + //~^ ERROR mismatched method receiver fn dummy3(self: &&Bar) {} - //~^ ERROR mismatched types + //~^ ERROR mismatched method receiver //~| expected type `&&'a Bar` //~| found type `&&Bar` //~| lifetime mismatch - //~| ERROR mismatched types + //~| ERROR mismatched method receiver //~| expected type `&&'a Bar` //~| found type `&&Bar` //~| lifetime mismatch