Skip to content

mir_build: Add an extra intermediate step in MIR building for patterns #155144

Open
Zalathar wants to merge 2 commits intorust-lang:mainfrom
Zalathar:inter-pat
Open

mir_build: Add an extra intermediate step in MIR building for patterns #155144
Zalathar wants to merge 2 commits intorust-lang:mainfrom
Zalathar:inter-pat

Conversation

@Zalathar
Copy link
Copy Markdown
Member

@Zalathar Zalathar commented Apr 11, 2026

View all comments

This is an attempt to partly decouple the data structures created by match_pair.rs from the data structures that are ultimately consumed by the main part of match lowering.

In some ways this is a reversal from #137875. That PR succeeded in removing the TestCase::Irrefutable variant, by taking a pre-existing “simplification” step and fusing it directly into MatchPairTree::for_pattern. Unfortunately, in doing so it also reinforced a very high degree of coupling between the transformations performed in match_pair, and the data structures used by later steps.

My hope is that these changes will make it easier for follow-up work to further separate decision-making from MIR building when lowering patterns.

r? Nadrieril

@rustbot rustbot added 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. labels Apr 11, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 11, 2026

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

@rust-log-analyzer

This comment has been minimized.

@Zalathar
Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Apr 11, 2026
mir_build: Add an extra intermediate step in MIR building for patterns
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 11, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 11, 2026

☀️ Try build successful (CI)
Build commit: 76367e4 (76367e4db9c5ebe2cd39a24f067d772b200f4158, parent: c29effdf79bdea928ea366a2a820ed15c1de5f43)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (76367e4): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (secondary 2.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.8% [0.9%, 6.8%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.9% [-3.9%, -3.9%] 1
All ❌✅ (primary) - - 0

Cycles

Results (primary -9.8%, secondary -4.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-9.8% [-20.6%, -3.1%] 13
Improvements ✅
(secondary)
-4.3% [-8.0%, -2.5%] 4
All ❌✅ (primary) -9.8% [-20.6%, -3.1%] 13

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 491.148s -> 509.241s (3.68%)
Artifact size: 394.19 MiB -> 394.20 MiB (0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 11, 2026
@Zalathar

This comment was marked as resolved.

@Zalathar Zalathar force-pushed the inter-pat branch 2 times, most recently from 0623749 to c9949ba Compare April 18, 2026 05:34
@rust-bors

This comment has been minimized.

@Zalathar Zalathar force-pushed the inter-pat branch 2 times, most recently from 9631275 to 7abdb0f Compare April 18, 2026 12:02
@Zalathar
Copy link
Copy Markdown
Member Author

Marking this as ready for consideration.

I'm not entirely confident about the name “InterPat”, and I'm aware that this change doesn't yet demonstrate any clear benefit on its own, but I think it's a worthwhile step towards hopefully being able to disentangle match-lowering a bit more.

@Zalathar Zalathar marked this pull request as ready for review April 18, 2026 12:06
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 18, 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. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 18, 2026
Copy link
Copy Markdown
Member

@Nadrieril Nadrieril left a comment

Choose a reason for hiding this comment

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

I like this, the separation of concerns is very clean. I'm on board, I only have a couple nits. Agreed that InterPat isn't an amazing name but it's used in a single file so who cares really.

View changes since this review

Comment thread compiler/rustc_mir_build/src/builder/matches/match_pair.rs Outdated
Comment on lines +136 to +142
// Recursively squash any subpatterns into refutable `MatchPairTree` forests.
let mut subpairs = vec![];
for subpat in inter_pat.subpats {
MatchPairTree::squash_inter_pat(subpat, &mut subpairs, extra_data);
}

if let Some(testable_case) = inter_pat.testable_case {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For clarity I'd prefer the rest of the function to be in the else branch of the "or pattern" case

Comment on lines -168 to -170
// In order to please the borrow checker, when lowering a pattern
// like `x @ subpat` we must establish any bindings in `subpat`
// before establishing the binding for `x`.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that comment get lost, I don't see it anymore

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I found the original comment to be too long and verbose, so I rewrote it as this shorter comment in squash_inter_pat:

// If present, the binding must be pushed _after_ traversing subpatterns.
// This is so that when lowering something like `x @ NonCopy { copy_field }`,
// the binding to `copy_field` will occur before the binding for `x`.
// See <https://github.com/rust-lang/rust/issues/69971> for more background.

Do you think that's fine, or is there more detail you'd like me to retain?

(Note that the ordering restriction is not yet relevant when building InterPat; it only becomes important when squashing InterPat into MatchPairTree.)

match_pairs: &mut Vec<Self>, // Newly-created nodes are added to this vector
extra_data: &mut PatternExtraData<'tcx>, // Bindings/ascriptions are added here
) {
extra_data.ascriptions.extend(inter_pat.ascriptions);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would you mind starting the function with a let InterPat { ascriptions, ....} = inter_pat with an exhaustive field list so we're sure not to forget a field?

ascriptions: vec![],
is_never: inter_pat.is_never,
};
MatchPairTree::squash_inter_pat(inter_pat, &mut match_pairs, &mut extra_data);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: I have a preference for inlining squash_inter_pat into here, the current split took me a couple seconds to understand

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

squash_inter_pat is recursive (since it recursively squashes subpatterns), so I don't think it can be inlined.

Comment thread compiler/rustc_mir_build/src/builder/matches/match_pair.rs Outdated
This makes it easier to perform coordinated modifications to `FlatPat::new` and
`MatchPairTree::from_pattern`, because the two functions are heavily coupled.
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 25, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Zalathar
Copy link
Copy Markdown
Member Author

(Rebased onto main to make things easier; no actual changes yet.)

Comment on lines +128 to +129
// Or-patterns never need a place during MIR building.
place: None,
Copy link
Copy Markdown
Member Author

@Zalathar Zalathar Apr 25, 2026

Choose a reason for hiding this comment

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

Note that currently we retain places for or-pattern nodes and then (I believe) never use them, so this is a deliberate change from current behaviour.

View changes since the review

@Zalathar
Copy link
Copy Markdown
Member Author

Updated some comments (diff); still considering what to do about the style feedback.

This splits out a separate `InterPat` data structure from
`MatchPairTree`/`FlatPat`/`Candidate`, which should hopefully make it easier to
modify these earlier parts of match lowering without having to simultaneously
adjust many use-sites in later steps.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants