Skip to content

ClassTag-based pattern matching does not work with extractors #1991

Closed
@smarter

Description

@smarter
import scala.reflect.ClassTag

class A[Foo](implicit tag: ClassTag[Foo]) {
  object ExtractFoo {
    def unapply(foo: Foo): Boolean = true
  }

  def isFoo(x: Any) = x match {
    case ExtractFoo() => true
    case _ => false
  }
}

object Test {
  def main(args: Array[String]): Unit = {
    assert((new A[String]).isFoo("foo")) // OK
    assert(!(new A[String]).isFoo(42)) // OK in scalac, fails in Dotty
  }
}

The ClassTag handling is done in typedTyped but the extractor handling is done in typedUnapply where we create a Typed node using tpd.Typed which does not call typedTyped

This is needed to get the dotty-compiled shared backend to have the correct runtime behavior.

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