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.

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

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 23, 2025
Merge `PatKind::Path` into `PatKind::Expr`

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 29, 2025
Merge `PatKind::Path` into `PatKind::Expr`

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 Feb 3, 2025
…, r=<try>

Fix accidentally not emitting overflowing literals lints anymore in patterns

This was regressed in rust-lang#134228 (not in beta yet).

The issue was that previously we nested `hir::Expr` inside `hir::PatKind::Lit`, so it was linted by the expression code.

So now I've set it up for visitors to be able to directly visit literals and get all literals
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Feb 5, 2025
…eril

Reject negative literals for unsigned or char types in pattern ranges and literals

It sucks a bit that we have to duplicate the work here (normal expressions just get this for free from the `ExprKind::UnOp(UnOp::Neg, ...)` typeck logic.

In rust-lang#134228 I caused

```rust
fn main() {
    match 42_u8 {
        -10..255 => {},
        _ => {}
    }
}
```

to just compile without even a lint.

I can't believe we didn't have tests for this

Amusingly rust-lang#136302 will also register a delayed bug in `lit_to_const` for this, so we'll have a redundancy if something like this fails again.
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Feb 5, 2025
…ks, r=compiler-errors

Fix accidentally not emitting overflowing literals lints anymore in patterns

This was regressed in rust-lang#134228 (not in beta yet).

The issue was that previously we nested `hir::Expr` inside `hir::PatKind::Lit`, so it was linted by the expression code.

So now I've set it up for visitors to be able to directly visit literals and get all literals
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Feb 5, 2025
…eril

Reject negative literals for unsigned or char types in pattern ranges and literals

It sucks a bit that we have to duplicate the work here (normal expressions just get this for free from the `ExprKind::UnOp(UnOp::Neg, ...)` typeck logic.

In rust-lang#134228 I caused

```rust
fn main() {
    match 42_u8 {
        -10..255 => {},
        _ => {}
    }
}
```

to just compile without even a lint.

I can't believe we didn't have tests for this

Amusingly rust-lang#136302 will also register a delayed bug in `lit_to_const` for this, so we'll have a redundancy if something like this fails again.
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Feb 5, 2025
…ks, r=compiler-errors

Fix accidentally not emitting overflowing literals lints anymore in patterns

This was regressed in rust-lang#134228 (not in beta yet).

The issue was that previously we nested `hir::Expr` inside `hir::PatKind::Lit`, so it was linted by the expression code.

So now I've set it up for visitors to be able to directly visit literals and get all literals
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Feb 5, 2025
…eril

Reject negative literals for unsigned or char types in pattern ranges and literals

It sucks a bit that we have to duplicate the work here (normal expressions just get this for free from the `ExprKind::UnOp(UnOp::Neg, ...)` typeck logic.

In rust-lang#134228 I caused

```rust
fn main() {
    match 42_u8 {
        -10..255 => {},
        _ => {}
    }
}
```

to just compile without even a lint.

I can't believe we didn't have tests for this

Amusingly rust-lang#136302 will also register a delayed bug in `lit_to_const` for this, so we'll have a redundancy if something like this fails again.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 6, 2025
Rollup merge of rust-lang#136304 - oli-obk:push-ymxoklvzrpvx, r=Nadrieril

Reject negative literals for unsigned or char types in pattern ranges and literals

It sucks a bit that we have to duplicate the work here (normal expressions just get this for free from the `ExprKind::UnOp(UnOp::Neg, ...)` typeck logic.

In rust-lang#134228 I caused

```rust
fn main() {
    match 42_u8 {
        -10..255 => {},
        _ => {}
    }
}
```

to just compile without even a lint.

I can't believe we didn't have tests for this

Amusingly rust-lang#136302 will also register a delayed bug in `lit_to_const` for this, so we'll have a redundancy if something like this fails again.
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Feb 6, 2025
…ks, r=compiler-errors

Fix accidentally not emitting overflowing literals lints anymore in patterns

This was regressed in rust-lang#134228 (not in beta yet).

The issue was that previously we nested `hir::Expr` inside `hir::PatKind::Lit`, so it was linted by the expression code.

So now I've set it up for visitors to be able to directly visit literals and get all literals
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 6, 2025
…ks, r=compiler-errors

Fix accidentally not emitting overflowing literals lints anymore in patterns

This was regressed in rust-lang#134228 (not in beta yet).

The issue was that previously we nested `hir::Expr` inside `hir::PatKind::Lit`, so it was linted by the expression code.

So now I've set it up for visitors to be able to directly visit literals and get all literals
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 6, 2025
Rollup merge of rust-lang#136393 - oli-obk:pattern-type-lit-oflo-checks, r=compiler-errors

Fix accidentally not emitting overflowing literals lints anymore in patterns

This was regressed in rust-lang#134228 (not in beta yet).

The issue was that previously we nested `hir::Expr` inside `hir::PatKind::Lit`, so it was linted by the expression code.

So now I've set it up for visitors to be able to directly visit literals and get all literals
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Feb 7, 2025
…piler-errors

Fix accidentally not emitting overflowing literals lints anymore in patterns

This was regressed in rust-lang/rust#134228 (not in beta yet).

The issue was that previously we nested `hir::Expr` inside `hir::PatKind::Lit`, so it was linted by the expression code.

So now I've set it up for visitors to be able to directly visit literals and get all literals
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