Skip to content

Move recursion out of MatchPairTree::for_pattern helpers #154943

Open
Zalathar wants to merge 2 commits intorust-lang:mainfrom
Zalathar:for-each-subpat
Open

Move recursion out of MatchPairTree::for_pattern helpers #154943
Zalathar wants to merge 2 commits intorust-lang:mainfrom
Zalathar:for-each-subpat

Conversation

@Zalathar
Copy link
Copy Markdown
Member

@Zalathar Zalathar commented Apr 7, 2026

The helper functions now just iterate over the relevant subpatterns, while leaving recursion up to the main function.

This avoids passing parameters that were only used for recursive plumbing, and consolidates all recursive calls into for_pattern itself, which should make it easier to experiment with changes to the recursive structure.

There should be no change to compiler behaviour.

Zalathar added 2 commits April 7, 2026 20:37
The helper functions now just iterate over the relevant subpatterns, while
leaving recursion up to the main function.

This avoids passing parameters that were only used for recursive plumbing, and
consolidates all recursive calls into `for_pattern` itself, which should make
it easier to experiment with changes to the recursive structure.

There should be no change to compiler behaviour.
`PatKind::Variant` is always used for enum variants, even for enums that only
have one variant.
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 7, 2026

Some changes occurred in match lowering

cc @Nadrieril

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 7, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 7, 2026

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, mir
  • compiler, mir expanded to 69 candidates
  • Random selection from 10 candidates

Copy link
Copy Markdown
Contributor

@nnethercote nnethercote left a comment

Choose a reason for hiding this comment

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

The first commit description says:

consolidates all recursive calls into for_pattern itself

but for_pattern isn't changed?

View changes since this review

fn for_each_field_subpat<'tcx>(
place: &PlaceBuilder<'tcx>,
subpatterns: &[FieldPat<'tcx>],
mut callback_fn: impl FnMut(PlaceBuilder<'tcx>, &Pat<'tcx>),
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.

Does this need to be a closure? For every occurrence the callback just calls for_pattern. Seems more general than necessary.

prefix: &[Pat<'tcx>],
opt_middle: &Option<Box<Pat<'tcx>>>,
suffix: &[Pat<'tcx>],
mut callback_fn: impl FnMut(PlaceBuilder<'tcx>, &Pat<'tcx>),
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.

ditto

@nnethercote nnethercote 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 Apr 8, 2026
@Zalathar
Copy link
Copy Markdown
Member Author

Zalathar commented Apr 8, 2026

Thinking about this some more, it might be worth just fully inlining both helpers.

The field-subpat helper is trivial, and much of the complexity in the array/slice helper comes from generalising over array and slice patterns, which have significantly different indexing needs.

@Nadrieril
Copy link
Copy Markdown
Member

Nadrieril commented Apr 8, 2026

much of the complexity in the array/slice helper comes from generalising over array and slice patterns, which have significantly different indexing needs.

You can try and see but I think keeping them together is clearer. I'm thinking, how about returning an impl Iterator<Item=(ProjectionElem, Pat)> and iterating over that, to avoid the callback?

The field-subpat helper is trivial

True, we could inline that one

(Happy to take the r ? on this one, it's code I'm very familiar with)

@nnethercote
Copy link
Copy Markdown
Contributor

Ok, thanks!

r? @Nadrieril

@rustbot rustbot assigned Nadrieril and unassigned nnethercote Apr 8, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 8, 2026

Nadrieril is not on the review rotation at the moment.
They may take a while to respond.

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.

4 participants