-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ trait QuoteMatching: | |
* - `ExprMatch.unapply('{ f(0, myInt) })('{ f(patternHole[Int], patternHole[Int]) }, _)` | ||
* will return `Some(Tuple2('{0}, '{ myInt }))` | ||
* - `ExprMatch.unapply('{ f(0, "abc") })('{ f(0, patternHole[Int]) }, _)` | ||
* will return `None` due to the missmatch of types in the hole | ||
* will return `None` due to the mismatch of types in the hole | ||
* | ||
* Holes: | ||
* - scala.quoted.runtime.Patterns.patternHole[T]: hole that matches an expression `x` of type `Expr[U]` | ||
|
@@ -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] | ||
} | ||
|
||
val TypeMatch: TypeMatchModule | ||
|
@@ -40,5 +40,10 @@ 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] | ||
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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. This method is implemented in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. |
||
} | ||
|
||
object QuoteMatching: | ||
type KList | ||
type KCons[+H <: AnyKind, +T <: KList] <: KList | ||
type KNil <: KList |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import scala.quoted._ | ||
|
||
private def impl(x: Expr[Any])(using Quotes): Expr[Unit] = { | ||
x match | ||
case '{ foo[x] } => | ||
assert(Type.show[x] == "scala.Int", Type.show[x]) | ||
case '{ type f[X]; foo[`f`] } => | ||
assert(Type.show[f] == "[A >: scala.Nothing <: scala.Any] => scala.collection.immutable.List[A]", Type.show[f]) | ||
case '{ type f <: AnyKind; foo[`f`] } => | ||
assert(Type.show[f] == "[K >: scala.Nothing <: scala.Any, V >: scala.Nothing <: scala.Any] => scala.collection.immutable.Map[K, V]", Type.show[f]) | ||
case x => throw MatchError(x.show) | ||
'{} | ||
} | ||
|
||
inline def test(inline x: Any): Unit = ${ impl('x) } | ||
|
||
def foo[T <: AnyKind]: Any = ??? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@main | ||
def Test = | ||
test(foo[Int]) | ||
test(foo[List]) | ||
test(foo[Map]) |
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 inTup
.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 toTypeBindings
?