Skip to content

Commit

Permalink
Improve the "try using a variant of the expected type" hint.
Browse files Browse the repository at this point in the history
  • Loading branch information
Patryk27 committed Oct 28, 2019
1 parent 8d78bf6 commit 273ee61
Show file tree
Hide file tree
Showing 19 changed files with 47 additions and 44 deletions.
2 changes: 0 additions & 2 deletions src/librustc/hir/print.rs
Expand Up @@ -1523,9 +1523,7 @@ impl<'a> State<'a> {
colons_before_params)
}
hir::QPath::TypeRelative(ref qself, ref item_segment) => {
self.s.word("<");
self.print_type(qself);
self.s.word(">");
self.s.word("::");
self.print_ident(item_segment.ident);
self.print_generic_args(item_segment.generic_args(),
Expand Down
9 changes: 7 additions & 2 deletions src/librustc_typeck/check/demand.rs
Expand Up @@ -172,10 +172,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}).peekable();

if compatible_variants.peek().is_some() {
let expr_text = print::to_string(print::NO_ANN, |s| s.print_expr(expr));
let expr_text = self.tcx.sess
.source_map()
.span_to_snippet(expr.span)
.unwrap_or_else(|_| {
print::to_string(print::NO_ANN, |s| s.print_expr(expr))
});
let suggestions = compatible_variants
.map(|v| format!("{}({})", v, expr_text));
let msg = "try using a variant of the expected type";
let msg = "try using a variant of the expected enum";
err.span_suggestions(expr.span, msg, suggestions, Applicability::MaybeIncorrect);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/issue-42764.rs
Expand Up @@ -10,7 +10,7 @@ fn main() {
let n: usize = 42;
this_function_expects_a_double_option(n);
//~^ ERROR mismatched types
//~| HELP try using a variant of the expected type
//~| HELP try using a variant of the expected enum
}


Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/issue-42764.stderr
Expand Up @@ -6,7 +6,7 @@ LL | this_function_expects_a_double_option(n);
|
= note: expected type `DoubleOption<_>`
found type `usize`
help: try using a variant of the expected type
help: try using a variant of the expected enum
|
LL | this_function_expects_a_double_option(DoubleOption::FirstSome(n));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/error-codes/E0164.stderr
@@ -1,4 +1,4 @@
error[E0164]: expected tuple struct or tuple variant, found associated constant `<Foo>::B`
error[E0164]: expected tuple struct or tuple variant, found associated constant `Foo::B`
--> $DIR/E0164.rs:9:9
|
LL | Foo::B(i) => i,
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/fn-in-pat.stderr
@@ -1,4 +1,4 @@
error[E0164]: expected tuple struct or tuple variant, found method `<A>::new`
error[E0164]: expected tuple struct or tuple variant, found method `A::new`
--> $DIR/fn-in-pat.rs:11:9
|
LL | A::new() => (),
Expand Down
Expand Up @@ -5,7 +5,7 @@ LL | x = 5;
| ^
| |
| expected enum `std::option::Option`, found integer
| help: try using a variant of the expected type: `Some(5)`
| help: try using a variant of the expected enum: `Some(5)`
|
= note: expected type `std::option::Option<usize>`
found type `{integer}`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-28992-empty.rs
Expand Up @@ -12,5 +12,5 @@ impl S {
fn main() {
if let C1(..) = 0 {} //~ ERROR expected tuple struct or tuple variant, found constant `C1`
if let S::C2(..) = 0 {}
//~^ ERROR expected tuple struct or tuple variant, found associated constant `<S>::C2`
//~^ ERROR expected tuple struct or tuple variant, found associated constant `S::C2`
}
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-28992-empty.stderr
Expand Up @@ -4,7 +4,7 @@ error[E0532]: expected tuple struct or tuple variant, found constant `C1`
LL | if let C1(..) = 0 {}
| ^^ not a tuple struct or tuple variant

error[E0164]: expected tuple struct or tuple variant, found associated constant `<S>::C2`
error[E0164]: expected tuple struct or tuple variant, found associated constant `S::C2`
--> $DIR/issue-28992-empty.rs:14:12
|
LL | if let S::C2(..) = 0 {}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-46112.stderr
Expand Up @@ -5,7 +5,7 @@ LL | fn main() { test(Ok(())); }
| ^^
| |
| expected enum `std::option::Option`, found ()
| help: try using a variant of the expected type: `Some(())`
| help: try using a variant of the expected enum: `Some(())`
|
= note: expected type `std::option::Option<()>`
found type `()`
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-55587.stderr
@@ -1,4 +1,4 @@
error[E0164]: expected tuple struct or tuple variant, found method `<Path>::new`
error[E0164]: expected tuple struct or tuple variant, found method `Path::new`
--> $DIR/issue-55587.rs:4:9
|
LL | let Path::new();
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/match/match-fn-call.stderr
@@ -1,12 +1,12 @@
error[E0164]: expected tuple struct or tuple variant, found method `<Path>::new`
error[E0164]: expected tuple struct or tuple variant, found method `Path::new`
--> $DIR/match-fn-call.rs:6:9
|
LL | Path::new("foo") => println!("foo"),
| ^^^^^^^^^^^^^^^^ `fn` calls are not allowed in patterns
|
= help: for more information, visit https://doc.rust-lang.org/book/ch18-00-patterns.html

error[E0164]: expected tuple struct or tuple variant, found method `<Path>::new`
error[E0164]: expected tuple struct or tuple variant, found method `Path::new`
--> $DIR/match-fn-call.rs:8:9
|
LL | Path::new("bar") => println!("bar"),
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/methods/method-path-in-pattern.rs
Expand Up @@ -13,20 +13,20 @@ impl MyTrait for Foo {}
fn main() {
match 0u32 {
Foo::bar => {}
//~^ ERROR expected unit struct, unit variant or constant, found method `<Foo>::bar`
//~^ ERROR expected unit struct, unit variant or constant, found method `Foo::bar`
}
match 0u32 {
<Foo>::bar => {}
//~^ ERROR expected unit struct, unit variant or constant, found method `<Foo>::bar`
//~^ ERROR expected unit struct, unit variant or constant, found method `Foo::bar`
}
match 0u32 {
<Foo>::trait_bar => {}
//~^ ERROR expected unit struct, unit variant or constant, found method `<Foo>::trait_bar`
//~^ ERROR expected unit struct, unit variant or constant, found method `Foo::trait_bar`
}
if let Foo::bar = 0u32 {}
//~^ ERROR expected unit struct, unit variant or constant, found method `<Foo>::bar`
//~^ ERROR expected unit struct, unit variant or constant, found method `Foo::bar`
if let <Foo>::bar = 0u32 {}
//~^ ERROR expected unit struct, unit variant or constant, found method `<Foo>::bar`
//~^ ERROR expected unit struct, unit variant or constant, found method `Foo::bar`
if let Foo::trait_bar = 0u32 {}
//~^ ERROR expected unit struct, unit variant or constant, found method `<Foo>::trait_bar`
//~^ ERROR expected unit struct, unit variant or constant, found method `Foo::trait_bar`
}
12 changes: 6 additions & 6 deletions src/test/ui/methods/method-path-in-pattern.stderr
@@ -1,34 +1,34 @@
error[E0533]: expected unit struct, unit variant or constant, found method `<Foo>::bar`
error[E0533]: expected unit struct, unit variant or constant, found method `Foo::bar`
--> $DIR/method-path-in-pattern.rs:15:9
|
LL | Foo::bar => {}
| ^^^^^^^^

error[E0533]: expected unit struct, unit variant or constant, found method `<Foo>::bar`
error[E0533]: expected unit struct, unit variant or constant, found method `Foo::bar`
--> $DIR/method-path-in-pattern.rs:19:9
|
LL | <Foo>::bar => {}
| ^^^^^^^^^^

error[E0533]: expected unit struct, unit variant or constant, found method `<Foo>::trait_bar`
error[E0533]: expected unit struct, unit variant or constant, found method `Foo::trait_bar`
--> $DIR/method-path-in-pattern.rs:23:9
|
LL | <Foo>::trait_bar => {}
| ^^^^^^^^^^^^^^^^

error[E0533]: expected unit struct, unit variant or constant, found method `<Foo>::bar`
error[E0533]: expected unit struct, unit variant or constant, found method `Foo::bar`
--> $DIR/method-path-in-pattern.rs:26:12
|
LL | if let Foo::bar = 0u32 {}
| ^^^^^^^^

error[E0533]: expected unit struct, unit variant or constant, found method `<Foo>::bar`
error[E0533]: expected unit struct, unit variant or constant, found method `Foo::bar`
--> $DIR/method-path-in-pattern.rs:28:12
|
LL | if let <Foo>::bar = 0u32 {}
| ^^^^^^^^^^

error[E0533]: expected unit struct, unit variant or constant, found method `<Foo>::trait_bar`
error[E0533]: expected unit struct, unit variant or constant, found method `Foo::trait_bar`
--> $DIR/method-path-in-pattern.rs:30:12
|
LL | if let Foo::trait_bar = 0u32 {}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/qualified/qualified-path-params.rs
Expand Up @@ -18,7 +18,7 @@ impl S {
fn main() {
match 10 {
<S as Tr>::A::f::<u8> => {}
//~^ ERROR expected unit struct, unit variant or constant, found method `<<S as Tr>::A>::f<u8>`
//~^ ERROR expected unit struct, unit variant or constant, found method `<S as Tr>::A::f<u8>`
0 ..= <S as Tr>::A::f::<u8> => {} //~ ERROR only char and numeric types are allowed in range
}
}
2 changes: 1 addition & 1 deletion src/test/ui/qualified/qualified-path-params.stderr
@@ -1,4 +1,4 @@
error[E0533]: expected unit struct, unit variant or constant, found method `<<S as Tr>::A>::f<u8>`
error[E0533]: expected unit struct, unit variant or constant, found method `<S as Tr>::A::f<u8>`
--> $DIR/qualified-path-params.rs:20:9
|
LL | <S as Tr>::A::f::<u8> => {}
Expand Down
@@ -1,4 +1,4 @@
error[E0533]: expected unit struct, unit variant or constant, found tuple variant `<Self>::A`
error[E0533]: expected unit struct, unit variant or constant, found tuple variant `Self::A`
--> $DIR/incorrect-variant-form-through-Self-issue-58006.rs:8:13
|
LL | Self::A => (),
Expand Down
Expand Up @@ -8,14 +8,14 @@ type Alias = Enum;

fn main() {
Alias::Braced;
//~^ ERROR expected unit struct, unit variant or constant, found struct variant `<Alias>::Braced` [E0533]
//~^ ERROR expected unit struct, unit variant or constant, found struct variant `Alias::Braced` [E0533]
let Alias::Braced = panic!();
//~^ ERROR expected unit struct, unit variant or constant, found struct variant `<Alias>::Braced` [E0533]
//~^ ERROR expected unit struct, unit variant or constant, found struct variant `Alias::Braced` [E0533]
let Alias::Braced(..) = panic!();
//~^ ERROR expected tuple struct or tuple variant, found struct variant `<Alias>::Braced` [E0164]
//~^ ERROR expected tuple struct or tuple variant, found struct variant `Alias::Braced` [E0164]

Alias::Unit();
//~^ ERROR expected function, found enum variant `<Alias>::Unit`
//~^ ERROR expected function, found enum variant `Alias::Unit`
let Alias::Unit() = panic!();
//~^ ERROR expected tuple struct or tuple variant, found unit variant `<Alias>::Unit` [E0164]
//~^ ERROR expected tuple struct or tuple variant, found unit variant `Alias::Unit` [E0164]
}
@@ -1,38 +1,38 @@
error[E0533]: expected unit struct, unit variant or constant, found struct variant `<Alias>::Braced`
error[E0533]: expected unit struct, unit variant or constant, found struct variant `Alias::Braced`
--> $DIR/incorrect-variant-form-through-alias-caught.rs:10:5
|
LL | Alias::Braced;
| ^^^^^^^^^^^^^

error[E0533]: expected unit struct, unit variant or constant, found struct variant `<Alias>::Braced`
error[E0533]: expected unit struct, unit variant or constant, found struct variant `Alias::Braced`
--> $DIR/incorrect-variant-form-through-alias-caught.rs:12:9
|
LL | let Alias::Braced = panic!();
| ^^^^^^^^^^^^^

error[E0164]: expected tuple struct or tuple variant, found struct variant `<Alias>::Braced`
error[E0164]: expected tuple struct or tuple variant, found struct variant `Alias::Braced`
--> $DIR/incorrect-variant-form-through-alias-caught.rs:14:9
|
LL | let Alias::Braced(..) = panic!();
| ^^^^^^^^^^^^^^^^^ not a tuple variant or struct

error[E0618]: expected function, found enum variant `<Alias>::Unit`
error[E0618]: expected function, found enum variant `Alias::Unit`
--> $DIR/incorrect-variant-form-through-alias-caught.rs:17:5
|
LL | enum Enum { Braced {}, Unit, Tuple() }
| ---- `<Alias>::Unit` defined here
| ---- `Alias::Unit` defined here
...
LL | Alias::Unit();
| ^^^^^^^^^^^--
| |
| call expression requires function
|
help: `<Alias>::Unit` is a unit variant, you need to write it without the parenthesis
help: `Alias::Unit` is a unit variant, you need to write it without the parenthesis
|
LL | <Alias>::Unit;
| ^^^^^^^^^^^^^
LL | Alias::Unit;
| ^^^^^^^^^^^

error[E0164]: expected tuple struct or tuple variant, found unit variant `<Alias>::Unit`
error[E0164]: expected tuple struct or tuple variant, found unit variant `Alias::Unit`
--> $DIR/incorrect-variant-form-through-alias-caught.rs:19:9
|
LL | let Alias::Unit() = panic!();
Expand Down

0 comments on commit 273ee61

Please sign in to comment.