-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Scala 3 macro cannot match abstract types #16782
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
Comments
It fails for all abstract types import scala.quoted.*
object X:
opaque type Opaque[T] = Array[T]
type Invariant[T]
type Covariant[+T]
class InvariantClass[T]
class CovariantClass[+T]
import X.*
inline def testTypeMatch(): Unit =
${testTypeMatchExpr}
def testTypeMatchExpr(using Quotes) =
Type.of[Opaque[Int]] match
case '[Opaque[t]] => // fails
Type.of[Invariant[Int]] match
case '[Invariant[t]] => // fails
Type.of[Covariant[Int]] match
case '[Covariant[t]] => // fails
Type.of[CovariantClass[Int]] match
case '[CovariantClass[t]] =>
Type.of[InvariantClass[Int]] match
case '[InvariantClass[t]] =>
Type.of[CovariantClass[Int]] match
case '[CovariantClass[t]] =>
'{} def test = testTypeMatch() |
The issue is that we have a @abgruszecki do you have some insight on why this is the case when we use gadt constraints? |
It's because classes are known to be injective. It's justifiable to return What behaviour would you expect? |
Looking at the code, it feels to me like pattern matching on quoted types should be based on the structure of the types. The actual type information can be kept in |
Same issue in #15470 |
Note that this issue also affects something like: type Abs[T]
inline def test =
inline erasedValue[Abs[1]] match
case _: Abs[a] => valueOf[a] which is also incorrectly relying on the GADT logic currently: scala3/compiler/src/dotty/tools/dotc/inlines/InlineReducer.scala Lines 279 to 283 in d2bb85d
The same fix should apply to both form of type matching. We could either use regular subtype checking instead of GADT checking, or reuse the match type algorithm (https://docs.scala-lang.org/sips/match-types-spec.html) without the disjointness checks, but this might be constraining in some situations (/cc @sjrd). As a workaround (for the |
Uh oh!
There was an error while loading. Please reload this page.
Compiler version
3.2.2
Minimized code
https://scastie.scala-lang.org/rDUMGIiBQ5Km5vxE7TEl1Q
Output
Expectation
The text was updated successfully, but these errors were encountered: