Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing unchecked warning for type pattern, invariant extends contravariant #12439

Open
lrytz opened this issue Aug 5, 2021 · 1 comment
Open
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) patmat
Milestone

Comments

@lrytz
Copy link
Member

lrytz commented Aug 5, 2021

From scala/scala#9672 (comment)

trait C[+T]
trait D[T] extends C[T] {
  def f(x: T): T
}
class E extends D[String] {
  def f(x: String) = x.trim
}

object Test {
  def foo[T](c: C[T], t: T): Any = c match {
    case d: D[T] => d.f(t)
    case _ => 0
  }
  def main(args: Array[String]): Unit = {
    foo[Any](new E, 1)
  }
}

Doesn't give an unchecked warning in Scala 2 (fixed in Scala 3) and runs into a CCE.

@lrytz lrytz added patmat fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) labels Aug 5, 2021
@lrytz lrytz added this to the Backlog milestone Aug 5, 2021
@lrytz
Copy link
Member Author

lrytz commented Aug 11, 2021

The method in question is propagateKnownTypes (https://github.com/scala/scala/blob/2149b975b5f39d6c9d8ab8431477d1fa6b60edda/src/compiler/scala/tools/nsc/typechecker/Checkable.scala#L98).

Related to #6944. Also related to this comment: https://github.com/scala/scala/blob/2149b975b5f39d6c9d8ab8431477d1fa6b60edda/src/compiler/scala/tools/nsc/typechecker/PatternTypers.scala#L246-L248

In Scala 3 the implementation for the unchecked warning is reusing the pattern type inference (https://github.com/lampepfl/dotty/blob/38b983c1b4a61d4c45f314ae895b95317c322c62/compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala#L101), which seems the right thing to do. I don't know gadt inference in Scala 2, but constrainPatternType seems potentially better grounded (scala/scala3#6398).

So the cost/benefit of fixing this "the right way" seems too high at least for me personally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) patmat
Projects
None yet
Development

No branches or pull requests

1 participant