Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve: improve "try using the enum's variant" #77341

Merged

Conversation

davidtwco
Copy link
Member

Fixes #73427.

This PR improves the "try using the enum's variant" suggestion:

  • Variants in suggestions would not result in more errors (e.g. use of a struct variant is only suggested if the suggestion can trivially construct that variant). Therefore, suggestions are only emitted for variants that have no fields (since the suggestion can't know what value fields would have).
  • Suggestions include the syntax for constructing the variant. If a struct or tuple variant is suggested, then it is constructed in the suggestion - unless in pattern-matching or when arguments are already provided.
  • A help message is added which mentions the variants which are no longer suggested.

All of the diagnostic logic introduced by this PR is separated from the normal code path for a successful compilation.

r? @estebank

This commit improves the "try using the enum's variant" suggestion:

- Variants in suggestions would not result in more errors (e.g. use
  of a struct variant is only suggested if the suggestion can
  trivially construct that variant). Therefore, suggestions are only
  emitted for variants that have no fields (since the suggestion
  can't know what value fields would have).
- Suggestions include the syntax for constructing the variant. If a
  struct or tuple variant is suggested, then it is constructed in the
  suggestion - unless in pattern-matching or when arguments are already
  provided.
- A help message is added which mentions the variants which are no
  longer suggested.

Signed-off-by: David Wood <[email protected]>
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 29, 2020
@estebank
Copy link
Contributor

estebank commented Oct 2, 2020

I'm slightly concerned that by not showing suggestions for the variants with fields we might mislead people into thinking those don't exist. I also notice that we only check that the expression is tuple variant-like only, we do not check that the number of fields match the suggested variants. In my mind we would provide suggestions for the cases that don't match (like a struct variant with multiple fields) with placeholders.

I like the new logic overall otherwise and appreciate moving it to a new method.

Comment on lines +1236 to +1240
if non_suggestable_variant_count == 1 {
err.help("you might have meant to use the enum's variant");
} else if non_suggestable_variant_count >= 1 {
err.help("you might have meant to use one of the enum's variants");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this to somehow suggest the non-tuple variants with placeholders. It would also mean getting the full span, if I'm reading correctly.

@davidtwco
Copy link
Member Author

I'm slightly concerned that by not showing suggestions for the variants with fields we might mislead people into thinking those don't exist.

I tried to alleviate this with the help notes that the PR adds.

I also notice that we only check that the expression is tuple variant-like only, we do not check that the number of fields match the suggested variants. In my mind we would provide suggestions for the cases that don't match (like a struct variant with multiple fields) with placeholders.

I was tempted to do this, but the PR was already changing a lot and I wasn't sure how complicated it would end up (PathSource contained an slice of spans for the pattern-matching case but I wasn't sure how that would handle .. in patterns; I didn't look into how I'd get the number of arguments passed in the call case). I think this would be better as a follow-up.

@estebank
Copy link
Contributor

estebank commented Oct 7, 2020

Ok, I'm r+ but let's wait until the new beta is cut-off to merge so that we have time to do the follow up work with a relaxed timeline so that we land the whole thing on the same stable release.

@Mark-Simulacrum
Copy link
Member

Beta cutoff was last Friday -- you can check src/version for the current version number generally speaking and if that's nightly+1 according to https://forge.rust-lang.org/ then we're in the new cycle.

@estebank
Copy link
Contributor

estebank commented Oct 7, 2020

Neat, for some reason I conflated the stable and beta release dates (which of course make no sense to be tied together).

@bors r+

@bors
Copy link
Contributor

bors commented Oct 7, 2020

📌 Commit 9ef68f5 has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 7, 2020
@bors
Copy link
Contributor

bors commented Oct 7, 2020

⌛ Testing commit 9ef68f5 with merge deec530...

@bors
Copy link
Contributor

bors commented Oct 7, 2020

☀️ Test successful - checks-actions, checks-azure
Approved by: estebank
Pushing deec530 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 7, 2020
@bors bors merged commit deec530 into rust-lang:master Oct 7, 2020
@rustbot rustbot added this to the 1.49.0 milestone Oct 7, 2020
@davidtwco davidtwco deleted the issue-73427-you-might-have-meant-variant branch October 7, 2020 17:41
davidtwco added a commit to davidtwco/rust that referenced this pull request Oct 15, 2020
This commit improves the diagnostic modified in rust-lang#77341 to
suggest not only those variants which do not have fields, but those with
fields (by suggesting with placeholders).

Signed-off-by: David Wood <[email protected]>
davidtwco added a commit to davidtwco/rust that referenced this pull request Oct 15, 2020
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 <[email protected]>
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Oct 16, 2020
…nstructable-variants, r=estebank

resolve: further improvements to "try using the enum's variant" diagnostic

Follow-up on rust-lang#77341 (comment).

This PR improves the diagnostic modified in rust-lang#77341 to suggest not only those variants which do not have fields, but those with fields (by suggesting with placeholders). In addition, the wording of the tuple-variant-only case is improved slightly.

I've not made further changes to the tuple-variant-only case (e.g. to only suggest variants with the correct number of fields) because I don't think I have enough information to do so reliably (e.g. in the case where there is an attempt to construct a tuple variant, I have no information on how many fields were provided; and in the case of pattern matching, I only have a slice of spans and would need to check for things like `..` in those spans, which doesn't seem worth it).

r? @estebank
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Oct 16, 2020
…nstructable-variants, r=estebank

resolve: further improvements to "try using the enum's variant" diagnostic

Follow-up on rust-lang#77341 (comment).

This PR improves the diagnostic modified in rust-lang#77341 to suggest not only those variants which do not have fields, but those with fields (by suggesting with placeholders). In addition, the wording of the tuple-variant-only case is improved slightly.

I've not made further changes to the tuple-variant-only case (e.g. to only suggest variants with the correct number of fields) because I don't think I have enough information to do so reliably (e.g. in the case where there is an attempt to construct a tuple variant, I have no information on how many fields were provided; and in the case of pattern matching, I only have a slice of spans and would need to check for things like `..` in those spans, which doesn't seem worth it).

r? @estebank
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Oct 16, 2020
…nstructable-variants, r=estebank

resolve: further improvements to "try using the enum's variant" diagnostic

Follow-up on rust-lang#77341 (comment).

This PR improves the diagnostic modified in rust-lang#77341 to suggest not only those variants which do not have fields, but those with fields (by suggesting with placeholders). In addition, the wording of the tuple-variant-only case is improved slightly.

I've not made further changes to the tuple-variant-only case (e.g. to only suggest variants with the correct number of fields) because I don't think I have enough information to do so reliably (e.g. in the case where there is an attempt to construct a tuple variant, I have no information on how many fields were provided; and in the case of pattern matching, I only have a slice of spans and would need to check for things like `..` in those spans, which doesn't seem worth it).

r? @estebank
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Name of enum variant with fields is suggested in error messages
6 participants