Skip to content

Commit 3d910b8

Browse files
authored
Rollup merge of rust-lang#49223 - GuillaumeGomez:propose-variant-for-E0599, r=cramertj
Propose a variant if it is an enum for E0599 Fixes rust-lang#49192.
2 parents 42de36d + 1f51840 commit 3d910b8

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/librustc_typeck/check/method/suggest.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ use rustc::traits::{Obligation, SelectionContext};
2222
use util::nodemap::FxHashSet;
2323

2424
use syntax::ast;
25+
use syntax::util::lev_distance::find_best_match_for_name;
2526
use errors::DiagnosticBuilder;
2627
use syntax_pos::Span;
2728

2829
use rustc::hir;
2930
use rustc::hir::print;
3031
use rustc::infer::type_variable::TypeVariableOrigin;
32+
use rustc::ty::TyAdt;
3133

3234
use std::cell;
3335
use std::cmp::Ordering;
@@ -179,9 +181,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
179181
let actual = self.resolve_type_vars_if_possible(&rcvr_ty);
180182
let ty_string = self.ty_to_string(actual);
181183
let is_method = mode == Mode::MethodCall;
184+
let mut suggestion = None;
182185
let type_str = if is_method {
183186
"method"
184187
} else if actual.is_enum() {
188+
if let TyAdt(ref adt_def, _) = actual.sty {
189+
let names = adt_def.variants.iter().map(|s| &s.name);
190+
suggestion = find_best_match_for_name(names,
191+
&item_name.as_str(),
192+
None);
193+
}
185194
"variant"
186195
} else {
187196
match (item_name.as_str().chars().next(), actual.is_fresh_ty()) {
@@ -256,15 +265,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
256265
err.emit();
257266
return;
258267
} else {
259-
struct_span_err!(
268+
let mut err = struct_span_err!(
260269
tcx.sess,
261270
span,
262271
E0599,
263272
"no {} named `{}` found for type `{}` in the current scope",
264273
type_str,
265274
item_name,
266275
ty_string
267-
)
276+
);
277+
if let Some(suggestion) = suggestion {
278+
err.note(&format!("did you mean `{}::{}`?", type_str, suggestion));
279+
}
280+
err
268281
}
269282
} else {
270283
tcx.sess.diagnostic().struct_dummy()

src/test/ui/issue-23217.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ LL | pub enum SomeEnum {
55
| ----------------- variant `A` not found here
66
LL | B = SomeEnum::A,
77
| ^^^^^^^^^^^ variant not found in `SomeEnum`
8+
|
9+
= note: did you mean `variant::B`?
810

911
error: aborting due to previous error
1012

src/test/ui/issue-28971.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ LL | enum Foo {
66
...
77
LL | Foo::Baz(..) => (),
88
| ^^^^^^^^^^^^ variant not found in `Foo`
9+
|
10+
= note: did you mean `variant::Bar`?
911

1012
error: aborting due to previous error
1113

0 commit comments

Comments
 (0)