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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 8 additions & 5 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)