Skip to content

Commit 273ee61

Browse files
committed
Improve the "try using a variant of the expected type" hint.
1 parent 8d78bf6 commit 273ee61

19 files changed

+47
-44
lines changed

src/librustc/hir/print.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,9 +1523,7 @@ impl<'a> State<'a> {
15231523
colons_before_params)
15241524
}
15251525
hir::QPath::TypeRelative(ref qself, ref item_segment) => {
1526-
self.s.word("<");
15271526
self.print_type(qself);
1528-
self.s.word(">");
15291527
self.s.word("::");
15301528
self.print_ident(item_segment.ident);
15311529
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/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 {}

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

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

0 commit comments

Comments
 (0)