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

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

Merged
merged 3 commits into from
Oct 29, 2019
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
14 changes: 11 additions & 3 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1523,9 +1523,17 @@ 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(">");
// If we've got a compound-qualified-path, let's push an additional pair of angle
// brackets, so that we pretty-print `<<A::B>::C>` as `<A::B>::C`, instead of just
// `A::B::C` (since the latter could be ambiguous to the user)
if let hir::TyKind::Path(hir::QPath::Resolved(None, _)) = &qself.kind {
self.print_type(qself);
} else {
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
Original file line number Diff line number Diff line change
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/pretty/issue-4264.pp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


((::alloc::fmt::format as
for<'r> fn(std::fmt::Arguments<'r>) -> std::string::String {std::fmt::format})(((<::core::fmt::Arguments>::new_v1
for<'r> fn(std::fmt::Arguments<'r>) -> std::string::String {std::fmt::format})(((::core::fmt::Arguments::new_v1
as
fn(&[&str], &[std::fmt::ArgumentV1<'_>]) -> std::fmt::Arguments<'_> {std::fmt::Arguments::<'_>::new_v1})((&([("test"
as
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/did_you_mean/issue-42764.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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]
}
Original file line number Diff line number Diff line change
@@ -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