Skip to content

Commit f897162

Browse files
committed
resolve: improve "try using tuple struct" message
This commit improves the tuple struct case added in rust-lang#77341 so that the context is mentioned in more of the message. Signed-off-by: David Wood <david@davidtw.co>
1 parent adf31e9 commit f897162

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1341,29 +1341,29 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
13411341

13421342
let non_suggestable_variant_count = variants.len() - suggestable_variants.len();
13431343

1344+
let source_msg = if source.is_call() {
1345+
"to construct"
1346+
} else if matches!(source, PathSource::TupleStruct(..)) {
1347+
"to match against"
1348+
} else {
1349+
unreachable!()
1350+
};
1351+
13441352
if !suggestable_variants.is_empty() {
13451353
let msg = if non_suggestable_variant_count == 0 && suggestable_variants.len() == 1 {
1346-
"try using the enum's variant"
1354+
format!("try {} the enum's variant", source_msg)
13471355
} else {
1348-
"try using one of the enum's variants"
1356+
format!("try {} one of the enum's variants", source_msg)
13491357
};
13501358

13511359
err.span_suggestions(
13521360
span,
1353-
msg,
1361+
&msg,
13541362
suggestable_variants.drain(..),
13551363
Applicability::MaybeIncorrect,
13561364
);
13571365
}
13581366

1359-
let source_msg = if source.is_call() {
1360-
"to construct"
1361-
} else if matches!(source, PathSource::TupleStruct(..)) {
1362-
"to match against"
1363-
} else {
1364-
unreachable!()
1365-
};
1366-
13671367
// If the enum has no tuple variants..
13681368
if non_suggestable_variant_count == variants.len() {
13691369
err.help(&format!("the enum has no tuple variants {}", source_msg));

src/test/ui/did_you_mean/issue-43871-enum-instead-of-variant.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ error[E0423]: expected function, tuple struct or tuple variant, found enum `Opti
22
--> $DIR/issue-43871-enum-instead-of-variant.rs:19:13
33
|
44
LL | let x = Option(1);
5-
| ^^^^^^ help: try using one of the enum's variants: `std::option::Option::Some`
5+
| ^^^^^^ help: try to construct one of the enum's variants: `std::option::Option::Some`
66
|
77
= help: you might have meant to construct the enum's non-tuple variant
88

99
error[E0532]: expected tuple struct or tuple variant, found enum `Option`
1010
--> $DIR/issue-43871-enum-instead-of-variant.rs:21:12
1111
|
1212
LL | if let Option(_) = x {
13-
| ^^^^^^ help: try using one of the enum's variants: `std::option::Option::Some`
13+
| ^^^^^^ help: try to match against one of the enum's variants: `std::option::Option::Some`
1414
|
1515
= help: you might have meant to match against the enum's non-tuple variant
1616

1717
error[E0532]: expected tuple struct or tuple variant, found enum `Example`
1818
--> $DIR/issue-43871-enum-instead-of-variant.rs:27:12
1919
|
2020
LL | if let Example(_) = y {
21-
| ^^^^^^^ help: try using one of the enum's variants: `Example::Ex`
21+
| ^^^^^^^ help: try to match against one of the enum's variants: `Example::Ex`
2222
|
2323
= help: you might have meant to match against the enum's non-tuple variant
2424
note: the enum is defined here

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ LL | | Tuple(),
118118
LL | | Unit,
119119
LL | | }
120120
| |_^
121-
help: try using one of the enum's variants
121+
help: try to construct one of the enum's variants
122122
|
123123
LL | let x = A::TupleWithFields(3);
124124
| ^^^^^^^^^^^^^^^^^^
@@ -143,7 +143,7 @@ LL | | Tuple(),
143143
LL | | Unit,
144144
LL | | }
145145
| |_^
146-
help: try using one of the enum's variants
146+
help: try to match against one of the enum's variants
147147
|
148148
LL | if let A::TupleWithFields(3) = x { }
149149
| ^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)