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

Fix HK quoted pattern type variables #16907

Merged

Conversation

nicolasstucki
Copy link
Contributor

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.

@nicolasstucki nicolasstucki self-assigned this Feb 14, 2023
@nicolasstucki nicolasstucki force-pushed the fix-hk-quoted-pattern-type-variables branch from 25ddf81 to b0dd16b Compare February 14, 2023 12:23
@nicolasstucki nicolasstucki added the needs-minor-release This PR cannot be merged until the next minor release label Feb 14, 2023
@@ -40,5 +42,12 @@ trait QuoteMatching:
* @param pattern `Type[?]` containing the pattern tree
* @return None if it did not match, `Some(tup)` if it matched where `tup` contains `Type[Ti]``
*/
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]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sjrd @smarter Is this something that could potentially be TASTy compatible. It seems that all calls to this method generated by an older compiler should be unpicklable by the new one.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

@nicolasstucki nicolasstucki Feb 15, 2023

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.

@nicolasstucki nicolasstucki force-pushed the fix-hk-quoted-pattern-type-variables branch 5 times, most recently from b15546f to 7f7b0b5 Compare February 15, 2023 16:56
@nicolasstucki nicolasstucki marked this pull request as ready for review February 16, 2023 08:39
@nicolasstucki nicolasstucki added this to the 3.4.0 milestone Feb 16, 2023
@@ -27,7 +27,7 @@ trait QuoteMatching:
* @param pattern `Expr[Any]` containing the pattern tree
* @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]
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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?

@nicolasstucki nicolasstucki force-pushed the fix-hk-quoted-pattern-type-variables branch from 7f7b0b5 to a91eefd Compare February 17, 2023 08:01
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Feb 17, 2023
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
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Feb 17, 2023
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
nicolasstucki added a commit to dotty-staging/dotty that referenced this pull request Feb 17, 2023
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.
@nicolasstucki nicolasstucki force-pushed the fix-hk-quoted-pattern-type-variables branch from a91eefd to 0ce5c32 Compare February 20, 2023 14:33
@Kordyjan Kordyjan modified the milestones: 3.4.0, 3.3.0 backports Feb 20, 2023
@Kordyjan Kordyjan added the backport:accepted This PR needs to be backported, once it's been backported replace this tag by "backport:done" label Feb 20, 2023
@smarter smarter removed the needs-minor-release This PR cannot be merged until the next minor release label Feb 20, 2023
@nicolasstucki nicolasstucki merged commit 0b54a0b into scala:main Feb 21, 2023
@nicolasstucki nicolasstucki deleted the fix-hk-quoted-pattern-type-variables branch February 21, 2023 07:29
Kordyjan added a commit that referenced this pull request Feb 21, 2023
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
@Kordyjan Kordyjan added backport:done This PR was successfully backported. and removed backport:accepted This PR needs to be backported, once it's been backported replace this tag by "backport:done" labels Feb 21, 2023
@Kordyjan Kordyjan modified the milestones: 3.3.0 backports, 3.3.1, 3.3.0 Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:done This PR was successfully backported.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants