Skip to content

Allow const patterns of matches to contain pattern types #138393

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

Merged
merged 1 commit into from
Apr 15, 2025

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Mar 12, 2025

Trying to pattern match on a type containing a pattern type will currently fail with an ICE

error: internal compiler error: compiler/rustc_mir_build/src/builder/matches/test.rs:459:18: invalid type for non-scalar compare: (u32) is 1..
  --> src/main.rs:22:5
   |
22 |     TWO => {}
   |     ^^^

because the compiler tries to generate a MIR BinOp(Eq) operation on a pattern type, which is not supported. While we could support that, there are side effects of allowing this (none that would compile, but the compiler would simultaneously think it could == pattern types and that it could not because PartialEq is not implemented. So instead I change the logic for pattern matching to transmute pattern types to their base type before comparing.

r? @BoxyUwU

cc #123646 @scottmcm @joshtriplett

@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 Mar 12, 2025
@rustbot
Copy link
Collaborator

rustbot commented Mar 12, 2025

Some changes occurred in match lowering

cc @Nadrieril

@BoxyUwU BoxyUwU 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 Mar 26, 2025
@oli-obk oli-obk force-pushed the pattern-type-in-pattern branch from a464513 to 0fe88f5 Compare April 7, 2025 10:04
@oli-obk oli-obk 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 7, 2025
@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the pattern-type-in-pattern branch from 0fe88f5 to 0842836 Compare April 7, 2025 11:05
@@ -0,0 +1,26 @@
//! Check that pattern types don't implement traits of their base automatically.
//! Exceptions are `Clone` and `Copy`.
Copy link
Member

Choose a reason for hiding this comment

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

what makes clone/copy work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

they are builtin impls. added that as a comment here

@BoxyUwU BoxyUwU 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 Apr 14, 2025
@oli-obk oli-obk force-pushed the pattern-type-in-pattern branch from 0842836 to 71412c0 Compare April 14, 2025 14:27
@oli-obk oli-obk force-pushed the pattern-type-in-pattern branch from 71412c0 to 7ad1697 Compare April 14, 2025 14:27
@BoxyUwU
Copy link
Member

BoxyUwU commented Apr 14, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Apr 14, 2025

📌 Commit 7ad1697 has been approved by BoxyUwU

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 Apr 14, 2025
Zalathar added a commit to Zalathar/rust that referenced this pull request Apr 15, 2025
…=BoxyUwU

Allow const patterns of matches to contain pattern types

Trying to pattern match on a type containing a pattern type will currently fail with an ICE

```rust
error: internal compiler error: compiler/rustc_mir_build/src/builder/matches/test.rs:459:18: invalid type for non-scalar compare: (u32) is 1..
  --> src/main.rs:22:5
   |
22 |     TWO => {}
   |     ^^^
```

because the compiler tries to generate a MIR `BinOp(Eq)` operation on a pattern type, which is not supported. While we could support that, there are side effects of allowing this (none that would compile, but the compiler would simultaneously think it could `==` pattern types and that it could not because `PartialEq` is not implemented. So instead I change the logic for pattern matching to transmute pattern types to their base type before comparing.

r? `@BoxyUwU`

cc rust-lang#123646 `@scottmcm` `@joshtriplett`
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 15, 2025
Rollup of 12 pull requests

Successful merges:

 - rust-lang#138374 (Enable contracts for const functions)
 - rust-lang#138380 (ci: add runners for vanilla LLVM 20)
 - rust-lang#138393 (Allow const patterns of matches to contain pattern types)
 - rust-lang#139393 (rustdoc-json: Output target feature information)
 - rust-lang#139517 (std: sys: process: uefi: Use NULL stdin by default)
 - rust-lang#139554 (std: add Output::exit_ok)
 - rust-lang#139745 (Avoid unused clones in `Cloned<I>` and `Copied<I>`)
 - rust-lang#139757 (opt-dist: use executable-extension for host llvm-profdata)
 - rust-lang#139778 (Add test for issue 34834)
 - rust-lang#139783 (Use `compiletest-ignore-dir` for bootstrap self-tests)
 - rust-lang#139789 (do not unnecessarily leak auto traits in item bounds)
 - rust-lang#139791 (drop global where-bounds before merging candidates)

r? `@ghost`
`@rustbot` modify labels: rollup
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Apr 15, 2025
…=BoxyUwU

Allow const patterns of matches to contain pattern types

Trying to pattern match on a type containing a pattern type will currently fail with an ICE

```rust
error: internal compiler error: compiler/rustc_mir_build/src/builder/matches/test.rs:459:18: invalid type for non-scalar compare: (u32) is 1..
  --> src/main.rs:22:5
   |
22 |     TWO => {}
   |     ^^^
```

because the compiler tries to generate a MIR `BinOp(Eq)` operation on a pattern type, which is not supported. While we could support that, there are side effects of allowing this (none that would compile, but the compiler would simultaneously think it could `==` pattern types and that it could not because `PartialEq` is not implemented. So instead I change the logic for pattern matching to transmute pattern types to their base type before comparing.

r? ``@BoxyUwU``

cc rust-lang#123646 ``@scottmcm`` ``@joshtriplett``
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 15, 2025
Rollup of 17 pull requests

Successful merges:

 - rust-lang#138374 (Enable contracts for const functions)
 - rust-lang#138380 (ci: add runners for vanilla LLVM 20)
 - rust-lang#138393 (Allow const patterns of matches to contain pattern types)
 - rust-lang#139517 (std: sys: process: uefi: Use NULL stdin by default)
 - rust-lang#139554 (std: add Output::exit_ok)
 - rust-lang#139660 (compiletest: Add an experimental new executor to replace libtest)
 - rust-lang#139669 (Overhaul `AssocItem`)
 - rust-lang#139671 (Proc macro span API redesign: Replace proc_macro::SourceFile by Span::{file, local_file})
 - rust-lang#139750 (std/thread: Use default stack size from menuconfig for NuttX)
 - rust-lang#139772 (Remove `hir::Map`)
 - rust-lang#139785 (Let CStrings be either 1 or 2 byte aligned.)
 - rust-lang#139789 (do not unnecessarily leak auto traits in item bounds)
 - rust-lang#139791 (drop global where-bounds before merging candidates)
 - rust-lang#139798 (normalize: prefer `ParamEnv` over `AliasBound` candidates)
 - rust-lang#139822 (Fix: Map EOPNOTSUPP to ErrorKind::Unsupported on Unix)
 - rust-lang#139833 (Fix some HIR pretty-printing problems)
 - rust-lang#139836 (Basic tests of MPMC receiver cloning)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit aa9a80c into rust-lang:master Apr 15, 2025
6 checks passed
@rustbot rustbot added this to the 1.88.0 milestone Apr 15, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 15, 2025
Rollup merge of rust-lang#138393 - oli-obk:pattern-type-in-pattern, r=BoxyUwU

Allow const patterns of matches to contain pattern types

Trying to pattern match on a type containing a pattern type will currently fail with an ICE

```rust
error: internal compiler error: compiler/rustc_mir_build/src/builder/matches/test.rs:459:18: invalid type for non-scalar compare: (u32) is 1..
  --> src/main.rs:22:5
   |
22 |     TWO => {}
   |     ^^^
```

because the compiler tries to generate a MIR `BinOp(Eq)` operation on a pattern type, which is not supported. While we could support that, there are side effects of allowing this (none that would compile, but the compiler would simultaneously think it could `==` pattern types and that it could not because `PartialEq` is not implemented. So instead I change the logic for pattern matching to transmute pattern types to their base type before comparing.

r? ```@BoxyUwU```

cc rust-lang#123646 ```@scottmcm``` ```@joshtriplett```
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Apr 19, 2025
Rollup of 17 pull requests

Successful merges:

 - rust-lang#138374 (Enable contracts for const functions)
 - rust-lang#138380 (ci: add runners for vanilla LLVM 20)
 - rust-lang#138393 (Allow const patterns of matches to contain pattern types)
 - rust-lang#139517 (std: sys: process: uefi: Use NULL stdin by default)
 - rust-lang#139554 (std: add Output::exit_ok)
 - rust-lang#139660 (compiletest: Add an experimental new executor to replace libtest)
 - rust-lang#139669 (Overhaul `AssocItem`)
 - rust-lang#139671 (Proc macro span API redesign: Replace proc_macro::SourceFile by Span::{file, local_file})
 - rust-lang#139750 (std/thread: Use default stack size from menuconfig for NuttX)
 - rust-lang#139772 (Remove `hir::Map`)
 - rust-lang#139785 (Let CStrings be either 1 or 2 byte aligned.)
 - rust-lang#139789 (do not unnecessarily leak auto traits in item bounds)
 - rust-lang#139791 (drop global where-bounds before merging candidates)
 - rust-lang#139798 (normalize: prefer `ParamEnv` over `AliasBound` candidates)
 - rust-lang#139822 (Fix: Map EOPNOTSUPP to ErrorKind::Unsupported on Unix)
 - rust-lang#139833 (Fix some HIR pretty-printing problems)
 - rust-lang#139836 (Basic tests of MPMC receiver cloning)

r? `@ghost`
`@rustbot` modify labels: rollup
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.

5 participants