improve diagnostic suggestion when matching struct variants with tuple#158748
improve diagnostic suggestion when matching struct variants with tuple#158748Snxhit wants to merge 2 commits into
Conversation
|
Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @hanna-kruppe (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Thanks, this new suggestion is a big improvement.
I have some concerns about adding even more case distinctions and parameters to report_unexpected_variant_res as it's already very unwieldy. But I don't see a way to avoid that without a much larger refactoring, so I've limited my suggestions to the code this PR adds/touches.
There's also one more thing to improve about the suggestion and some more tests I'd like to see.
| let msg = if sub_pats.is_some() { | ||
| "add the names to match a struct variant's fields".to_string() | ||
| } else { | ||
| format!( |
There was a problem hiding this comment.
There is only a single test left that exercises this message, tests/ui/match/unit-pattern-error-on-tuple-and-struct-variants-63983.rs. But the new suggestion also reads better there. So I'd suggest removing this branch entirely and only have two messages:
- "use the struct variant pattern syntax" for when there's no fields
- "add the names to match a struct variant's fields" otherwise
| tcx: TyCtxt<'_>, | ||
| res: Res, | ||
| expr: Option<&hir::Expr<'_>>, | ||
| sub_pats: Option<&[hir::Pat<'_>]>, |
There was a problem hiding this comment.
It seems like the only reason to distinguish None from Some(&[]) here is to preserve the "fields are being ignored" message in some cases. But if we drop that message and always use the new one as I suggested, then this can (and probably should) be a plain &[hir::Pat<'_>] for simplicity.
| if let Some(pats) = sub_pats | ||
| && let Some(user_pat) = pats.get(i) | ||
| { | ||
| let pat_snippet = tcx | ||
| .sess | ||
| .source_map() | ||
| .span_to_snippet(user_pat.span) | ||
| .unwrap_or_else(|_| "_".to_string()); | ||
| format!("{field_name}: {pat_snippet}") | ||
| } else { | ||
| format!("{field_name}: _") | ||
| } |
There was a problem hiding this comment.
This seems more complicated than it has to be, since it always produces {field_name}: {pat} with pat falling back to _ if anything goes wrong. Especially with my other suggestion to accept sub_paths: &[hir::Pat<'_>] without Option, this could just be:
let pat_snippet = sub_pats
.get(i)
.and_then(|sub_pat| {
tcx.sess.source_map().span_to_snippet(sub_pat.span).ok()
})
.unwrap_or_else(|| "_".to_string());
format!("{field_name}: {pat_snippet}")| | | ||
| LL - Enum::Foo(a, b) => {} | ||
| LL + Enum::Foo { a: _, b: _ } => {} | ||
| LL + Enum::Foo { a: a, b: b } => {} |
There was a problem hiding this comment.
We should suggest Enum::Foo { a, b } in case the pattern matches the field name. I think it should be easy to catch this while building fields_sugg (essentially, compute the suggested pattern for the field as normal, and then check if that string is equal to the field name).
There was a problem hiding this comment.
Please add a few more tests to exercise potential corner cases in the suggestion (I think your current code handles most or all of them, but we should have tests to keep it that way):
- Different identifiers for the patterns than for the fields (e.g.
Enum::Foo(x, y)should get suggestionEnum::Foo { a: x, b: y }) - Sub-patterns other than identifiers and
_(e.g.,Enum::Foo(1 | 2, b @ 3)-- I think that one will suggest invalid syntax) - Fewer sub-patterns than fields (e.g.,
Enum::Foo(a)=>Enum::Foo { a, b: _ }) - More sub-patterns than fields (e.g.,
Enum::Foo(a, b, c)=>Enum::Foo { a, b }) -- note that this dropscand therefore may introduce compilation errors
There was a problem hiding this comment.
(e.g.,
Enum::Foo(1 | 2, b @ 3)-- I think that one will suggest invalid syntax)
Correction: the suggestion compiles, it just looks very odd. I don't think it's worth the complexity of trying to give a better suggestion in such cases.
|
Reminder, once the PR becomes ready for a review, use |
fixes #158641
changes:
passed
sub_patsfromresolve_pat_tuple_structintoreport_unexpected_variant_res, match user's variable names to struct fields, fix suggestion name.