Skip to content

Commit

Permalink
resolve: improve "try using tuple struct" message
Browse files Browse the repository at this point in the history
This commit improves the tuple struct case added in #77341
so that the context is mentioned in more of the message.

Signed-off-by: David Wood <david@davidtw.co>
  • Loading branch information
davidtwco committed Oct 15, 2020
1 parent adf31e9 commit f897162
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
22 changes: 11 additions & 11 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Expand Up @@ -1341,29 +1341,29 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {

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

let source_msg = if source.is_call() {
"to construct"
} else if matches!(source, PathSource::TupleStruct(..)) {
"to match against"
} else {
unreachable!()
};

if !suggestable_variants.is_empty() {
let msg = if non_suggestable_variant_count == 0 && suggestable_variants.len() == 1 {
"try using the enum's variant"
format!("try {} the enum's variant", source_msg)
} else {
"try using one of the enum's variants"
format!("try {} one of the enum's variants", source_msg)
};

err.span_suggestions(
span,
msg,
&msg,
suggestable_variants.drain(..),
Applicability::MaybeIncorrect,
);
}

let source_msg = if source.is_call() {
"to construct"
} else if matches!(source, PathSource::TupleStruct(..)) {
"to match against"
} else {
unreachable!()
};

// If the enum has no tuple variants..
if non_suggestable_variant_count == variants.len() {
err.help(&format!("the enum has no tuple variants {}", source_msg));
Expand Down
Expand Up @@ -2,23 +2,23 @@ error[E0423]: expected function, tuple struct or tuple variant, found enum `Opti
--> $DIR/issue-43871-enum-instead-of-variant.rs:19:13
|
LL | let x = Option(1);
| ^^^^^^ help: try using one of the enum's variants: `std::option::Option::Some`
| ^^^^^^ help: try to construct one of the enum's variants: `std::option::Option::Some`
|
= help: you might have meant to construct the enum's non-tuple variant

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

error[E0532]: expected tuple struct or tuple variant, found enum `Example`
--> $DIR/issue-43871-enum-instead-of-variant.rs:27:12
|
LL | if let Example(_) = y {
| ^^^^^^^ help: try using one of the enum's variants: `Example::Ex`
| ^^^^^^^ help: try to match against one of the enum's variants: `Example::Ex`
|
= help: you might have meant to match against the enum's non-tuple variant
note: the enum is defined here
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/issues/issue-73427.stderr
Expand Up @@ -118,7 +118,7 @@ LL | | Tuple(),
LL | | Unit,
LL | | }
| |_^
help: try using one of the enum's variants
help: try to construct one of the enum's variants
|
LL | let x = A::TupleWithFields(3);
| ^^^^^^^^^^^^^^^^^^
Expand All @@ -143,7 +143,7 @@ LL | | Tuple(),
LL | | Unit,
LL | | }
| |_^
help: try using one of the enum's variants
help: try to match against one of the enum's variants
|
LL | if let A::TupleWithFields(3) = x { }
| ^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit f897162

Please sign in to comment.