From 273ee611f08e4a788da5f3e137feb68ee310a5c8 Mon Sep 17 00:00:00 2001 From: Patryk Wychowaniec Date: Fri, 18 Oct 2019 18:05:54 +0200 Subject: [PATCH] Improve the "try using a variant of the expected type" hint. --- src/librustc/hir/print.rs | 2 -- src/librustc_typeck/check/demand.rs | 9 +++++++-- src/test/ui/did_you_mean/issue-42764.rs | 2 +- src/test/ui/did_you_mean/issue-42764.stderr | 2 +- src/test/ui/error-codes/E0164.stderr | 2 +- src/test/ui/fn-in-pat.stderr | 2 +- .../fully-qualified-type-name1.stderr | 2 +- src/test/ui/issues/issue-28992-empty.rs | 2 +- src/test/ui/issues/issue-28992-empty.stderr | 2 +- src/test/ui/issues/issue-46112.stderr | 2 +- src/test/ui/issues/issue-55587.stderr | 2 +- src/test/ui/match/match-fn-call.stderr | 4 ++-- src/test/ui/methods/method-path-in-pattern.rs | 12 ++++++------ .../ui/methods/method-path-in-pattern.stderr | 12 ++++++------ src/test/ui/qualified/qualified-path-params.rs | 2 +- .../ui/qualified/qualified-path-params.stderr | 2 +- ...ariant-form-through-Self-issue-58006.stderr | 2 +- ...orrect-variant-form-through-alias-caught.rs | 10 +++++----- ...ct-variant-form-through-alias-caught.stderr | 18 +++++++++--------- 19 files changed, 47 insertions(+), 44 deletions(-) diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index b852098d4ce..b6001c30cb0 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -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(), diff --git a/src/librustc_typeck/check/demand.rs b/src/librustc_typeck/check/demand.rs index 3509d6566ec..b4e07e4a0df 100644 --- a/src/librustc_typeck/check/demand.rs +++ b/src/librustc_typeck/check/demand.rs @@ -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); } } diff --git a/src/test/ui/did_you_mean/issue-42764.rs b/src/test/ui/did_you_mean/issue-42764.rs index 5dd70aade67..700f8128a93 100644 --- a/src/test/ui/did_you_mean/issue-42764.rs +++ b/src/test/ui/did_you_mean/issue-42764.rs @@ -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 } diff --git a/src/test/ui/did_you_mean/issue-42764.stderr b/src/test/ui/did_you_mean/issue-42764.stderr index 64868c414ef..0b3e44446ae 100644 --- a/src/test/ui/did_you_mean/issue-42764.stderr +++ b/src/test/ui/did_you_mean/issue-42764.stderr @@ -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)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/test/ui/error-codes/E0164.stderr b/src/test/ui/error-codes/E0164.stderr index 4bbddb1978c..0db89dfec84 100644 --- a/src/test/ui/error-codes/E0164.stderr +++ b/src/test/ui/error-codes/E0164.stderr @@ -1,4 +1,4 @@ -error[E0164]: expected tuple struct or tuple variant, found associated constant `::B` +error[E0164]: expected tuple struct or tuple variant, found associated constant `Foo::B` --> $DIR/E0164.rs:9:9 | LL | Foo::B(i) => i, diff --git a/src/test/ui/fn-in-pat.stderr b/src/test/ui/fn-in-pat.stderr index 70f84993acf..5d6632f2fc2 100644 --- a/src/test/ui/fn-in-pat.stderr +++ b/src/test/ui/fn-in-pat.stderr @@ -1,4 +1,4 @@ -error[E0164]: expected tuple struct or tuple variant, found method `::new` +error[E0164]: expected tuple struct or tuple variant, found method `A::new` --> $DIR/fn-in-pat.rs:11:9 | LL | A::new() => (), diff --git a/src/test/ui/fully-qualified-type/fully-qualified-type-name1.stderr b/src/test/ui/fully-qualified-type/fully-qualified-type-name1.stderr index 62ded3e255a..e488b1f6b0c 100644 --- a/src/test/ui/fully-qualified-type/fully-qualified-type-name1.stderr +++ b/src/test/ui/fully-qualified-type/fully-qualified-type-name1.stderr @@ -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` found type `{integer}` diff --git a/src/test/ui/issues/issue-28992-empty.rs b/src/test/ui/issues/issue-28992-empty.rs index f61daa94c5d..74cfeb6db8e 100644 --- a/src/test/ui/issues/issue-28992-empty.rs +++ b/src/test/ui/issues/issue-28992-empty.rs @@ -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 `::C2` + //~^ ERROR expected tuple struct or tuple variant, found associated constant `S::C2` } diff --git a/src/test/ui/issues/issue-28992-empty.stderr b/src/test/ui/issues/issue-28992-empty.stderr index a4311880bcb..71f337278f2 100644 --- a/src/test/ui/issues/issue-28992-empty.stderr +++ b/src/test/ui/issues/issue-28992-empty.stderr @@ -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 `::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 {} diff --git a/src/test/ui/issues/issue-46112.stderr b/src/test/ui/issues/issue-46112.stderr index 939d945c191..07e90c56748 100644 --- a/src/test/ui/issues/issue-46112.stderr +++ b/src/test/ui/issues/issue-46112.stderr @@ -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 `()` diff --git a/src/test/ui/issues/issue-55587.stderr b/src/test/ui/issues/issue-55587.stderr index 307227e1c4d..bb0d15a23d6 100644 --- a/src/test/ui/issues/issue-55587.stderr +++ b/src/test/ui/issues/issue-55587.stderr @@ -1,4 +1,4 @@ -error[E0164]: expected tuple struct or tuple variant, found method `::new` +error[E0164]: expected tuple struct or tuple variant, found method `Path::new` --> $DIR/issue-55587.rs:4:9 | LL | let Path::new(); diff --git a/src/test/ui/match/match-fn-call.stderr b/src/test/ui/match/match-fn-call.stderr index 611904e6e91..2d7a0f16141 100644 --- a/src/test/ui/match/match-fn-call.stderr +++ b/src/test/ui/match/match-fn-call.stderr @@ -1,4 +1,4 @@ -error[E0164]: expected tuple struct or tuple variant, found method `::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 `::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"), diff --git a/src/test/ui/methods/method-path-in-pattern.rs b/src/test/ui/methods/method-path-in-pattern.rs index 49f5e09edf4..f94be1734b7 100644 --- a/src/test/ui/methods/method-path-in-pattern.rs +++ b/src/test/ui/methods/method-path-in-pattern.rs @@ -13,20 +13,20 @@ impl MyTrait for Foo {} fn main() { match 0u32 { Foo::bar => {} - //~^ ERROR expected unit struct, unit variant or constant, found method `::bar` + //~^ ERROR expected unit struct, unit variant or constant, found method `Foo::bar` } match 0u32 { ::bar => {} - //~^ ERROR expected unit struct, unit variant or constant, found method `::bar` + //~^ ERROR expected unit struct, unit variant or constant, found method `Foo::bar` } match 0u32 { ::trait_bar => {} - //~^ ERROR expected unit struct, unit variant or constant, found method `::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 `::bar` + //~^ ERROR expected unit struct, unit variant or constant, found method `Foo::bar` if let ::bar = 0u32 {} - //~^ ERROR expected unit struct, unit variant or constant, found method `::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 `::trait_bar` + //~^ ERROR expected unit struct, unit variant or constant, found method `Foo::trait_bar` } diff --git a/src/test/ui/methods/method-path-in-pattern.stderr b/src/test/ui/methods/method-path-in-pattern.stderr index b290c34d527..6b0c5946ff8 100644 --- a/src/test/ui/methods/method-path-in-pattern.stderr +++ b/src/test/ui/methods/method-path-in-pattern.stderr @@ -1,34 +1,34 @@ -error[E0533]: expected unit struct, unit variant or constant, found method `::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 `::bar` +error[E0533]: expected unit struct, unit variant or constant, found method `Foo::bar` --> $DIR/method-path-in-pattern.rs:19:9 | LL | ::bar => {} | ^^^^^^^^^^ -error[E0533]: expected unit struct, unit variant or constant, found method `::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 | ::trait_bar => {} | ^^^^^^^^^^^^^^^^ -error[E0533]: expected unit struct, unit variant or constant, found method `::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 `::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 ::bar = 0u32 {} | ^^^^^^^^^^ -error[E0533]: expected unit struct, unit variant or constant, found method `::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 {} diff --git a/src/test/ui/qualified/qualified-path-params.rs b/src/test/ui/qualified/qualified-path-params.rs index b1b60b4b73f..27cad33b553 100644 --- a/src/test/ui/qualified/qualified-path-params.rs +++ b/src/test/ui/qualified/qualified-path-params.rs @@ -18,7 +18,7 @@ fn f() {} fn main() { match 10 { ::A::f:: => {} - //~^ ERROR expected unit struct, unit variant or constant, found method `<::A>::f` + //~^ ERROR expected unit struct, unit variant or constant, found method `::A::f` 0 ..= ::A::f:: => {} //~ ERROR only char and numeric types are allowed in range } } diff --git a/src/test/ui/qualified/qualified-path-params.stderr b/src/test/ui/qualified/qualified-path-params.stderr index 92792f2e86a..54e34b50806 100644 --- a/src/test/ui/qualified/qualified-path-params.stderr +++ b/src/test/ui/qualified/qualified-path-params.stderr @@ -1,4 +1,4 @@ -error[E0533]: expected unit struct, unit variant or constant, found method `<::A>::f` +error[E0533]: expected unit struct, unit variant or constant, found method `::A::f` --> $DIR/qualified-path-params.rs:20:9 | LL | ::A::f:: => {} diff --git a/src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-Self-issue-58006.stderr b/src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-Self-issue-58006.stderr index cfe273b9dd2..15d15f2f40d 100644 --- a/src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-Self-issue-58006.stderr +++ b/src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-Self-issue-58006.stderr @@ -1,4 +1,4 @@ -error[E0533]: expected unit struct, unit variant or constant, found tuple variant `::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 => (), diff --git a/src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-alias-caught.rs b/src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-alias-caught.rs index efdbebf2662..5772450477c 100644 --- a/src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-alias-caught.rs +++ b/src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-alias-caught.rs @@ -8,14 +8,14 @@ enum Enum { Braced {}, Unit, Tuple() } fn main() { Alias::Braced; - //~^ ERROR expected unit struct, unit variant or constant, found struct variant `::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 `::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 `::Braced` [E0164] + //~^ ERROR expected tuple struct or tuple variant, found struct variant `Alias::Braced` [E0164] Alias::Unit(); - //~^ ERROR expected function, found enum variant `::Unit` + //~^ ERROR expected function, found enum variant `Alias::Unit` let Alias::Unit() = panic!(); - //~^ ERROR expected tuple struct or tuple variant, found unit variant `::Unit` [E0164] + //~^ ERROR expected tuple struct or tuple variant, found unit variant `Alias::Unit` [E0164] } diff --git a/src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-alias-caught.stderr b/src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-alias-caught.stderr index 17efc08c632..b0de3ee42e3 100644 --- a/src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-alias-caught.stderr +++ b/src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-alias-caught.stderr @@ -1,38 +1,38 @@ -error[E0533]: expected unit struct, unit variant or constant, found struct variant `::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 `::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 `::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 `::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() } - | ---- `::Unit` defined here + | ---- `Alias::Unit` defined here ... LL | Alias::Unit(); | ^^^^^^^^^^^-- | | | call expression requires function | -help: `::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 | ::Unit; - | ^^^^^^^^^^^^^ +LL | Alias::Unit; + | ^^^^^^^^^^^ -error[E0164]: expected tuple struct or tuple variant, found unit variant `::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!();