-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 #10864 Fixes #11738
- Loading branch information
1 parent
3958ee7
commit 6cb6ae3
Showing
16 changed files
with
226 additions
and
16 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,12 @@ | ||
-- Warning: tests/neg-macros/quote-type-variable-no-inference.scala:5:17 ----------------------------------------------- | ||
5 | case '[ F[t, t] ] => // warn // error | ||
| ^ | ||
| Ignored bound <: Double | ||
| | ||
| Consider defining bounds explicitly `'{ type t <: Int & Double; ... }` | ||
-- [E057] Type Mismatch Error: tests/neg-macros/quote-type-variable-no-inference.scala:5:15 ---------------------------- | ||
5 | case '[ F[t, t] ] => // warn // error | ||
| ^ | ||
| Type argument t does not conform to upper bound Double | ||
| | ||
| longer explanation available when compiling with `-explain` |
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 test(x: Type[?])(using Quotes) = | ||
x match | ||
case '[ F[t, t] ] => // warn // error | ||
case '[ type u <: Int & Double; F[u, u] ] => | ||
|
||
type F[x <: Int, y <: Double] |
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] | ||
} => | ||
} |