Skip to content

Returning a non-tuple into a val pattern match compiles #14472

Closed
@Daenyth

Description

@Daenyth

Compiler version

scala 3.1.1

Minimized code

This code compiles, and fails at runtime with a MatchError:

val (n, m) = (1, 2) match {
  case _ => 1
  case null => (1,2)
}
val (n, m) = (1, 2) match {
  case pair if false => (1,2)
  case other => 1
}

Output

Fails with scala.MatchError: 1 (of class java.lang.Integer)

Expectation

The code should not compile; the return 1 cannot be destructured into a Tuple2

For comparison, the following code does not compile:

// cannot test if value of type (1 : Int) | (2 : Int) is a reference of class Tuple2
val (n, m) = (1, 2) match {
  case _ => 1
  case null => 2
}
// cannot test if value of type Int is a reference of class Tuple2
val (n, m) = (1, 2) match {
  case _ => 1
  case null => ???
}
// cannot test if value of type Int is a reference of class Tuple2
val (n, m) = (1, 2) match {
  case _ => 1
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions