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

match lowering: Hide Candidate from outside the lowering algorithm #127159

Merged
merged 5 commits into from
Aug 1, 2024

Conversation

Nadrieril
Copy link
Member

@Nadrieril Nadrieril commented Jun 30, 2024

The internals of Candidate are tricky and a source of confusion. This PR makes it so we don't expose Candidates outside the lowering algorithm. Now:

  • false edges are handled in lower_match_tree;
  • lower_match_tree takes a list of patterns as input;
  • lower_match_tree returns a flat datastructure that contains only the necessary information.

r? @matthewjasper

try-job: x86_64-rust-for-linux

@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 Jun 30, 2024
@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as resolved.

// the failure block.
previous_candidate.as_mut().unwrap().next_candidate_start_block = Some(otherwise_block)
} else {
if !refutable {
Copy link
Contributor

Choose a reason for hiding this comment

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

Side note: refutable: bool seems like another good candidate for a dedicated enum in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Possibly yes. I have hopes to remove the need for it, we'll see

@Zalathar
Copy link
Contributor

So if I understand this correctly:

  • There is a flat list of MatchTreeBranch at the top level, and each one corresponds 1:1 with an arm of the match expression.
    • Should we assert that this is true just before zipping the arms/branches back together?
  • Each of those branches contains a flat list of 1 or more MatchTreeSubBranch.
    • Should we assert that the list of sub-branches is not empty?
    • A sub-branch represents a “way” for that arm to decide that it should jump to checking the guard (or jump to the arm body if the arm has no guard).
    • Arms without or-patterns should have exactly 1 sub-branch.
    • For arms with or-patterns, the exact number of sub-branches is an implementation detail, depending on opportunities for simplification (which typically require there to be no guard).
    • The guard needs to be lowered separately for each sub-branch (because each sub-branch might bind different values).
    • If the guard fails, we jump to that sub-branch's otherwise_block, which will have been wired up to jump to the next sub-branch (if one exists), or otherwise to the next top-level arm branch.

@Nadrieril
Copy link
Member Author

Nadrieril commented Jul 10, 2024

Yep, all correct. I don't feel the need to assert the number of arms, it's just a collect().into_iter().map().collect() in lower_match_tree.

@bors

This comment was marked as resolved.

@rust-log-analyzer

This comment has been minimized.

@bors

This comment was marked as resolved.

@Zalathar
Copy link
Contributor

Zalathar commented Jul 22, 2024

I was playing around with this, and one thing I found was that it's fairly easy to change MatchTreeBranch::from_candidate and MatchTreeSubBranch::from_sub_candidate so that they take &Candidate by reference instead of Candidate by value.

Obviously both ways are possible (since the candidates aren't used afterwards), but the by-reference version feels a bit nicer to me, because there's no need to worry about being able to move things at the right time.

Similarly, it's possible to have lower_match_tree take a &BuiltMatchTree by reference instead of BuiltMatchTree by value, with only minimal tweaks to the relevant code.

(Not a blocking concern; I'm happy to propose this change as a follow-up PR.)

@Nadrieril
Copy link
Member Author

I was playing around with this, and one thing I found was that it's fairly easy to change MatchTreeBranch::from_candidate and MatchTreeSubBranch::from_sub_candidate so that they take &Candidate by reference instead of Candidate by value.

Obviously both ways are possible (since the candidates aren't used afterwards), but the by-reference version feels a bit nicer to me, because there's no need to worry about being able to move things at the right time.

I made them take Candidate exactly because it feels important that we don't use the Candidates afterward. These functions are meant to be used exactly in one spot, why would there be a worry about being able to move things?

Similarly, it's possible to have lower_match_tree take a &BuiltMatchTree by reference instead of BuiltMatchTree by value, with only minimal tweaks to the relevant code.

I'm guessing you mean bind_pattern could take &MatchTreeBranch? Sure, that one feels less important. But again I'm curious what your usecase is, given that this is meant to be called exactly once per branch directly after lower_match_tree.

@Zalathar
Copy link
Contributor

Ah, I didn't have a specific use case (beyond trying to get rid of the slightly ugly length check and try_into in bind_pattern), so if you did it this way on purpose then I won't worry about it.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 25, 2024
Various notes on match lowering

This is an assortment of comments for things that I found unclear or confusing when I was learning how match lowering works.

This PR only adds/modifies comments, so there are no functional changes.

I have tried to avoid touching code that would conflict with rust-lang#127159.

r? `@Nadrieril`
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jul 26, 2024
Rollup merge of rust-lang#128085 - Zalathar:notes, r=Nadrieril

Various notes on match lowering

This is an assortment of comments for things that I found unclear or confusing when I was learning how match lowering works.

This PR only adds/modifies comments, so there are no functional changes.

I have tried to avoid touching code that would conflict with rust-lang#127159.

r? `@Nadrieril`
@bors

This comment was marked as outdated.

@bors
Copy link
Contributor

bors commented Jul 31, 2024

📌 Commit 08bcc01 has been approved by matthewjasper

It is now in the queue for this repository.

@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 Jul 31, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 31, 2024
…wjasper

match lowering: Hide `Candidate` from outside the lowering algorithm

The internals of `Candidate` are tricky and a source of confusion. This PR makes it so we don't expose `Candidate`s outside the lowering algorithm. Now:
- false edges are handled in `lower_match_tree`;
- `lower_match_tree` takes a list of patterns as input;
- `lower_match_tree` returns a flat datastructure that contains only the necessary information.

r? `@matthewjasper`
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 31, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#123813 (Add `REDUNDANT_IMPORTS` lint for new redundant import detection)
 - rust-lang#127159 (match lowering: Hide `Candidate` from outside the lowering algorithm)
 - rust-lang#128162 (Cleanup sys module to match house style)
 - rust-lang#128296 (Update target-spec metadata for loongarch64 targets)
 - rust-lang#128417 (Add `f16` and `f128` math functions)
 - rust-lang#128431 (Add myself as VxWorks target maintainer for reference)
 - rust-lang#128437 (improve bootstrap to allow selecting llvm tools individually)

r? `@ghost`
`@rustbot` modify labels: rollup
tgross35 added a commit to tgross35/rust that referenced this pull request Jul 31, 2024
…wjasper

match lowering: Hide `Candidate` from outside the lowering algorithm

The internals of `Candidate` are tricky and a source of confusion. This PR makes it so we don't expose `Candidate`s outside the lowering algorithm. Now:
- false edges are handled in `lower_match_tree`;
- `lower_match_tree` takes a list of patterns as input;
- `lower_match_tree` returns a flat datastructure that contains only the necessary information.

r? ``@matthewjasper``
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 31, 2024
Rollup of 7 pull requests

Successful merges:

 - rust-lang#117468 (Stabilize Wasm relaxed SIMD)
 - rust-lang#123813 (Add `REDUNDANT_IMPORTS` lint for new redundant import detection)
 - rust-lang#127060 (Migrate `symbol-visibility` `run-make` test to rmake)
 - rust-lang#127159 (match lowering: Hide `Candidate` from outside the lowering algorithm)
 - rust-lang#128296 (Update target-spec metadata for loongarch64 targets)
 - rust-lang#128416 (android: Remove libstd hacks for unsupported Android APIs)
 - rust-lang#128431 (Add myself as VxWorks target maintainer for reference)

r? `@ghost`
`@rustbot` modify labels: rollup
@tgross35
Copy link
Contributor

I think this may have somehow failed the rollup at #128454 (comment). I am not entirely sure how but this seems like the only PR in the rollup that affects codegen.

I'll start a try job to confirm. The RFL job failed but I assume that was just the first CI job to complete and others would have failed too. Also going to ask on Zulip.

@bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jul 31, 2024
@tgross35
Copy link
Contributor

@bors try

@bors
Copy link
Contributor

bors commented Jul 31, 2024

⌛ Trying commit 08bcc01 with merge c52c83f...

bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 31, 2024
match lowering: Hide `Candidate` from outside the lowering algorithm

The internals of `Candidate` are tricky and a source of confusion. This PR makes it so we don't expose `Candidate`s outside the lowering algorithm. Now:
- false edges are handled in `lower_match_tree`;
- `lower_match_tree` takes a list of patterns as input;
- `lower_match_tree` returns a flat datastructure that contains only the necessary information.

r? `@matthewjasper`

try-job: x86_64-rust-for-linux
@tgross35
Copy link
Contributor

I am totally wrong, the wasm PR updated stdarch. Sorry about that, please re-r+ after the try job completes.

@bors
Copy link
Contributor

bors commented Jul 31, 2024

☀️ Try build successful - checks-actions
Build commit: c52c83f (c52c83f6ff32a78324decbca0ba3839309e99e52)

@tgross35
Copy link
Contributor

@bors r=matthewjasper

@bors
Copy link
Contributor

bors commented Jul 31, 2024

📌 Commit 08bcc01 has been approved by matthewjasper

It is now in the queue for this repository.

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 31, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 31, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#123813 (Add `REDUNDANT_IMPORTS` lint for new redundant import detection)
 - rust-lang#126697 ([RFC] mbe: consider the `_` in 2024 an expression)
 - rust-lang#127159 (match lowering: Hide `Candidate` from outside the lowering algorithm)
 - rust-lang#128244 (Peel off explicit (or implicit) deref before suggesting clone on move error in borrowck, remove some hacks)
 - rust-lang#128431 (Add myself as VxWorks target maintainer for reference)
 - rust-lang#128438 (Add special-case for [T, 0] in dropck_outlives)
 - rust-lang#128457 (Fix docs for OnceLock::get_mut_or_init)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 1, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#123813 (Add `REDUNDANT_IMPORTS` lint for new redundant import detection)
 - rust-lang#126697 ([RFC] mbe: consider the `_` in 2024 an expression)
 - rust-lang#127159 (match lowering: Hide `Candidate` from outside the lowering algorithm)
 - rust-lang#128244 (Peel off explicit (or implicit) deref before suggesting clone on move error in borrowck, remove some hacks)
 - rust-lang#128431 (Add myself as VxWorks target maintainer for reference)
 - rust-lang#128438 (Add special-case for [T, 0] in dropck_outlives)
 - rust-lang#128457 (Fix docs for OnceLock::get_mut_or_init)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 19f6ff0 into rust-lang:master Aug 1, 2024
7 checks passed
@rustbot rustbot added this to the 1.82.0 milestone Aug 1, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Aug 1, 2024
Rollup merge of rust-lang#127159 - Nadrieril:hide-candidate, r=matthewjasper

match lowering: Hide `Candidate` from outside the lowering algorithm

The internals of `Candidate` are tricky and a source of confusion. This PR makes it so we don't expose `Candidate`s outside the lowering algorithm. Now:
- false edges are handled in `lower_match_tree`;
- `lower_match_tree` takes a list of patterns as input;
- `lower_match_tree` returns a flat datastructure that contains only the necessary information.

r? ```@matthewjasper```
@Nadrieril Nadrieril deleted the hide-candidate branch August 1, 2024 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

7 participants