Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propose a variant if it is an enum for E0599 #49223

Merged
merged 1 commit into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this a span_suggestion call?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did at first, the rendering is ugly because we use the same span.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

span_suggestion_short helps in many such cases, maybe here, too?

}
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