Skip to content

Commit 774f2fb

Browse files
committed
Improve the error message when resolution of methods/items finds no matching impl.
1 parent bc39aab commit 774f2fb

32 files changed

+72
-70
lines changed

src/librustc_typeck/check/method/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub enum MethodError {
4040
// Did not find an applicable method, but we did find various
4141
// static methods that may apply, as well as a list of
4242
// not-in-scope traits which may work.
43-
NoMatch(Vec<CandidateSource>, Vec<ast::DefId>),
43+
NoMatch(Vec<CandidateSource>, Vec<ast::DefId>, probe::Mode),
4444

4545
// Multiple methods might apply.
4646
Ambiguity(Vec<CandidateSource>),

src/librustc_typeck/check/method/probe.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub fn probe<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
136136
let steps = if mode == Mode::MethodCall {
137137
match create_steps(fcx, span, self_ty) {
138138
Some(steps) => steps,
139-
None => return Err(MethodError::NoMatch(Vec::new(), Vec::new())),
139+
None => return Err(MethodError::NoMatch(Vec::new(), Vec::new(), mode)),
140140
}
141141
} else {
142142
vec![CandidateStep {
@@ -866,7 +866,7 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
866866
}
867867
}
868868
}).collect(),
869-
Some(Err(MethodError::NoMatch(_, others))) => {
869+
Some(Err(MethodError::NoMatch(_, others, _))) => {
870870
assert!(others.is_empty());
871871
vec![]
872872
}
@@ -877,7 +877,7 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
877877
None => vec![],
878878
};
879879

880-
Err(MethodError::NoMatch(static_candidates, out_of_scope_traits))
880+
Err(MethodError::NoMatch(static_candidates, out_of_scope_traits, self.mode))
881881
}
882882

883883
fn pick_core(&mut self) -> Option<PickResult<'tcx>> {

src/librustc_typeck/check/method/suggest.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use std::cell;
2828
use std::cmp::Ordering;
2929

3030
use super::{MethodError, CandidateSource, impl_item, trait_item};
31+
use super::probe::Mode;
3132

3233
pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
3334
span: Span,
@@ -42,17 +43,19 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
4243
}
4344

