diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-10-29 04:08:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-29 04:08:21 +0100 |
| commit | 9c5b6b2201a3af818a6efb0650b1622a342f40f5 (patch) | |
| tree | 8380765a6f83c8190332c0e360ebbc0d1d79c6c5 | |
| parent | ffc6225cd2db1d9634259aef38fd0197b12aacf9 (diff) | |
| parent | 5c023d68d8b54d651e1775a69e999503ae5b2b30 (diff) | |
| download | rust-9c5b6b2201a3af818a6efb0650b1622a342f40f5.tar.gz rust-9c5b6b2201a3af818a6efb0650b1622a342f40f5.zip | |
Rollup merge of #65562 - Patryk27:master, r=estebank
Improve the "try using a variant of the expected type" hint. Fix https://github.com/rust-lang/rust/issues/65494. - Change type-printing output. - Use `span_to_snippet` when possible. - Change the message to `try using a variant of the expected enum`
18 files changed, 57 insertions, 44 deletions
diff --git a/src/librustc/hir/print.rs b/src/librustc/hir/print.rs index b852098d4ce..64b355f6ec9 100644 --- a/src/librustc/hir/print.rs +++ b/src/librustc/hir/print.rs @@ -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(), 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 @@ 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); } } diff --git a/src/test/pretty/issue-4264.pp b/src/test/pretty/issue-4264.pp index 4cf2e90e635..b545146c964 100644 --- a/src/test/pretty/issue-4264.pp +++ b/src/test/pretty/issue-4264.pp @@ -30,7 +30,7 @@ pub fn bar() ({ ((::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 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 `<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, 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 `<A>::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<usize>` 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 `<S>::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 `<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 {} 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 `<Path>::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 `<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"), 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 `<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` } 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 `<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 {} 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 `<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 => (), 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 @@ 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] } 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 `<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!(); |
