Skip to content

Spurious missing TypeTest instance for a match type #13433

@neko-kai

Description

@neko-kai

Compiler version

3.0.1

Minimized code

https://scastie.scala-lang.org/R5e5jgGkSJ6fU7WTy4eqQw

import scala.reflect.TypeTest

type Matcher[A] = A match { case String => String }

def patternMatch[A](a: Any)(using tt: TypeTest[Any, Matcher[A]]): Option[Matcher[A]] = {
  // type T = RDF.Triple[Rdf]
  a match { 
    case res: Matcher[A] => Some(res)
    case _ => None
  }
}

def patternMatchWithAlias[A](a: Any)(using tt: TypeTest[Any, Matcher[A]]): Option[Matcher[A]] = {
  type T = Matcher[A]
  a match { 
    case res: T => Some(res)
    case _ => None
  }
}


@main def main = {
  println(patternMatch[String]("abc"))
  println(patternMatchWithAlias[String]("abc"))
  
  println(patternMatch[String](1))
  println(patternMatchWithAlias[String](1))
}

Output

the type test for Matcher[A] cannot be checked at runtime

Inside the body of patternMatch and subsequent failing type test:

Some(abc)
Some(abc)
Some(1)
None

However, the version of the code in patternMatchWithAlias works correctly and produces no warning. The only difference is a tautological alias

type T = Matcher[A]

and matching on the alias instead of the match type.

case res: T => res

Expectation

Expected both versions to work and to produce no spurious warnings:

the type test for Matcher[A] cannot be checked at runtime
the type test for Matcher[String] cannot be checked at runtime

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions