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

Exhaustively handle expressions in patterns #134228

Merged
merged 4 commits into from
Jan 9, 2025
Merged

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Dec 12, 2024

We currently have this invariant in HIR that a PatKind::Lit or a PatKind::Range only contains

  • ExprKind::Lit
  • ExprKind::UnOp(Neg, ExprKind::Lit)
  • ExprKind::Path
  • ExprKind::ConstBlock

So I made PatKind::Lit and PatKind::Range stop containing Expr, and instead created a PatLit type whose kind enum only contains those variants.

The only place code got more complicated was in clippy, as it couldn't share as much anymore with Expr handling

It may be interesting on merging ExprKind::{Path,Lit,ConstBlock} in the future and using the same PatLit type (under a new name).

Then it should also be easier to eliminate any and all UnOp(Neg, Lit) | Lit matching that we have across the compiler. Some day we should fold the negation into the literal itself and just store it on the numeric literals

@rustbot
Copy link
Collaborator

rustbot commented Dec 12, 2024

r? @compiler-errors

rustbot has assigned @compiler-errors.
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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Dec 12, 2024
@rustbot
Copy link
Collaborator

rustbot commented Dec 12, 2024

HIR ty lowering was modified

cc @fmease

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in match checking

cc @Nadrieril

@rustbot
Copy link
Collaborator

rustbot commented Dec 12, 2024

HIR ty lowering was modified

cc @fmease

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in match checking

cc @Nadrieril

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

@rustbot author

@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 Dec 12, 2024
@oli-obk
Copy link
Contributor Author

oli-obk commented Dec 13, 2024

I removed the PatKind::Path removal from this PR. I think it should be done, but I'm doing it as a follow-up PR in #134248

@rustbot ready

@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 Dec 13, 2024
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Dec 18, 2024

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

@bors
Copy link
Contributor

bors commented Dec 26, 2024

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

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

Other than the naming, which I think we should rename to PatExpr to make it clear that it's the subset of expressions allowed in pats, I think this looks fine.

@rustbot

This comment was marked as resolved.

@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 Jan 8, 2025
@oli-obk
Copy link
Contributor Author

oli-obk commented Jan 8, 2025

@bors r=compiler-errors

@bors
Copy link
Contributor

bors commented Jan 8, 2025

📌 Commit 4a8773a has been approved by compiler-errors

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 Jan 8, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 8, 2025
Merge `PatKind::Path` into `PatKind::Lit`

Follow-up to rust-lang#134228

We always had a duplication where `Path`s could be represented as `PatKind::Path` or `PatKind::Lit(ExprKind::Path)`. We had to handle both everywhere, and still do after rust-lang#134228, so I'm removing it now. subsequently we can also nuke `visit_pattern_type_pattern`
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 8, 2025
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#134228 (Exhaustively handle expressions in patterns)
 - rust-lang#135194 (triagebot: mark tidy changes with a more specific `A-tidy` label)
 - rust-lang#135222 (Ensure that we don't try to access fields on a non-struct pattern type)
 - rust-lang#135250 (A couple simple borrowck cleanups)
 - rust-lang#135252 (Fix release notes link)
 - rust-lang#135253 (Revert rust-lang#131365)

Failed merges:

 - rust-lang#135195 (Make `lit_to_mir_constant` and `lit_to_const` infallible)

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

Rollup of 6 pull requests

Successful merges:

 - rust-lang#134228 (Exhaustively handle expressions in patterns)
 - rust-lang#135194 (triagebot: mark tidy changes with a more specific `A-tidy` label)
 - rust-lang#135222 (Ensure that we don't try to access fields on a non-struct pattern type)
 - rust-lang#135250 (A couple simple borrowck cleanups)
 - rust-lang#135252 (Fix release notes link)
 - rust-lang#135253 (Revert rust-lang#131365)

Failed merges:

 - rust-lang#135195 (Make `lit_to_mir_constant` and `lit_to_const` infallible)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit f92a5ed into rust-lang:master Jan 9, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 9, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 9, 2025
Rollup merge of rust-lang#134228 - oli-obk:pat-lit-path, r=compiler-errors

Exhaustively handle expressions in patterns

We currently have this invariant in HIR that a `PatKind::Lit` or a `PatKind::Range` only contains

* `ExprKind::Lit`
* `ExprKind::UnOp(Neg, ExprKind::Lit)`
* `ExprKind::Path`
* `ExprKind::ConstBlock`

So I made `PatKind::Lit` and `PatKind::Range` stop containing `Expr`, and instead created a `PatLit` type whose `kind` enum only contains those variants.

The only place code got more complicated was in clippy, as it couldn't share as much anymore with `Expr` handling

It may be interesting on merging `ExprKind::{Path,Lit,ConstBlock}` in the future and using the same `PatLit` type (under a new name).

Then it should also be easier to eliminate any and all `UnOp(Neg, Lit) | Lit` matching that we have across the compiler. Some day we should fold the negation into the literal itself and just store it on the numeric literals
@oli-obk oli-obk deleted the pat-lit-path branch January 9, 2025 10:30
jhpratt added a commit to jhpratt/rust that referenced this pull request Jan 10, 2025
Only treat plain literal patterns as short

See rust-lang#134228 (comment) and rust-lang#134228 (comment) for context. We never wanted to treat const blocks and paths as short, only plain literals.

I don't know how to write a test for this, it.s not clear to me how the short pattern check actually affects the formatting
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 10, 2025
Merge `PatKind::Path` into `PatKind::Lit`

Follow-up to rust-lang#134228

We always had a duplication where `Path`s could be represented as `PatKind::Path` or `PatKind::Lit(ExprKind::Path)`. We had to handle both everywhere, and still do after rust-lang#134228, so I'm removing it now. subsequently we can also nuke `visit_pattern_type_pattern`
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 10, 2025
Rollup merge of rust-lang#135251 - oli-obk:push-lmpyvvyrtplk, r=ytmimi

Only treat plain literal patterns as short

See rust-lang#134228 (comment) and rust-lang#134228 (comment) for context. We never wanted to treat const blocks and paths as short, only plain literals.

I don't know how to write a test for this, it.s not clear to me how the short pattern check actually affects the formatting
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 16, 2025
Only treat plain literal patterns as short

See rust-lang#134228 (comment) and rust-lang#134228 (comment) for context. We never wanted to treat const blocks and paths as short, only plain literals.

I don't know how to write a test for this, it.s not clear to me how the short pattern check actually affects the formatting
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 16, 2025
Only treat plain literal patterns as short

See rust-lang#134228 (comment) and rust-lang#134228 (comment) for context. We never wanted to treat const blocks and paths as short, only plain literals.

I don't know how to write a test for this, it.s not clear to me how the short pattern check actually affects the formatting
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 16, 2025
Only treat plain literal patterns as short

See rust-lang#134228 (comment) and rust-lang#134228 (comment) for context. We never wanted to treat const blocks and paths as short, only plain literals.

I don't know how to write a test for this, it.s not clear to me how the short pattern check actually affects the formatting
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jan 16, 2025
Rollup merge of rust-lang#135251 - oli-obk:push-lmpyvvyrtplk, r=ytmimi

Only treat plain literal patterns as short

See rust-lang#134228 (comment) and rust-lang#134228 (comment) for context. We never wanted to treat const blocks and paths as short, only plain literals.

I don't know how to write a test for this, it.s not clear to me how the short pattern check actually affects the formatting
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 17, 2025
Merge `PatKind::Path` into `PatKind::Lit`

Follow-up to rust-lang#134228

We always had a duplication where `Path`s could be represented as `PatKind::Path` or `PatKind::Lit(ExprKind::Path)`. We had to handle both everywhere, and still do after rust-lang#134228, so I'm removing it now.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 17, 2025
Merge `PatKind::Path` into `PatKind::Lit`

Follow-up to rust-lang#134228

We always had a duplication where `Path`s could be represented as `PatKind::Path` or `PatKind::Lit(ExprKind::Path)`. We had to handle both everywhere, and still do after rust-lang#134228, so I'm removing it now.
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