4445
match error {
45-
MethodError::NoMatch(static_sources, out_of_scope_traits) => {
46+
MethodError::NoMatch(static_sources, out_of_scope_traits, mode) => {
4647
let cx = fcx.tcx();
4748
let item_ustring = item_name.user_string(cx);
4849

4950
fcx.type_error_message(
5051
span,
5152
|actual| {
52-
format!("type `{}` does not implement any \
53-
item in scope named `{}`",
54-
actual,
55-
item_ustring)
53+
format!("no {} named `{}` found for type `{}` \
54+
in the current scope",
55+
if mode == Mode::MethodCall { "method" }
56+
else { "associated item" },
57+
item_ustring,
58+
actual)
5659
},
5760
rcvr_ty,
5861
None);

src/test/compile-fail/auto-ref-slice-plus-ref.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ fn main() {
1515
// vectors to slices then automatically create a self reference.
1616

1717
let mut a = vec!(0);
18-
a.test_mut(); //~ ERROR does not implement any item in scope named `test_mut`
19-
a.test(); //~ ERROR does not implement any item in scope named `test`
18+
a.test_mut(); //~ ERROR no method named `test_mut` found
19+
a.test(); //~ ERROR no method named `test` found
2020

21-
([1]).test(); //~ ERROR does not implement any item in scope named `test`
22-
(&[1]).test(); //~ ERROR does not implement any item in scope named `test`
21+
([1]).test(); //~ ERROR no method named `test` found
22+
(&[1]).test(); //~ ERROR no method named `test` found
2323
}
2424

2525
trait MyIter {

src/test/compile-fail/class-cast-to-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,5 @@ fn cat(in_x : usize, in_y : isize, in_name: String) -> cat {
6060

6161
fn main() {
6262
let nyan: Box<noisy> = box cat(0, 2, "nyan".to_string()) as Box<noisy>;
63-
nyan.eat(); //~ ERROR does not implement any item in scope named `eat`
63+
nyan.eat(); //~ ERROR no method named `eat` found
6464
}

src/test/compile-fail/coherence_inherent.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mod NoImport {
3838
use Lib::TheStruct;
3939

4040
fn call_the_fn(s: &TheStruct) {
41-
s.the_fn(); //~ ERROR does not implement any item in scope named `the_fn`
41+
s.the_fn(); //~ ERROR no method named `the_fn` found
4242
}
4343
}
4444

src/test/compile-fail/coherence_inherent_cc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ mod NoImport {
3030
use coherence_inherent_cc_lib::TheStruct;
3131

3232
fn call_the_fn(s: &TheStruct) {
33-
s.the_fn(); //~ ERROR does not implement any item in scope named `the_fn`
33+
s.the_fn(); //~ ERROR no method named `the_fn` found
3434
}
3535
}
3636

src/test/compile-fail/copy-a-resource.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ fn foo(i:isize) -> foo {
2626
fn main() {
2727
let x = foo(10);
2828
let _y = x.clone();
29-
//~^ ERROR does not implement any item in scope
29+
//~^ ERROR no method named `clone` found
3030
println!("{:?}", x);
3131
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub mod b {
2424
use b::B;
2525

2626
fn foo(b: &B) {
27-
b.foo(); //~ ERROR: does not implement any item in scope named
27+
b.foo(); //~ ERROR: no method named `foo` found
2828
}
2929
}
3030

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl Node for Stuff {
3131
}
3232

3333
fn iterate<N: Node, G: Graph<N>>(graph: &G) {
34-
for node in graph.iter() { //~ ERROR does not implement any item in scope named
34+
for node in graph.iter() { //~ ERROR no method named `iter` found
3535
node.zomg(); //~ error: the type of this value must be known in this context
3636
}
3737
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ struct Obj<F> where F: FnMut() -> u32 {
1414

1515
fn main() {
1616
let o = Obj { closure: || 42 };
17-
o.closure(); //~ ERROR does not implement any item in scope named `closure`
17+
o.closure(); //~ ERROR no method named `closure` found
1818
//~^ NOTE use `(s.closure)(...)` if you meant to call the function stored in the `closure` field
1919
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414
let f = 42;
1515

1616
let _g = if f < 5 {
17-
f.honk() //~ ERROR does not implement any item in scope named `honk`
17+
f.honk() //~ ERROR no method named `honk` found
1818
}
1919
else {
2020
()

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
#![feature(unboxed_closures)]
1212

1313
fn main() {
14-
"".homura()(); //~ ERROR does not implement any item
14+
"".homura()(); //~ ERROR no method named `homura` found
1515
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
struct Homura;
1212

1313
fn akemi(homura: Homura) {
14-
let Some(ref madoka) = Some(homura.kaname()); //~ ERROR does not implement any item
14+
let Some(ref madoka) = Some(homura.kaname()); //~ ERROR no method named `kaname` found
1515
madoka.clone();
1616
}
1717

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ impl<A> vec_monad<A> for Vec<A> {
2222
}
2323
fn main() {
2424
["hi"].bind(|x| [x] );
25-
//~^ ERROR type `[&str; 1]` does not implement any item in scope named `bind`
25+
//~^ ERROR no method named `bind` found for type `[&str; 1]` in the current scope
2626
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ impl Drop for C {
2020

2121
fn main() {
2222
let c = C{ x: 2};
23-
let _d = c.clone(); //~ ERROR does not implement any item in scope
23+
let _d = c.clone(); //~ ERROR no method named `clone` found
2424
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
trait A {
1212
fn a(&self) {
1313
|| self.b()
14-
//~^ ERROR type `&Self` does not implement any item in scope named `b`
14+
//~^ ERROR no method named `b` found for type `&Self` in the current scope
1515
//~| ERROR mismatched types
1616
//~| expected `()`
1717
//~| found closure

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Obj {
1717
return 1+1 == 2
1818
}
1919
pub fn chirp(&self) {
20-
self.boom(); //~ ERROR `&Obj` does not implement any item in scope named `boom`
20+
self.boom(); //~ ERROR no method named `boom` found for type `&Obj` in the current scope
2121
}
2222
}
2323

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern: type `&Foo` does not implement any item in scope named `foo`
12-
1311
trait Foo {
1412
fn foo(self: Box<Self>);
1513
}
@@ -20,4 +18,5 @@ impl Foo for isize {
2018

2119
fn main() {
2220
(&5 as &Foo).foo();
21+
//~^ ERROR: no method named `foo` found for type `&Foo` in the current scope
2322
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,15 @@ impl ManyImplTrait for Myisize {}
7171

7272
fn no_param_bound(u: usize, m: Myisize) -> usize {
7373
u.f8(42) + u.f9(342) + m.fff(42)
74-
//~^ ERROR type `usize` does not implement any item in scope named `f9`
74+
//~^ ERROR no method named `f9` found for type `usize` in the current scope
7575
//~^^ NOTE found defined static methods, maybe a `self` is missing?
76-
//~^^^ ERROR type `Myisize` does not implement any item in scope named `fff`
76+
//~^^^ ERROR no method named `fff` found for type `Myisize` in the current scope
7777
//~^^^^ NOTE found defined static methods, maybe a `self` is missing?
7878
}
7979

8080
fn param_bound<T: ManyImplTrait>(t: T) -> bool {
8181
t.is_str()
82-
//~^ ERROR type `T` does not implement any item in scope named `is_str`
82+
//~^ ERROR no method named `is_str` found for type `T` in the current scope
8383
//~^^ NOTE found defined static methods, maybe a `self` is missing?
8484
}
8585

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
struct Foo;
1414

1515
fn main() {
16-
Foo::bar(); //~ ERROR type `Foo` does not implement any item in scope named `bar`
16+
Foo::bar(); //~ ERROR no associated item named `bar` found for type `Foo` in the current scope
1717
}

src/test/compile-fail/macro-backtrace-invalid-internals.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
macro_rules! fake_method_stmt { //~ NOTE in expansion of
1414
() => {
15-
1.fake() //~ ERROR does not implement any item
15+
1.fake() //~ ERROR no method named `fake` found
1616
}
1717
}
1818

@@ -30,7 +30,7 @@ macro_rules! fake_anon_field_stmt { //~ NOTE in expansion of
3030

3131
macro_rules! fake_method_expr { //~ NOTE in expansion of
3232
() => {
33-
1.fake() //~ ERROR does not implement any item
33+
1.fake() //~ ERROR no method named `fake` found
3434
}
3535
}
3636

src/test/compile-fail/method-call-err-msg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ fn main() {
2525

2626
let y = Foo;
2727
y.zero()
28-
.take() //~ ERROR type `Foo` does not implement any item in scope named `take`
28+
.take() //~ ERROR no method named `take` found for type `Foo` in the current scope
2929
.one(0);
3030
}

src/test/compile-fail/method-suggestion-no-duplication.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn foo<F>(f: F) where F: FnMut(Foo) {}
1616

1717
fn main() {
1818
foo(|s| s.is_empty());
19-
//~^ ERROR does not implement any item
19+
//~^ ERROR no method named `is_empty` found
2020
//~^^ HELP #1: `core::slice::SliceExt`
2121
//~^^^ HELP #2: `core::str::StrExt`
2222
}

0 commit comments

Comments
 (0)