Skip to content

Commit 9c5b6b2

Browse files
authoredOct 29, 2019
Rollup merge of rust-lang#65562 - Patryk27:master, r=estebank
Improve the "try using a variant of the expected type" hint. Fix rust-lang#65494. - Change type-printing output. - Use `span_to_snippet` when possible. - Change the message to `try using a variant of the expected enum`
2 parents ffc6225 + 5c023d6 commit 9c5b6b2

18 files changed

+57
-44
lines changed
 

‎src/librustc/hir/print.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -1523,9 +1523,17 @@ impl<'a> State<'a> {
15231523
colons_before_params)
15241524
}
15251525
hir::QPath::TypeRelative(ref qself, ref item_segment) => {
1526-
self.s.word("<");
1527-
self.print_type(qself);
1528-
self.s.word(">");
1526+
// If we've got a compound-qualified-path, let's push an additional pair of angle
1527+
// brackets, so that we pretty-print `<<A::B>::C>` as `<A::B>::C`, instead of just
1528+
// `A::B::C` (since the latter could be ambiguous to the user)
1529+
if let hir::TyKind::Path(hir::QPath::Resolved(None, _)) = &qself.kind {
1530+
self.print_type(qself);
1531+
} else {
1532+
self.s.word("<");
1533+
self.print_type(qself);
1534+
self.s.word(">");
1535+
}
1536+
15291537
self.s.word("::");
15301538
self.print_ident(item_segment.ident);
15311539
self.print_generic_args(item_segment.generic_args(),

‎src/librustc_typeck/check/demand.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
172172
}).peekable();
173173

174174
if compatible_variants.peek().is_some() {
175-
let expr_text = print::to_string(print::NO_ANN, |s| s.print_expr(expr));
175+
let expr_text = self.tcx.sess
176+
.source_map()
177+
.span_to_snippet(expr.span)
178+
.unwrap_or_else(|_| {
179+
print::to_string(print::NO_ANN, |s| s.print_expr(expr))
180+
});
176181
let suggestions = compatible_variants
177182
.map(|v| format!("{}({})", v, expr_text));
178-
let msg = "try using a variant of the expected type";
183+
let msg = "try using a variant of the expected enum";
179184
err.span_suggestions(expr.span, msg, suggestions, Applicability::MaybeIncorrect);
180185
}
181186
}

‎src/test/pretty/issue-4264.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131

