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

Replace some magic booleans in match-lowering with enums #126981

Merged
merged 3 commits into from
Jun 30, 2024

Conversation

Zalathar
Copy link
Contributor

@Zalathar Zalathar commented Jun 26, 2024

This PR takes some boolean arguments used by the match-lowering code, and replaces them with dedicated enums that more clearly express their effect, while also making it much easier to find how each value is ultimately used.

@rustbot
Copy link
Collaborator

rustbot commented Jun 26, 2024

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

@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 26, 2024
@Zalathar
Copy link
Contributor Author

Zalathar commented Jun 26, 2024

@rust-log-analyzer

This comment has been minimized.

@Nadrieril
Copy link
Member

r? @Nadrieril

I like this a lot! r=me once the other PR is merged.

@bors
Copy link
Contributor

bors commented Jun 29, 2024

☔ The latest upstream changes (presumably #127111) made this pull request unmergeable. Please resolve the merge conflicts.

@Zalathar Zalathar marked this pull request as ready for review June 29, 2024 12:56
@rustbot
Copy link
Collaborator

rustbot commented Jun 29, 2024

Some changes occurred in match lowering

cc @Nadrieril

@Zalathar
Copy link
Contributor Author

Rebased after #126835 (finally) landed.

&& schedule_drop
{
self.schedule_drop(span, region_scope, local_id, DropKind::Storage);
if let Some(region_scope) = self.region_scope_tree.var_scope(var.0.local_id) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Side note: I was looking at the history of this if let (#94849), and it made me wonder if the None case can never actually occur.

I tried replacing the None case with span_bug and couldn't observe any failures in the test suites I ran. That's not entirely conclusive, but it is suggestive.

(Obviously this is all outside the scope of this PR; just noting something interesting that I noticed.)

Copy link
Member

@Nadrieril Nadrieril Jun 29, 2024

Choose a reason for hiding this comment

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

Hm weird, that PR does add a test that used to get a None, so something else must have changed. I can't say I understand this code enough to tell though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My guess is that at the time the change was made, there were fewer “early” syntactic checks for let expressions in bad places, so it was possible for one to (illegally) exist inside certain const contexts that would survive to this point and cause an unwrap panic.

The change from unwrap to if-let was requested by #94849 (comment), but it doesn't give context for why. I suspect that it would have been preferable to make the None case do a span_delayed_bug instead of silently doing nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like #115677 is what moved the syntactic checks to parse-time, guaranteeing that illegal let expressions don't proceed to MIR building. That gives me a bit more confidence that the None scenario can't happen, and that we have tests that would likely hit this edge-case if it were possible.

Comment on lines +61 to +63
/// Let expressions are not permitted in this context, so it is a bug to
/// try to lower one (e.g inside lazy-boolean-or or boolean-not).
LetNotPermitted,
Copy link
Member

@Nadrieril Nadrieril Jun 29, 2024

Choose a reason for hiding this comment

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

I like this! It's much easier to follow than a boolean whose value will be ignored

@Nadrieril
Copy link
Member

Nadrieril commented Jun 29, 2024

Looks good! I have only one question: what benefit do you see in an exhaustive match with a {} arm over an if let or matches!()?

Zalathar added 2 commits June 30, 2024 18:55
The new enum `DeclareLetBindings` has three variants:
- `Yes`: Declare `let` bindings as normal, for `if` conditions.
- `No`: Don't declare bindings, for match guards and let-else.
- `LetNotPermitted`: Assert that `let` expressions should not occur.
The previous boolean used `true` to indicate that storage-live should _not_ be
emitted, so all occurrences of `Yes` and `No` should be the logical opposite of
the previous value.
@Zalathar
Copy link
Contributor Author

Replaced the exhaustive match on ScheduleDrops with if matches! (diff).

@Nadrieril
Copy link
Member

Thanks!

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jun 30, 2024

📌 Commit ed07712 has been approved by Nadrieril

It is now in the queue for this repository.

@bors bors removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 30, 2024
@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jun 30, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 30, 2024
Replace some magic booleans in match-lowering with enums

This PR takes some boolean arguments used by the match-lowering code, and replaces them with dedicated enums that more clearly express their effect, while also making it much easier to find how each value is ultimately used.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 30, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#126018 (Remove the `box_pointers` lint.)
 - rust-lang#126895 (Fix simd_gather documentation)
 - rust-lang#126981 (Replace some magic booleans in match-lowering with enums)
 - rust-lang#127069 (small correction to fmt::Pointer impl)
 - rust-lang#127157 (coverage: Avoid getting extra unexpansion info when we don't need it)
 - rust-lang#127160 (Add a regression test for rust-lang#123630)
 - rust-lang#127161 (Improve `run-make-support` library `args` API)
 - rust-lang#127162 (Subtree sync for rustc_codegen_cranelift)

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

Rollup of 9 pull requests

Successful merges:

 - rust-lang#126018 (Remove the `box_pointers` lint.)
 - rust-lang#126895 (Fix simd_gather documentation)
 - rust-lang#126981 (Replace some magic booleans in match-lowering with enums)
 - rust-lang#127038 (Update test comment)
 - rust-lang#127053 (Update the LoongArch target documentation)
 - rust-lang#127069 (small correction to fmt::Pointer impl)
 - rust-lang#127157 (coverage: Avoid getting extra unexpansion info when we don't need it)
 - rust-lang#127160 (Add a regression test for rust-lang#123630)
 - rust-lang#127161 (Improve `run-make-support` library `args` API)

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

Rollup of 9 pull requests

Successful merges:

 - rust-lang#126018 (Remove the `box_pointers` lint.)
 - rust-lang#126895 (Fix simd_gather documentation)
 - rust-lang#126981 (Replace some magic booleans in match-lowering with enums)
 - rust-lang#127038 (Update test comment)
 - rust-lang#127053 (Update the LoongArch target documentation)
 - rust-lang#127069 (small correction to fmt::Pointer impl)
 - rust-lang#127157 (coverage: Avoid getting extra unexpansion info when we don't need it)
 - rust-lang#127160 (Add a regression test for rust-lang#123630)
 - rust-lang#127161 (Improve `run-make-support` library `args` API)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit d404ce7 into rust-lang:master Jun 30, 2024
6 checks passed
@rustbot rustbot added this to the 1.81.0 milestone Jun 30, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jun 30, 2024
Rollup merge of rust-lang#126981 - Zalathar:enums, r=Nadrieril

Replace some magic booleans in match-lowering with enums

This PR takes some boolean arguments used by the match-lowering code, and replaces them with dedicated enums that more clearly express their effect, while also making it much easier to find how each value is ultimately used.
@Zalathar Zalathar deleted the enums branch June 30, 2024 23: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.

6 participants