Skip to content

Commit

Permalink
Suggest correct enum variant on typo
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Nov 25, 2018
1 parent 4632cf2 commit 6b338e0
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
29 changes: 28 additions & 1 deletion src/librustc_typeck/astconv.rs
Expand Up @@ -36,8 +36,9 @@ use lint;

use std::iter;
use syntax::ast;
use syntax::ptr::P;
use syntax::feature_gate::{GateIssue, emit_feature_err};
use syntax::ptr::P;
use syntax::util::lev_distance::find_best_match_for_name;
use syntax_pos::{DUMMY_SP, Span, MultiSpan};

pub trait AstConv<'gcx, 'tcx> {
Expand Down Expand Up @@ -1303,6 +1304,32 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx>+'o {
Err(ErrorReported) => return (tcx.types.err, Def::Err),
}
}
(&ty::Adt(adt_def, _substs), Def::Enum(_did)) => {
let ty_str = ty.to_string();
// Incorrect enum variant
let mut err = tcx.sess.struct_span_err(
span,
&format!("no variant `{}` on enum `{}`", &assoc_name.as_str(), ty_str),
);
// Check if it was a typo
let input = adt_def.variants.iter().map(|variant| &variant.name);
if let Some(suggested_name) = find_best_match_for_name(
input,
&assoc_name.as_str(),
None,
) {
err.span_suggestion_with_applicability(
span,
"did you mean",
format!("{}::{}", ty_str, suggested_name.to_string()),
Applicability::MaybeIncorrect,
);
} else {
err.span_label(span, "unknown variant");
}
err.emit();
return (tcx.types.err, Def::Err);
}
_ => {
// Don't print TyErr to the user.
if !ty.references_error() {
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-34209.rs
Expand Up @@ -14,8 +14,8 @@ enum S {

fn bug(l: S) {
match l {
S::B{ } => { },
//~^ ERROR ambiguous associated type
S::B { } => { },
//~^ ERROR no variant `B` on enum `S`
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/test/ui/issues/issue-34209.stderr
@@ -1,9 +1,8 @@
error[E0223]: ambiguous associated type
error: no variant `B` on enum `S`
--> $DIR/issue-34209.rs:17:9
|
LL | S::B{ } => { },
| ^^^^ help: use fully-qualified syntax: `<S as Trait>::B`
LL | S::B { } => { },
| ^^^^ help: did you mean: `S::A`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0223`.
15 changes: 15 additions & 0 deletions src/test/ui/suggestions/suggest-variants.rs
@@ -0,0 +1,15 @@
#[derive(Debug)]
enum Shape {
Square { size: i32 },
Circle { radius: i32 },
}

struct S {
x: usize,
}

fn main() {
println!("My shape is {:?}", Shape::Squareee { size: 5});
println!("My shape is {:?}", Shape::Circl { size: 5});
println!("My shape is {:?}", Shape::Rombus{ size: 5});
}
20 changes: 20 additions & 0 deletions src/test/ui/suggestions/suggest-variants.stderr
@@ -0,0 +1,20 @@
error: no variant `Squareee` on enum `Shape`
--> $DIR/suggest-variants.rs:12:34
|
LL | println!("My shape is {:?}", Shape::Squareee { size: 5});
| ^^^^^^^^^^^^^^^ help: did you mean: `Shape::Square`

error: no variant `Circl` on enum `Shape`
--> $DIR/suggest-variants.rs:13:34
|
LL | println!("My shape is {:?}", Shape::Circl { size: 5});
| ^^^^^^^^^^^^ help: did you mean: `Shape::Circle`

error: no variant `Rombus` on enum `Shape`
--> $DIR/suggest-variants.rs:14:34
|
LL | println!("My shape is {:?}", Shape::Rombus{ size: 5});
| ^^^^^^^^^^^^^ unknown variant

error: aborting due to 3 previous errors

0 comments on commit 6b338e0

Please sign in to comment.