rust-lang / rust-lang/rust

`unreachable_pattern` could get more specific if the preempting pattern resembles a `const`

Open
#129,631 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-lints A-patterns C-bug L-unreachable_patterns T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

matchallways picks the first arm if matching value is not defined/unknown.
I was using constto define the values for the arms.
But the const were places inside the main() and the match expression was in a separate function.

I tried this code:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4b8fce2e782604f738c4fb9229b2a29b

Uncommenting the first 2 const gives the wanted results.

I expected to see this happen:

That the compiler finds the const values or stop compiling because the matching values are unknown.

Instead, this happened:

It just compiles.
But it gives some warnings like warning: unreachable pattern.
It just runs but with the wrong values.

See play link. See the behavior on all the versions "Stable", "Beta" and "Nightly".

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked Rust Playground reproducer on Stable, Beta, and Nightly, including the versions with the first two const declarations uncommented. The issue names no source files or tests; done should establish whether unknown const values in a separate function should change match analysis or produce a compilation error, and should include regression coverage if a behavior change is accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.