Skip to content

Commit 80c603f

Browse files
committed
path, not name, in sole-argument variant type mismatch suggestion
We want the suggested replacement (which IDE tooling and such might offer to automatically swap in) to, like, actually be correct: suggesting `MyVariant(x)` when the actual fix is `MyEnum::MyVariant(x)` might be better than nothing, but Rust is supposed to be the future of computing: we're better than better than nothing. As an exceptional case, we excise the prelude path, preferring to suggest `Some` or `Ok` rather than `std::prelude::v1::Some` and `std::prelude::v2::Ok`. (It's not worth the effort to future-proof against hypothetical preludes v2, v3, &c.: we trust our successors to grep—excuse me, ripgrep—for that.) Also, don't make this preëmpt the existing probe-for-return-type suggestions, despite their being looked unfavorably upon, at least in this situation (#42764 (comment)): Cody Schafer pointed out that that's a separate issue (#43178 (comment)). This is in the matter of #42764.
1 parent eac7410 commit 80c603f

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/librustc_typeck/check/demand.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
106106
let sole_field = &variant.fields[0];
107107
let sole_field_ty = sole_field.ty(self.tcx, substs);
108108
if self.can_coerce(expr_ty, sole_field_ty) {
109-
compatible_variants.push(variant.name);
109+
let mut variant_path = self.tcx.item_path_str(variant.did);
110+
variant_path = variant_path.trim_left_matches("std::prelude::v1::")
111+
.to_string();
112+
compatible_variants.push(variant_path);
110113
}
111114
}
112115
}
@@ -117,7 +120,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
117120
err.span_suggestions(expr.span,
118121
"perhaps you meant to use a variant of the expected type",
119122
suggestions);
120-
return Some(err);
121123
}
122124
}
123125

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
enum DoubleOption<T> {
1212
FirstSome(T),
1313
AlternativeSome(T),
14-
None,
14+
Nothing,
1515
}
1616

1717
fn this_function_expects_a_double_option<T>(d: DoubleOption<T>) {}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ error[E0308]: mismatched types
88
found type `usize`
99
help: perhaps you meant to use a variant of the expected type
1010
|
11-
21 | this_function_expects_a_double_option(FirstSome(n));
12-
| ^^^^^^^^^^^^
13-
21 | this_function_expects_a_double_option(AlternativeSome(n));
14-
| ^^^^^^^^^^^^^^^^^^
11+
21 | this_function_expects_a_double_option(DoubleOption::FirstSome(n));
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
21 | this_function_expects_a_double_option(DoubleOption::AlternativeSome(n));
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515

1616
error: aborting due to previous error
1717

0 commit comments

Comments
 (0)