Skip to content

improve diagnostic suggestion when matching struct variants with tuple#158748

Open
Snxhit wants to merge 2 commits into
rust-lang:mainfrom
Snxhit:fix-struct-variant-suggestion
Open

improve diagnostic suggestion when matching struct variants with tuple#158748
Snxhit wants to merge 2 commits into
rust-lang:mainfrom
Snxhit:fix-struct-variant-suggestion

Conversation

@Snxhit

@Snxhit Snxhit commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

fixes #158641

changes:
passed sub_pats from resolve_pat_tuple_struct into report_unexpected_variant_res, match user's variable names to struct fields, fix suggestion name.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 3, 2026
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jul 3, 2026
@rustbot

rustbot commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

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 (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 21 candidates

@rust-log-analyzer

This comment has been minimized.

@hanna-kruppe hanna-kruppe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

View changes since this review

let msg = if sub_pats.is_some() {
"add the names to match a struct variant's fields".to_string()
} else {
format!(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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<'_>]>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +581 to +592
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}: _")
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 } => {}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 suggestion Enum::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 drops c and therefore may introduce compilation errors

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(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.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 12, 2026
@rustbot

rustbot commented Jul 12, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Misleading "fields are being ignored" help when changing a variant from tuple- to struct-like

4 participants