Skip to content

Commit fb73c0b

Browse files
authored
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 0ae035b + 5c023d6 commit fb73c0b

18 files changed

+57
-44
lines changed

src/librustc/hir/print.rs

Lines changed: 11 additions & 3 deletions
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

Lines changed: 7 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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 {}

0 commit comments

Comments
 (0)