3232
((::alloc::fmt::format as
33-
for<'r> fn(std::fmt::Arguments<'r>) -> std::string::String {std::fmt::format})(((<::core::fmt::Arguments>::new_v1
33+
for<'r> fn(std::fmt::Arguments<'r>) -> std::string::String {std::fmt::format})(((::core::fmt::Arguments::new_v1
3434
as
3535
fn(&[&str], &[std::fmt::ArgumentV1<'_>]) -> std::fmt::Arguments<'_> {std::fmt::Arguments::<'_>::new_v1})((&([("test"
3636
as

‎src/test/ui/did_you_mean/issue-42764.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main() {
1010
let n: usize = 42;
1111
this_function_expects_a_double_option(n);
1212
//~^ ERROR mismatched types
13-
//~| HELP try using a variant of the expected type
13+
//~| HELP try using a variant of the expected enum
1414
}
1515

1616

‎src/test/ui/did_you_mean/issue-42764.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | this_function_expects_a_double_option(n);
66
|
77
= note: expected type `DoubleOption<_>`
88
found type `usize`
9-
help: try using a variant of the expected type
9+
help: try using a variant of the expected enum
1010
|
1111
LL | this_function_expects_a_double_option(DoubleOption::FirstSome(n));
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

‎src/test/ui/error-codes/E0164.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0164]: expected tuple struct or tuple variant, found associated constant `<Foo>::B`
1+
error[E0164]: expected tuple struct or tuple variant, found associated constant `Foo::B`
22
--> $DIR/E0164.rs:9:9
33
|
44
LL | Foo::B(i) => i,

‎src/test/ui/fn-in-pat.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0164]: expected tuple struct or tuple variant, found method `<A>::new`
1+
error[E0164]: expected tuple struct or tuple variant, found method `A::new`
22
--> $DIR/fn-in-pat.rs:11:9
33
|
44
LL | A::new() => (),

‎src/test/ui/fully-qualified-type/fully-qualified-type-name1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | x = 5;
55
| ^
66
| |
77
| expected enum `std::option::Option`, found integer
8-
| help: try using a variant of the expected type: `Some(5)`
8+
| help: try using a variant of the expected enum: `Some(5)`
99
|
1010
= note: expected type `std::option::Option<usize>`
1111
found type `{integer}`

‎src/test/ui/issues/issue-28992-empty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ impl S {
1212
fn main() {
1313
if let C1(..) = 0 {} //~ ERROR expected tuple struct or tuple variant, found constant `C1`
1414
if let S::C2(..) = 0 {}
15-
//~^ ERROR expected tuple struct or tuple variant, found associated constant `<S>::C2`
15+
//~^ ERROR expected tuple struct or tuple variant, found associated constant `S::C2`
1616
}

‎src/test/ui/issues/issue-28992-empty.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0532]: expected tuple struct or tuple variant, found constant `C1`
44
LL | if let C1(..) = 0 {}
55
| ^^ not a tuple struct or tuple variant
66

7-
error[E0164]: expected tuple struct or tuple variant, found associated constant `<S>::C2`
7+
error[E0164]: expected tuple struct or tuple variant, found associated constant `S::C2`
88
--> $DIR/issue-28992-empty.rs:14:12
99
|
1010
LL | if let S::C2(..) = 0 {}

‎src/test/ui/issues/issue-46112.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | fn main() { test(Ok(())); }
55
| ^^
66
| |
77
| expected enum `std::option::Option`, found ()
8-
| help: try using a variant of the expected type: `Some(())`
8+
| help: try using a variant of the expected enum: `Some(())`
99
|
1010
= note: expected type `std::option::Option<()>`
1111
found type `()`

‎src/test/ui/issues/issue-55587.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0164]: expected tuple struct or tuple variant, found method `<Path>::new`
1+
error[E0164]: expected tuple struct or tuple variant, found method `Path::new`
22
--> $DIR/issue-55587.rs:4:9
33
|
44
LL | let Path::new();

‎src/test/ui/match/match-fn-call.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error[E0164]: expected tuple struct or tuple variant, found method `<Path>::new`
1+
error[E0164]: expected tuple struct or tuple variant, found method `Path::new`
22
--> $DIR/match-fn-call.rs:6:9
33
|
44
LL | Path::new("foo") => println!("foo"),
55
| ^^^^^^^^^^^^^^^^ `fn` calls are not allowed in patterns
66
|
77
= help: for more information, visit https://doc.rust-lang.org/book/ch18-00-patterns.html
88

9-
error[E0164]: expected tuple struct or tuple variant, found method `<Path>::new`
9+
error[E0164]: expected tuple struct or tuple variant, found method `Path::new`
1010
--> $DIR/match-fn-call.rs:8:9
1111
|
1212
LL | Path::new("bar") => println!("bar"),

‎src/test/ui/methods/method-path-in-pattern.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ impl MyTrait for Foo {}
1313
fn main() {
1414
match 0u32 {
1515
Foo::bar => {}
16-
//~^ ERROR expected unit struct, unit variant or constant, found method `<Foo>::bar`
16+
//~^ ERROR expected unit struct, unit variant or constant, found method `Foo::bar`
1717
}
1818
match 0u32 {
1919
<Foo>::bar => {}
20-
//~^ ERROR expected unit struct, unit variant or constant, found method `<Foo>::bar`
20+
//~^ ERROR expected unit struct, unit variant or constant, found method `Foo::bar`
2121
}
2222
match 0u32 {
2323
<Foo>::trait_bar => {}
24-
//~^ ERROR expected unit struct, unit variant or constant, found method `<Foo>::trait_bar`
24+
//~^ ERROR expected unit struct, unit variant or constant, found method `Foo::trait_bar`
2525
}
2626
if let Foo::bar = 0u32 {}
27-
//~^ ERROR expected unit struct, unit variant or constant, found method `<Foo>::bar`
27+
//~^ ERROR expected unit struct, unit variant or constant, found method `Foo::bar`
2828
if let <Foo>::bar = 0u32 {}
29-
//~^ ERROR expected unit struct, unit variant or constant, found method `<Foo>::bar`
29+
//~^ ERROR expected unit struct, unit variant or constant, found method `Foo::bar`
3030
if let Foo::trait_bar = 0u32 {}
31-
//~^ ERROR expected unit struct, unit variant or constant, found method `<Foo>::trait_bar`
31+
//~^ ERROR expected unit struct, unit variant or constant, found method `Foo::trait_bar`
3232
}

‎src/test/ui/methods/method-path-in-pattern.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
1-
error[E0533]: expected unit struct, unit variant or constant, found method `<Foo>::bar`
1+
error[E0533]: expected unit struct, unit variant or constant, found method `Foo::bar`
22
--> $DIR/method-path-in-pattern.rs:15:9
33
|
44
LL | Foo::bar => {}
55
| ^^^^^^^^
66

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

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

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

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

31-
error[E0533]: expected unit struct, unit variant or constant, found method `<Foo>::trait_bar`
31+
error[E0533]: expected unit struct, unit variant or constant, found method `Foo::trait_bar`
3232
--> $DIR/method-path-in-pattern.rs:30:12
3333
|
3434
LL | if let Foo::trait_bar = 0u32 {}

‎src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-Self-issue-58006.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0533]: expected unit struct, unit variant or constant, found tuple variant `<Self>::A`
1+
error[E0533]: expected unit struct, unit variant or constant, found tuple variant `Self::A`
22
--> $DIR/incorrect-variant-form-through-Self-issue-58006.rs:8:13
33
|
44
LL | Self::A => (),

‎src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-alias-caught.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ type Alias = Enum;
88

99
fn main() {
1010
Alias::Braced;
11-
//~^ ERROR expected unit struct, unit variant or constant, found struct variant `<Alias>::Braced` [E0533]
11+
//~^ ERROR expected unit struct, unit variant or constant, found struct variant `Alias::Braced` [E0533]
1212
let Alias::Braced = panic!();
13-
//~^ ERROR expected unit struct, unit variant or constant, found struct variant `<Alias>::Braced` [E0533]
13+
//~^ ERROR expected unit struct, unit variant or constant, found struct variant `Alias::Braced` [E0533]
1414
let Alias::Braced(..) = panic!();
15-
//~^ ERROR expected tuple struct or tuple variant, found struct variant `<Alias>::Braced` [E0164]
15+
//~^ ERROR expected tuple struct or tuple variant, found struct variant `Alias::Braced` [E0164]
1616

1717
Alias::Unit();
18-
//~^ ERROR expected function, found enum variant `<Alias>::Unit`
18+
//~^ ERROR expected function, found enum variant `Alias::Unit`
1919
let Alias::Unit() = panic!();
20-
//~^ ERROR expected tuple struct or tuple variant, found unit variant `<Alias>::Unit` [E0164]
20+
//~^ ERROR expected tuple struct or tuple variant, found unit variant `Alias::Unit` [E0164]
2121
}

‎src/test/ui/type-alias-enum-variants/incorrect-variant-form-through-alias-caught.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
error[E0533]: expected unit struct, unit variant or constant, found struct variant `<Alias>::Braced`
1+
error[E0533]: expected unit struct, unit variant or constant, found struct variant `Alias::Braced`
22
--> $DIR/incorrect-variant-form-through-alias-caught.rs:10:5
33
|
44
LL | Alias::Braced;
55
| ^^^^^^^^^^^^^
66

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

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

19-
error[E0618]: expected function, found enum variant `<Alias>::Unit`
19+
error[E0618]: expected function, found enum variant `Alias::Unit`
2020
--> $DIR/incorrect-variant-form-through-alias-caught.rs:17:5
2121
|
2222
LL | enum Enum { Braced {}, Unit, Tuple() }
23-
| ---- `<Alias>::Unit` defined here
23+
| ---- `Alias::Unit` defined here
2424
...
2525
LL | Alias::Unit();
2626
| ^^^^^^^^^^^--
2727
| |
2828
| call expression requires function
2929
|
30-
help: `<Alias>::Unit` is a unit variant, you need to write it without the parenthesis
30+
help: `Alias::Unit` is a unit variant, you need to write it without the parenthesis
3131
|
32-
LL | <Alias>::Unit;
33-
| ^^^^^^^^^^^^^
32+
LL | Alias::Unit;
33+
| ^^^^^^^^^^^
3434

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

0 commit comments

Comments
 (0)
Please sign in to comment.