Skip to content

Commit f796fcd

Browse files
committed
Point to ADT definition when not finding variant, method, assoc type
1 parent 6a972cd commit f796fcd

19 files changed

+285
-104
lines changed

src/librustc_typeck/check/method/suggest.rs

+42-23
Original file line numberDiff line numberDiff line change
@@ -175,33 +175,50 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
175175
let tcx = self.tcx;
176176

177177
let actual = self.resolve_type_vars_if_possible(&rcvr_ty);
178+
let ty_string = self.ty_to_string(actual);
179+
let type_str = if mode == Mode::MethodCall {
180+
"method"
181+
} else if actual.is_enum() {
182+
"variant"
183+
} else {
184+
match (item_name.as_str().chars().next(), actual.is_fresh_ty()) {
185+
(Some(name), false) if name.is_lowercase() => {
186+
"function or associated item"
187+
}
188+
(Some(_), false) => "associated item",
189+
(Some(_), true) | (None, false) => {
190+
"variant or associated item"
191+
}
192+
(None, true) => "variant",
193+
}
194+
};
178195
let mut err = if !actual.references_error() {
179-
struct_span_err!(tcx.sess, span, E0599,
180-
"no {} named `{}` found for type `{}` in the \
181-
current scope",
182-
if mode == Mode::MethodCall {
183-
"method"
184-
} else if actual.is_enum() {
185-
"variant"
186-
} else {
187-
let fresh_ty = actual.is_fresh_ty();
188-
match (item_name.as_str().chars().next(), fresh_ty) {
189-
(Some(name), false) if name.is_lowercase() => {
190-
"function or associated item"
191-
}
192-
(Some(_), false) => "associated item",
193-
(Some(_), true) | (None, false) => {
194-
"variant or associated item"
195-
}
196-
(None, true) => "variant",
197-
}
198-
},
199-
item_name,
200-
self.ty_to_string(actual))
196+
struct_span_err!(
197+
tcx.sess,
198+
span,
199+
E0599,
200+
"no {} named `{}` found for type `{}` in the current scope",
201+
type_str,
202+
item_name,
203+
ty_string
204+
)
201205
} else {
202-
self.tcx.sess.diagnostic().struct_dummy()
206+
tcx.sess.diagnostic().struct_dummy()
203207
};
204208

209+
if let Some(def) = actual.ty_adt_def() {
210+
let full_sp = tcx.def_span(def.did);
211+
let def_sp = tcx.sess.codemap().def_span(full_sp);
212+
err.span_label(def_sp, format!("{} `{}` not found {}",
213+
type_str,
214+
item_name,
215+
if def.is_enum() {
216+
"here"
217+
} else {
218+
"for this"
219+
}));
220+
}
221+
205222
// If the method name is the name of a field with a function or closure type,
206223
// give a helping note that it has to be called as (x.f)(...).
207224
if let Some(expr) = rcvr_expr {
@@ -243,6 +260,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
243260
_ => {}
244261
}
245262
}
263+
} else {
264+
err.span_label(span, format!("{} not found in `{}`", type_str, ty_string));
246265
}
247266

248267
if self.is_fn_ty(&rcvr_ty, span) {

src/test/compile-fail/bogus-tag.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
enum color { rgb(isize, isize, isize), rgba(isize, isize, isize, isize), }
13+
//~^ NOTE variant `hsl` not found here
1314

1415
fn main() {
1516
let red: color = color::rgb(255, 0, 0);

src/test/compile-fail/issue-22933-2.rs

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

11-
enum Delicious {
11+
enum Delicious { //~ NOTE variant `PIE` not found here
1212
Pie = 0x1,
1313
Apple = 0x2,
1414
ApplePie = Delicious::Apple as isize | Delicious::PIE as isize,

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

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
enum Token { LeftParen, RightParen, Plus, Minus, /* etc */ }
12+
//~^ NOTE variant `Homura` not found here
1213

1314
fn use_token(token: &Token) { unimplemented!() }
1415

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

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

11-
pub enum SomeEnum {
11+
pub enum SomeEnum { //~ NOTE variant `A` not found here
1212
B = SomeEnum::A,
1313
//~^ ERROR no variant named `A` found for type `SomeEnum`
1414
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// This should not cause an ICE
1212

13-
enum Foo {
13+
enum Foo { //~ NOTE variant `Baz` not found here
1414
Bar(u8)
1515
}
1616
fn main(){

src/test/ui/impl-trait/issue-21659-show-relevant-trait-impls-3.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0599]: no method named `foo` found for type `Bar` in the current scope
22
--> $DIR/issue-21659-show-relevant-trait-impls-3.rs:30:8
33
|
4+
23 | struct Bar;
5+
| ----------- method `foo` not found for this
6+
...
47
30 | f1.foo(1usize);
58
| ^^^
69
|

src/test/ui/impl-trait/method-suggestion-no-duplication.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0599]: no method named `is_empty` found for type `Foo` in the current scope
22
--> $DIR/method-suggestion-no-duplication.rs:19:15
33
|
4+
14 | struct Foo;
5+
| ----------- method `is_empty` not found for this
6+
...
47
19 | foo(|s| s.is_empty());
58
| ^^^^^^^^
69
|

0 commit comments

Comments
 (0)