forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support type variable definitions in quoted patterns
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] => case '[type f[X]; f] => case '[type f <: AnyKind; f] => ``` Fixes scala#10864 Fixes scala#11738
- Loading branch information
1 parent
3958ee7
commit bf472e3
Showing
14 changed files
with
205 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
tests/neg-custom-args/no-experimental/sip-53-exprimental-b.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import scala.quoted.* | ||
|
||
def empty[K <: AnyKind : Type](using Quotes): Type[?] = | ||
Type.of[K] match | ||
case '[type t; `t`] => Type.of[t] // error | ||
case '[type f[X]; `f`] => Type.of[f] // error | ||
case '[type f[X <: Int, Y]; `f`] => Type.of[f] // error | ||
case '[type k <: AnyKind; `k` ] => Type.of[k] // error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import scala.quoted.* | ||
def types(t: Type[?])(using Quotes) = t match { | ||
case '[ type t; Int ] => | ||
case '[ type t <: Int; Int ] => | ||
case '[ type t >: 1 <: Int; Int ] => | ||
case '[ type t = Int; Int ] => // error | ||
case '[ type t = scala.Int; Int ] => // error | ||
case '[ type f[t] <: List[Any]; Int ] => | ||
case '[ type f[t <: Int] <: List[Any]; Int ] => | ||
case '[ type f[t] = List[Any]; Int ] => // error | ||
} | ||
|
||
def expressions(x: Expr[Any])(using Quotes) = x match { | ||
case '{ type t; () } => | ||
case '{ type t <: Int; () } => | ||
case '{ type t >: 1 <: Int; () } => | ||
case '{ type t = Int; () } => // error | ||
case '{ type t = scala.Int; () } => // error | ||
case '{ type f[t] <: List[Any]; () } => | ||
case '{ type f[t <: Int] <: List[Any]; () } => | ||
case '{ type f[t] = List[Any]; () } => // error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import scala.quoted._ | ||
|
||
case class T(t: Type[_]) | ||
|
||
object T { | ||
def impl[T <: AnyKind](using tt: Type[T])(using Quotes): Expr[Unit] = { | ||
val t = T(tt) | ||
t.t match | ||
case '[type x <: AnyKind; x] => // ok | ||
case _ => quotes.reflect.report.error("not ok :(") | ||
'{} | ||
} | ||
|
||
inline def run[T <: AnyKind] = ${ impl[T] } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
def test = | ||
T.run[List] | ||
T.run[Map] | ||
T.run[Tuple22] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import scala.quoted._ | ||
|
||
case class T(t: Type[_]) | ||
|
||
object T { | ||
def impl[T <: AnyKind](using tt: Type[T])(using Quotes): Expr[Unit] = { | ||
val t = T(tt) | ||
t.t match | ||
case '[type x; x] => | ||
assert(Type.show[x] == "scala.Int", Type.show[x]) | ||
case '[type f[X]; f] => | ||
assert(Type.show[f] == "[A >: scala.Nothing <: scala.Any] => scala.collection.immutable.List[A]", Type.show[f]) | ||
case '[type f[X <: Int]; f] => | ||
assert(Type.show[f] == "[T >: scala.Nothing <: scala.Int] => C[T]", Type.show[f]) | ||
case '[type f <: AnyKind; f] => | ||
assert(Type.show[f] == "[K >: scala.Nothing <: scala.Any, V >: scala.Nothing <: scala.Any] => scala.collection.immutable.Map[K, V]", Type.show[f]) | ||
'{} | ||
} | ||
|
||
inline def run[T <: AnyKind] = ${ impl[T] } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
@main | ||
def run = | ||
T.run[Int] | ||
T.run[C] | ||
T.run[List] | ||
T.run[Map] | ||
|
||
class C[T <: Int] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import scala.quoted.* | ||
|
||
def blah[A](using Quotes, Type[A]): Expr[Unit] = | ||
Type.of[A] match | ||
case '[h *: t] => println(s"h = ${Type.show[h]}, t = ${Type.show[t]}") // ok | ||
case '[type f[X]; f[a]] => println(s"f = ${Type.show[f]}, a = ${Type.show[a]}") // error | ||
case _ => | ||
'{()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import scala.quoted.* | ||
def types(t: Type[?])(using Quotes) = t match { | ||
case '[ | ||
type t; | ||
t | ||
] => | ||
|
||
case '[ | ||
type t | ||
t | ||
] => | ||
|
||
case '[ | ||
type t | ||
List[t] | ||
] => | ||
|
||
case '[ | ||
type t; | ||
type u; | ||
Map[t, u] | ||
] => | ||
|
||
case '[ | ||
type t | ||
type u | ||
Map[t, u] | ||
] => | ||
|
||
case '[ | ||
type t; type u | ||
t => u | ||
] => | ||
} | ||
|
||
def expressions(x: Expr[Any])(using Quotes) = x match { | ||
case '{ | ||
type t; | ||
$x: t | ||
} => | ||
|
||
case '{ | ||
type t | ||
$x: t | ||
} => | ||
|
||
case '{ | ||
type t; | ||
List() | ||
} => | ||
|
||
case '{ | ||
type t | ||
List() | ||
} => | ||
|
||
case '{ | ||
type t | ||
type u | ||
Map.empty[t, u] | ||
} => | ||
} |