Skip to content

Pattern matching should retain the singleton type of the scrutinee #8083

Closed
@LPTK

Description

@LPTK

minimized code

This works in Scala 2.13, but fails in Dotty:

trait A {
  class B
}
abstract class Test2 { val a: A; val b: a.B }
object Test2 {
  def unapply(that: Test2): Option[(that.a.type, that.a.B)] = Some((that.a, that.b))
}
object anA extends A
object Test extends App {
  val t = new Test2 { val a = anA; val b = new a.B }
  t match {
    case Test2(u, v) =>
      u: A
      u: t.a.type // error
      v: t.a.B // error
  }
}

Scastie: https://scastie.scala-lang.org/nLrm92OqQgaolCvmPsf4ZA

Compilation output
      u: t.a.type // error
Found:    (u : (?1.a : A))
Required: (Test.t.a : A)

where:    ?1 is an unknown value of type Test2

      v: t.a.B // error
Found:    (v : ?1.a.B)
Required: Test.t.a.B

where:    ?1 is an unknown value of type Test2

It will probably be needed for #8073.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions