Skip to content

Matching BigInt against Int literal pattern leads to warning #23316

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

Open
t9dupuy opened this issue Jun 4, 2025 · 1 comment
Open

Matching BigInt against Int literal pattern leads to warning #23316

t9dupuy opened this issue Jun 4, 2025 · 1 comment

Comments

@t9dupuy
Copy link

t9dupuy commented Jun 4, 2025

Compiler version

3.7.0

Minimized code

//> using scala 3.7.0

@main def main(): Unit =
  BigInt(1) match
    case 1 => println("matched")
    case _ => println("didn't match")

Output

compile

[warn] -- [E030] Match case Unreachable Warning: D:\scala\run\src\main\scala\Main.scala:7:9 
[warn] 7 |    case 1 => println("matched")
[warn]   |         ^
[warn]   |         Unreachable case

run

matched

Expectation

No warning.

@t9dupuy t9dupuy added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 4, 2025
@som-snytt
Copy link
Contributor

Or, the warning is correct and pattern matching is broken. A literal pattern requires conformance, and an int is not a big int.

Welcome to Scala 2.13.16 (OpenJDK 64-Bit Server VM, Java 23.0.2).
Type in expressions for evaluation. Or try :help.

scala> BigInt(42) match { case 42 => "yes" case _ => "no" }
                               ^
       error: type mismatch;
        found   : Int(42)
        required: scala.math.BigInt

https://scala-lang.org/files/archive/spec/3.4/08-pattern-matching.html#literal-patterns

A related issue where patmat incorrectly defers to equals is #16821.

@som-snytt som-snytt added area:pattern-matching and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 4, 2025
@som-snytt som-snytt changed the title Matching Int against BigInt leads to false warning Matching BigInt against Int literal pattern leads to warning Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants