Skip to content

pattern matcher generates illegal isInstanceOf[Null] for nullable parameter of unapply #23243

Open
@olhotak

Description

@olhotak

Compiler version

Scala 3.7.2-RC1-bin-20250520-baac46c-NIGHTLY, JVM (8)

Minimized code

object Extractor:
  def unapply(s: String|Null): Boolean = true
  
class A

def main =
  ("foo": (A|String)) match
    case Extractor() => println("foo matched")
    case _ => println("foo didn't match")

Output

$ scala-cli -S 3.nightly patmat.scala 
Compiling project (Scala 3.7.2-RC1-bin-20250520-baac46c-NIGHTLY, JVM (8))
[error] ./patmat.scala:9:10
[error] class Null cannot be used in runtime type tests
[error]     case Extractor() => println("foo matched")
[error]          ^^^^^^^^^^^
Error compiling project (Scala 3.7.2-RC1-bin-20250520-baac46c-NIGHTLY, JVM (8))

Expectation

Is a nullable type parameter of unapply even allowed?

If not, the error message should point out the unapply method.

If yes, the pattern matcher should not generate an illegal isInstanceOf[Null] and the example should compile.

Note: the issue happens both with and without -Yexplicit-nulls.

Note: a similar unapply appears in the compiler source in dotty.tools.backend.jvm.DottyBackendInterface.DeconstructorCommon.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions