Improve the "try using a variant of the expected type" hint.

This commit is contained in:
Patryk Wychowaniec 2019-10-18 18:05:54 +02:00
parent 8d78bf6b27
commit 273ee611f0
No known key found for this signature in database
GPG Key ID: F62547D075E09767
19 changed files with 47 additions and 44 deletions

View File

@ -1523,9 +1523,7 @@ pub fn print_qpath(&mut self,
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(),

View File

@ -172,10 +172,15 @@ fn suggest_compatible_variants(
}).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);
}
}

View File

@ -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
}

View File

@ -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));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -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,

View File

@ -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() => (),

View File

@ -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}`

View File

@ -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`
}

View File

@ -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 {}

View File

@ -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 `()`

View File

@ -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();

View File

@ -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/match-fn-call.rs:6:9
|
LL | Path::new("foo") => println!("foo"),
@ -6,7 +6,7 @@ LL | Path::new("foo") => println!("foo"),
|
= 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"),

View File

@ -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`
}

View File

@ -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 {}

View File

@ -18,7 +18,7 @@ fn f<T>() {}
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
}
}

View File

@ -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> => {}

View File

@ -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 => (),

View File

@ -8,14 +8,14 @@ enum Enum { Braced {}, Unit, Tuple() }
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]
}

View File

@ -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!();