-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix HK quoted pattern type variables #16907
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
Fix HK quoted pattern type variables #16907
Conversation
25ddf81 to
b0dd16b
Compare
| */ | ||
| def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutinee: Type[?])(using pattern: Type[?]): Option[Tup] | ||
| // FIXME: is this change TASTy binary compatible? Or we need to create a new version of the interface? | ||
| def unapply[TypeBindings, Tup <: Tuple](scrutinee: Type[?])(using pattern: Type[?]): Option[Tup] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's no way it could have been overridden in a subclass, then it is indeed a backward compatible change. You can enlarge the bounds of a type parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is implemented in QuotesImpl in the compiler. I guess this means that it is not a TASTy compatible change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In second thought, this class can only be extended by the compiler. It must be mixed in Quotes, but users do not have access to this class. It should be fine to change it.
The only scenario where this could break is if we recompile the compiler from TASTy but use a newer version of the standard library. This is not something that we would ever want to do.
b15546f to
7f7b0b5
Compare
| * @return None if it did not match, `Some(tup)` if it matched where `tup` contains `Expr[Ti]`` | ||
| */ | ||
| def unapply[TypeBindings <: Tuple, Tup <: Tuple](scrutinee: Expr[Any])(using pattern: Expr[Any]): Option[Tup] | ||
| def unapply[TypeBindings, Tup <: Tuple](scrutinee: Expr[Any])(using pattern: Expr[Any]): Option[Tup] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TypeBindings does not appear anywhere in the signature of the method. How is it used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used as a placeholder for type-binding definitions. We must create them all in TypeBindings to make them available in Tup.
Also see https://github.com/lampepfl/dotty/blob/main/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala#L352-L380
This doc needed an update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if the user manually calls unapply and pass a random type to TypeBindings?
7f7b0b5 to
a91eefd
Compare
Support explicit type variable definition in quoted patterns.
This allows users to set explicit bounds or use the binding twice.
Previously this was only possible on quoted expression patterns case '{ ... }.
```scala
case '[type x; `x`] =>
case '[type x; Map[`x`, `x`]] =>
case '[type x <: List[Any]; `x`] =>
```
In combination with scala#16907 it would also allow
```
case '[type f[X]; `f`] =>
case '[type f <: AnyKind; `f`] =>
```
and therefore solve scala#10864 and scala#11738
Support explicit type variable definition in quoted patterns.
This allows users to set explicit bounds or use the binding twice.
Previously this was only possible on quoted expression patterns case '{ ... }.
```scala
case '[type x; `x`] =>
case '[type x; Map[`x`, `x`]] =>
case '[type x <: List[Any]; `x`] =>
```
In combination with scala#16907 it would also allow
```scala
case '[type f[X]; `f`] =>
case '[type f <: AnyKind; `f`] =>
```
and therefore solve scala#10864 and scala#11738
Support explicit type variable definition in quoted patterns.
This allows users to set explicit bounds or use the binding twice.
Previously this was only possible on quoted expression patterns case '{ ... }.
```scala
case '[type x; `x`] =>
case '[type x; Map[`x`, `x`]] =>
case '[type x <: List[Any]; `x`] =>
```
In combination with scala#16907 it would also allow
```scala
case '[type f[X]; `f`] =>
case '[type f <: AnyKind; `f`] =>
```
and therefore solve scala#10864 and scala#11738
The issue was in the encoding into `{ExprMatchModule,TypeMatchModule}.unapply`.
Specifically with the `TypeBindings` argument. This arguments holds the
list of type variable definitions (`tpd.Bind` trees). We used a `Tuple`
to list all the types inside. The problem is that higher-kinded type
variables do not conform with the upper bounds of the tuple elements.
The solution is to use an HList with any-kinded elements.
a91eefd to
0ce5c32
Compare
The issue was in the encoding into
`{ExprMatchModule,TypeMatchModule}.unapply`. Specifically with the
`TypeBindings` argument. This arguments holds the list of type variable
definitions (`tpd.Bind` trees). We used a `Tuple` to list all the types
inside. The problem is that higher-kinded type variables do not conform
with the upper bounds of the tuple elements. The solution is to use an
HList with any-kinded elements.
Backport of #16907
The issue was in the encoding into
{ExprMatchModule,TypeMatchModule}.unapply. Specifically with theTypeBindingsargument. This arguments holds the list of type variable definitions (tpd.Bindtrees). We used aTupleto list all the types inside. The problem is that higher-kinded type variables do not conform with the upper bounds of the tuple elements. The solution is to use an HList with any-kinded elements.