Skip to content

Commit 717e392

Browse files
committed
address review comments
I split the RFC1592 commit out
1 parent 23bb1df commit 717e392

File tree

12 files changed

+74
-85
lines changed

12 files changed

+74
-85
lines changed

src/librustc/infer/mod.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,6 @@ pub enum TypeOrigin {
196196
// FIXME(eddyb) #11161 is the original Expr required?
197197
ExprAssignable(Span),
198198

199-
// Relating trait refs when resolving vtables
200-
RelateTraitRefs(Span),
201-
202-
// Relating self types when resolving vtables
203-
RelateSelfType(Span),
204-
205199
// Relating trait type parameters to those found in impl etc
206200
RelateOutputImplTypes(Span),
207201

@@ -228,16 +222,17 @@ pub enum TypeOrigin {
228222

229223
// intrinsic has wrong type
230224
IntrinsicType(Span),
225+
226+
// method receiver
227+
MethodReceiver(Span),
231228
}
232229

233230
impl TypeOrigin {
234231
fn as_failure_str(&self) -> &'static str {
235232
match self {
236233
&TypeOrigin::Misc(_) |
237-
&TypeOrigin::RelateSelfType(_) |
238234
&TypeOrigin::RelateOutputImplTypes(_) |
239235
&TypeOrigin::ExprAssignable(_) => "mismatched types",
240-
&TypeOrigin::RelateTraitRefs(_) => "mismatched traits",
241236
&TypeOrigin::MethodCompatCheck(_) => "method not compatible with trait",
242237
&TypeOrigin::MatchExpressionArm(_, _, source) => match source {
243238
hir::MatchSource::IfLetDesugar{..} => "`if let` arms have incompatible types",
@@ -250,6 +245,7 @@ impl TypeOrigin {
250245
&TypeOrigin::MainFunctionType(_) => "main function has wrong type",
251246
&TypeOrigin::StartFunctionType(_) => "start function has wrong type",
252247
&TypeOrigin::IntrinsicType(_) => "intrinsic has wrong type",
248+
&TypeOrigin::MethodReceiver(_) => "mismatched method receiver",
253249
}
254250
}
255251

@@ -258,8 +254,6 @@ impl TypeOrigin {
258254
&TypeOrigin::Misc(_) => "types are compatible",
259255
&TypeOrigin::MethodCompatCheck(_) => "method type is compatible with trait",
260256
&TypeOrigin::ExprAssignable(_) => "expression is assignable",
261-
&TypeOrigin::RelateTraitRefs(_) => "traits are compatible",
262-
&TypeOrigin::RelateSelfType(_) => "self type matches impl self type",
263257
&TypeOrigin::RelateOutputImplTypes(_) => {
264258
"trait type parameters matches those specified on the impl"
265259
}
@@ -271,6 +265,7 @@ impl TypeOrigin {
271265
&TypeOrigin::MainFunctionType(_) => "`main` function has the correct type",
272266
&TypeOrigin::StartFunctionType(_) => "`start` function has the correct type",
273267
&TypeOrigin::IntrinsicType(_) => "intrinsic has the correct type",
268+
&TypeOrigin::MethodReceiver(_) => "method receiver has the correct type",
274269
}
275270
}
276271
}
@@ -1806,8 +1801,6 @@ impl TypeOrigin {
18061801
TypeOrigin::MethodCompatCheck(span) => span,
18071802
TypeOrigin::ExprAssignable(span) => span,
18081803
TypeOrigin::Misc(span) => span,
1809-
TypeOrigin::RelateTraitRefs(span) => span,
1810-
TypeOrigin::RelateSelfType(span) => span,
18111804
TypeOrigin::RelateOutputImplTypes(span) => span,
18121805
TypeOrigin::MatchExpressionArm(match_span, _, _) => match_span,
18131806
TypeOrigin::IfExpression(span) => span,
@@ -1817,6 +1810,7 @@ impl TypeOrigin {
18171810
TypeOrigin::MainFunctionType(span) => span,
18181811
TypeOrigin::StartFunctionType(span) => span,
18191812
TypeOrigin::IntrinsicType(span) => span,
1813+
TypeOrigin::MethodReceiver(span) => span,
18201814
}
18211815
}
18221816
}

src/librustc_const_eval/eval.rs

+1-27
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,6 @@ pub fn note_const_eval_err<'a, 'tcx>(
384384
diag.span_label(err.span, &message);
385385
}
386386
}
387-
ConstEvalErrDescription::ExpectedFound { error, expected, found } => {
388-
if check_old_school() {
389-
diag.note(&error);
390-
} else {
391-
diag.span_label(err.span, &error);
392-
}
393-
diag.note(&format!("expected `{}`", expected));
394-
diag.note(&format!("found `{}`", found));
395-
}
396387
}
397388

