scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.86k stars 1.06k forks source link

Lost type member bounds for refined pattern types #13820

Closed Linyxus closed 2 years ago

Linyxus commented 2 years ago

Compiler version

3.1.0

Minimized code

trait Expr { type T }

def foo[A](e: Expr { type T = A }) = e match
  case e1: Expr { type T <: Int } =>
    val i: Int = ??? : e1.T

Output

Found:    e1.T
Required: Int

Expectation

The code should be accepted, since e1.T is defined to be the subtype of Int. However, by inspecting the typer, we found that the pattern will be typed as _: Expr{type T = _} where _ is the wildcard type, and the information about the bounds <: Int gets lost.

soronpo commented 2 years ago

I'm not sure this is a bug. Same behavior in Scala 2. https://scastie.scala-lang.org/wvu8P6QkQAKujvrsXv2gDg

dwijnand commented 2 years ago

It's is/was a bug in Scala 2 too! There's no reason e1.T can't widen to Int there, that I can think of.

SethTisue commented 2 years ago

We should not allow this to compile unless we also ensure that it emits an unchecked warning.

abgruszecki commented 2 years ago

Unassigning myself since if there's anything in the compiler I will be taking a look at anytime soon, it will be issues directly related to GADTs.