Skip to content

Commit

Permalink
Propose a variant if it is an enum for E0599
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Mar 20, 2018
1 parent 75af15e commit 1f51840
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ use rustc::traits::{Obligation, SelectionContext};
use util::nodemap::FxHashSet;

use syntax::ast;
use syntax::util::lev_distance::find_best_match_for_name;
use errors::DiagnosticBuilder;
use syntax_pos::Span;

use rustc::hir;
use rustc::hir::print;
use rustc::infer::type_variable::TypeVariableOrigin;
use rustc::ty::TyAdt;

use std::cell;
use std::cmp::Ordering;
Expand Down Expand Up @@ -179,9 +181,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let actual = self.resolve_type_vars_if_possible(&rcvr_ty);
let ty_string = self.ty_to_string(actual);
let is_method = mode == Mode::MethodCall;
let mut suggestion = None;
let type_str = if is_method {
"method"
} else if actual.is_enum() {
if let TyAdt(ref adt_def, _) = actual.sty {
let names = adt_def.variants.iter().map(|s| &s.name);
suggestion = find_best_match_for_name(names,
&item_name.as_str(),
None);
}
"variant"
} else {
match (item_name.as_str().chars().next(), actual.is_fresh_ty()) {
Expand Down Expand Up @@ -256,15 +265,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
err.emit();
return;
} else {
struct_span_err!(
let mut err = struct_span_err!(
tcx.sess,
span,
E0599,
"no {} named `{}` found for type `{}` in the current scope",
type_str,
item_name,
ty_string
)
);
if let Some(suggestion) = suggestion {
err.note(&format!("did you mean `{}::{}`?", type_str, suggestion));
}
err
}
} else {
tcx.sess.diagnostic().struct_dummy()
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/issue-23217.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ LL | pub enum SomeEnum {
| ----------------- variant `A` not found here
LL | B = SomeEnum::A,
| ^^^^^^^^^^^ variant not found in `SomeEnum`
|
= note: did you mean `variant::B`?

error: aborting due to previous error

Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/issue-28971.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ LL | enum Foo {
...
LL | Foo::Baz(..) => (),
| ^^^^^^^^^^^^ variant not found in `Foo`
|
= note: did you mean `variant::Bar`?

error: aborting due to previous error

Expand Down

0 comments on commit 1f51840

Please sign in to comment.