398389
if !primary_span.contains(err.span) {
@@ -477,26 +468,13 @@ impl From<ConstMathErr> for ErrKind {
477468
#[derive(Clone, Debug)]
478469
pub enum ConstEvalErrDescription<'a> {
479470
Simple(Cow<'a, str>),
480-
ExpectedFound {
481-
error: Cow<'a, str>,
482-
expected: Cow<'a, str>,
483-
found: Cow<'a, str>
484-
}
485471
}
486472

487473
impl<'a> ConstEvalErrDescription<'a> {
488474
/// Return a one-line description of the error, for lints and such
489475
pub fn into_oneline(self) -> Cow<'a, str> {
490476
match self {
491477
ConstEvalErrDescription::Simple(simple) => simple,
492-
ConstEvalErrDescription::ExpectedFound {
493-
error,
494-
expected,
495-
found
496-
} => {
497-
format!("{}: expected `{}`, found `{}`", error, expected, found)
498-
.into_cow()
499-
}
500478
}
501479
}
502480
}
@@ -554,11 +532,7 @@ impl ConstEvalErr {
554532
the constant evaluator"),
555533

556534
TypeMismatch(ref expected, ref got) => {
557-
ExpectedFound {
558-
error: "mismatched types".into_cow(),
559-
expected: <&str>::into_cow(expected),
560-
found: got.description().into_cow()
561-
}
535+
simple!("expected {}, found {}", expected, got.description())
562536
},
563537
BadType(ref i) => simple!("value of wrong type: {:?}", i),
564538
ErroneousReferencedConstant(_) => simple!("could not evaluate referenced constant"),

src/librustc_typeck/check/demand.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
3333
}
3434

3535
pub fn demand_eqtype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
36-
let origin = TypeOrigin::Misc(sp);
36+
self.demand_eqtype_with_origin(TypeOrigin::Misc(sp), expected, actual);
37+
}
38+
39+
pub fn demand_eqtype_with_origin(&self,
40+
origin: TypeOrigin,
41+
expected: Ty<'tcx>,
42+
actual: Ty<'tcx>)
43+
{
3744
match self.eq_types(false, origin, actual, expected) {
3845
Ok(InferOk { obligations, .. }) => {
3946
// FIXME(#32730) propagate obligations

src/librustc_typeck/check/wfcheck.rs

+31-13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use constrained_type_params::{identify_constrained_type_params, Parameter};
1313
use CrateCtxt;
1414
use hir::def_id::DefId;
1515
use middle::region::{CodeExtent};
16+
use rustc::infer::TypeOrigin;
1617
use rustc::ty::subst::{self, TypeSpace, FnSpace, ParamSpace, SelfSpace};
1718
use rustc::traits;
1819
use rustc::ty::{self, Ty, TyCtxt};
@@ -157,7 +158,10 @@ impl<'ccx, 'gcx> CheckTypeWellFormedVisitor<'ccx, 'gcx> {
157158
}
158159
}
159160

160-
fn check_trait_or_impl_item(&mut self, item_id: ast::NodeId, span: Span) {
161+
fn check_trait_or_impl_item(&mut self,
162+
item_id: ast::NodeId,
163+
span: Span,
164+
sig_if_method: Option<&hir::MethodSig>) {
161165
let code = self.code.clone();
162166
self.for_id(item_id, span).with_fcx(|fcx, this| {
163167
let free_substs = &fcx.parameter_environment.free_substs;
@@ -182,7 +186,8 @@ impl<'ccx, 'gcx> CheckTypeWellFormedVisitor<'ccx, 'gcx> {
182186
let predicates = fcx.instantiate_bounds(span, free_substs, &method.predicates);
183187
this.check_fn_or_method(fcx, span, &method_ty, &predicates,
184188
free_id_outlive, &mut implied_bounds);
185-
this.check_method_receiver(fcx, span, &method,
189+
let sig_if_method = sig_if_method.expect("bad signature for method");
190+
this.check_method_receiver(fcx, sig_if_method, &method,
186191
free_id_outlive, self_ty);
187192
}
188193
ty::TypeTraitItem(assoc_type) => {
@@ -405,20 +410,15 @@ impl<'ccx, 'gcx> CheckTypeWellFormedVisitor<'ccx, 'gcx> {
405410

406411
fn check_method_receiver<'fcx, 'tcx>(&mut self,
407412
fcx: &FnCtxt<'fcx, 'gcx, 'tcx>,
408-
span: Span,
413+
method_sig: &hir::MethodSig,
409414
method: &ty::Method<'tcx>,
410415
free_id_outlive: CodeExtent,
411416
self_ty: ty::Ty<'tcx>)
412417
{
413418
// check that the type of the method's receiver matches the
414419
// method's first parameter.
415-
416-
let free_substs = &fcx.parameter_environment.free_substs;
417-
let fty = fcx.instantiate_type_scheme(span, free_substs, &method.fty);
418-
let sig = fcx.tcx.liberate_late_bound_regions(free_id_outlive, &fty.sig);
419-
420-
debug!("check_method_receiver({:?},cat={:?},self_ty={:?},sig={:?})",
421-
method.name, method.explicit_self, self_ty, sig);
420+
debug!("check_method_receiver({:?},cat={:?},self_ty={:?})",
421+
method.name, method.explicit_self, self_ty);
422422

423423
let rcvr_ty = match method.explicit_self {
424424
ty::ExplicitSelfCategory::Static => return,
@@ -431,13 +431,23 @@ impl<'ccx, 'gcx> CheckTypeWellFormedVisitor<'ccx, 'gcx> {
431431
}
432432
ty::ExplicitSelfCategory::ByBox => fcx.tcx.mk_box(self_ty)
433433
};
434+
435+
let span = method_sig.decl.inputs[0].pat.span;
436+
437+
let free_substs = &fcx.parameter_environment.free_substs;
438+
let fty = fcx.instantiate_type_scheme(span, free_substs, &method.fty);
439+
let sig = fcx.tcx.liberate_late_bound_regions(free_id_outlive, &fty.sig);
440+
441+
debug!("check_method_receiver: sig={:?}", sig);
442+
434443
let rcvr_ty = fcx.instantiate_type_scheme(span, free_substs, &rcvr_ty);
435444
let rcvr_ty = fcx.tcx.liberate_late_bound_regions(free_id_outlive,
436445
&ty::Binder(rcvr_ty));
437446

438447
debug!("check_method_receiver: receiver ty = {:?}", rcvr_ty);
439448

440-
fcx.demand_eqtype(span, rcvr_ty, sig.inputs[0]);
449+
let origin = TypeOrigin::MethodReceiver(span);
450+
fcx.demand_eqtype_with_origin(origin, rcvr_ty, sig.inputs[0]);
441451
}
442452

443453
fn check_variances_for_type_defn(&self,
@@ -552,13 +562,21 @@ impl<'ccx, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'ccx, 'tcx> {
552562

553563
fn visit_trait_item(&mut self, trait_item: &'v hir::TraitItem) {
554564
debug!("visit_trait_item: {:?}", trait_item);
555-
self.check_trait_or_impl_item(trait_item.id, trait_item.span);
565+
let method_sig = match trait_item.node {
566+
hir::TraitItem_::MethodTraitItem(ref sig, _) => Some(sig),
567+
_ => None
568+
};
569+
self.check_trait_or_impl_item(trait_item.id, trait_item.span, method_sig);
556570
intravisit::walk_trait_item(self, trait_item)
557571
}
558572

559573
fn visit_impl_item(&mut self, impl_item: &'v hir::ImplItem) {
560574
debug!("visit_impl_item: {:?}", impl_item);
561-
self.check_trait_or_impl_item(impl_item.id, impl_item.span);
575+
let method_sig = match impl_item.node {
576+
hir::ImplItemKind::Method(ref sig, _) => Some(sig),
577+
_ => None
578+
};
579+
self.check_trait_or_impl_item(impl_item.id, impl_item.span, method_sig);
562580
intravisit::walk_impl_item(self, impl_item)
563581
}
564582
}

src/test/compile-fail/const-eval-overflow-4b.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ use std::{u8, u16, u32, u64, usize};
2121
const A_I8_T
2222
: [u32; (i8::MAX as i8 + 1u8) as usize]
2323
//~^ ERROR constant evaluation error [E0080]
24-
//~| mismatched types
25-
//~| expected `i8`
26-
//~| found `u8`
24+
//~| expected i8, found u8
2725
= [0; (i8::MAX as usize) + 1];
2826

2927

src/test/compile-fail/discrim-ill-typed.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn f_i8() {
2626
Ok2,
2727
OhNo = 0_u8,
2828
//~^ ERROR E0080
29-
//~| mismatched types
29+
//~| expected i8, found u8
3030
}
3131

3232
let x = A::Ok;
@@ -39,7 +39,7 @@ fn f_u8() {
3939
Ok2,
4040
OhNo = 0_i8,
4141
//~^ ERROR E0080
42-
//~| mismatched types
42+
//~| expected u8, found i8
4343
}
4444

4545
let x = A::Ok;
@@ -52,7 +52,7 @@ fn f_i16() {
5252
Ok2,
5353
OhNo = 0_u16,
5454
//~^ ERROR E0080
55-
//~| mismatched types
55+
//~| expected i16, found u16
5656
}
5757

5858
let x = A::Ok;
@@ -65,7 +65,7 @@ fn f_u16() {
6565
Ok2,
6666
OhNo = 0_i16,
6767
//~^ ERROR E0080
68-
//~| mismatched types
68+
//~| expected u16, found i16
6969
}
7070

7171
let x = A::Ok;
@@ -78,7 +78,7 @@ fn f_i32() {
7878
Ok2,
7979
OhNo = 0_u32,
8080
//~^ ERROR E0080
81-
//~| mismatched types
81+
//~| expected i32, found u32
8282
}
8383

8484
let x = A::Ok;
@@ -91,7 +91,7 @@ fn f_u32() {
9191
Ok2,
9292
OhNo = 0_i32,
9393
//~^ ERROR E0080
94-
//~| mismatched types
94+
//~| expected u32, found i32
9595
}
9696

9797
let x = A::Ok;
@@ -104,7 +104,7 @@ fn f_i64() {
104104
Ok2,
105105
OhNo = 0_u64,
106106
//~^ ERROR E0080
107-
//~| mismatched types
107+
//~| expected i64, found u64
108108
}
109109

110110
let x = A::Ok;
@@ -117,7 +117,7 @@ fn f_u64() {
117117
Ok2,
118118
OhNo = 0_i64,
119119
//~^ ERROR E0080
120-
//~| mismatched types
120+
//~| expected u64, found i64
121121
}
122122

123123
let x = A::Ok;

src/test/compile-fail/explicit-self-lifetime-mismatch.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ struct Foo<'a,'b> {
1414
}
1515

1616
impl<'a,'b> Foo<'a,'b> {
17-
fn bar(self: Foo<'b,'a>) {}
18-
//~^ ERROR mismatched types
17+
fn bar(
18+
self
19+
//~^ ERROR mismatched method receiver
1920
//~| expected type `Foo<'a, 'b>`
2021
//~| found type `Foo<'b, 'a>`
2122
//~| lifetime mismatch
22-
//~| ERROR mismatched types
23+
//~| ERROR mismatched method receiver
2324
//~| expected type `Foo<'a, 'b>`
2425
//~| found type `Foo<'b, 'a>`
2526
//~| lifetime mismatch
27+
: Foo<'b,'a>) {}
2628
}
2729

2830
fn main() {}

src/test/compile-fail/issue-17740.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ struct Foo<'a> {
1414

1515
impl <'a> Foo<'a>{
1616
fn bar(self: &mut Foo) {
17-
//~^ mismatched types
17+
//~^ mismatched method receiver
1818
//~| expected type `&mut Foo<'a>`
1919
//~| found type `&mut Foo<'_>`
2020
//~| lifetime mismatch
21-
//~| mismatched types
21+
//~| mismatched method receiver
2222
//~| expected type `&mut Foo<'a>`
2323
//~| found type `&mut Foo<'_>`
2424
//~| lifetime mismatch

src/test/compile-fail/issue-26194.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct S(String);
1212

1313
impl S {
1414
fn f(self: *mut S) -> String { self.0 }
15-
//~^ ERROR mismatched types
15+
//~^ ERROR mismatched method receiver
1616
}
1717

1818
fn main() { S("".to_owned()).f(); }

src/test/compile-fail/issue-8761.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
enum Foo {
1212
A = 1i64,
1313
//~^ ERROR constant evaluation error
14-
//~| expected `isize`
15-
//~| found `i64`
14+
//~| expected isize, found i64
1615
B = 2u8
1716
//~^ ERROR constant evaluation error
18-
//~| expected `isize`
19-
//~| found `u8`
17+
//~| expected isize, found u8
2018
}
2119

2220
fn main() {}

0 commit comments

Comments
 (0)