Skip to content

Commit 40c2922

Browse files
committed
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
1 parent f128063 commit 40c2922

File tree

10 files changed

+77
-6
lines changed

10 files changed

+77
-6
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,19 @@ object Parsers {
16321632
t
16331633
end typ
16341634

1635+
/** TypeBlock ::= {TypeBlockStat semi} Type
1636+
* TypeBlockStat ::= ‘type’ {nl} TypeDcl
1637+
*/
1638+
def typeBlock(): Tree =
1639+
val tDefs = new ListBuffer[Tree]
1640+
while in.token == TYPE do
1641+
val mods = defAnnotsMods(modifierTokens)
1642+
tDefs += typeDefOrDcl(in.offset, in.skipToken(mods))
1643+
acceptStatSep()
1644+
val tpt = typ()
1645+
if tDefs.isEmpty then tpt else Block(tDefs.toList, tpt)
1646+
1647+
16351648
private def makeKindProjectorTypeDef(name: TypeName): TypeDef = {
16361649
val isVarianceAnnotated = name.startsWith("+") || name.startsWith("-")
16371650
// We remove the variance marker from the name without passing along the specified variance at all
@@ -2495,7 +2508,7 @@ object Parsers {
24952508
atSpan(in.skipToken()) {
24962509
withinStaged(StageKind.Quoted | (if (location.inPattern) StageKind.QuotedPattern else 0)) {
24972510
Quote {
2498-
if (in.token == LBRACKET) inBrackets(typ())
2511+
if (in.token == LBRACKET) inBrackets(typeBlock())
24992512
else stagedBlock()
25002513
}
25012514
}

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ trait QuotesAndSplices {
198198
* )
199199
* ```
200200
*/
201-
private def splitQuotePattern(quoted: Tree)(using Context): (Map[Symbol, Bind], Tree, List[Tree]) = {
201+
private def splitQuotePattern(quoted: Tree)(using Context): (Map[Symbol, Tree], Tree, List[Tree]) = {
202202
val ctx0 = ctx
203203

204-
val typeBindings: collection.mutable.Map[Symbol, Bind] = collection.mutable.Map.empty
205-
def getBinding(sym: Symbol): Bind =
204+
val typeBindings: collection.mutable.Map[Symbol, Tree] = collection.mutable.Map.empty
205+
def getBinding(sym: Symbol): Tree =
206206
typeBindings.getOrElseUpdate(sym, {
207207
val bindingBounds = sym.info
208208
val bsym = newPatternBoundSymbol(sym.name.toString.stripPrefix("$").toTypeName, bindingBounds, quoted.span)
@@ -396,7 +396,10 @@ trait QuotesAndSplices {
396396
val quoted0 = desugar.quotedPattern(quoted, untpd.TypedSplice(TypeTree(quotedPt)))
397397
val quoteCtx = quoteContext.addMode(Mode.QuotedPattern).retractMode(Mode.Pattern)
398398
val quoted1 =
399-
if quoted.isType then typedType(quoted0, WildcardType)(using quoteCtx)
399+
if quoted.isType then typedType(quoted0, WildcardType)(using quoteCtx) match
400+
case quoted1 @ Block(stats, Typed(tpt, _)) => cpy.Block(quoted1)(stats, tpt)
401+
case quoted1 => quoted1
402+
400403
else typedExpr(quoted0, WildcardType)(using quoteCtx)
401404

402405
val (typeBindings, shape, splices) = splitQuotePattern(quoted1)

docs/_docs/reference/metaprogramming/macros.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ def empty[T: Type]: Expr[T] =
530530
Type.of[T] match
531531
case '[String] => '{ "" }
532532
case '[List[t]] => '{ List.empty[t] }
533+
case '[type t <: Option[Int]; List[`t`]] => '{ List.empty[t] }
533534
...
534535
```
535536

docs/_docs/reference/syntax.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ ColonArgument ::= colon [LambdaStart]
277277
LambdaStart ::= FunParams (‘=>’ | ‘?=>’)
278278
| HkTypeParamClause ‘=>’
279279
Quoted ::= ‘'’ ‘{’ Block ‘}’
280-
| ‘'’ ‘[’ Type ‘]’
280+
| ‘'’ ‘[’ TypeBlock ‘]’
281281
ExprSplice ::= spliceId -- if inside quoted block
282282
| ‘$’ ‘{’ Block ‘}’ -- unless inside quoted pattern
283283
| ‘$’ ‘{’ Pattern ‘}’ -- when inside quoted pattern
@@ -296,6 +296,8 @@ BlockStat ::= Import
296296
| Extension
297297
| Expr1
298298
| EndMarker
299+
TypeBlock ::= {TypeBlockStat semi} Type
300+
TypeBlockStat ::= ‘type’ {nl} TypeDcl
299301
300302
ForExpr ::= ‘for’ ‘(’ Enumerators0 ‘)’ {nl} [‘do‘ | ‘yield’] Expr
301303
| ‘for’ ‘{’ Enumerators0 ‘}’ {nl} [‘do‘ | ‘yield’] Expr

tests/pos-macros/i10864/Macro_1.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted._
2+
3+
case class T(t: Type[_])
4+
5+
object T {
6+
def impl[T <: AnyKind](using tt: Type[T])(using Quotes): Expr[Unit] = {
7+
val t = T(tt)
8+
t.t match
9+
case '[type x <: AnyKind; `x`] => // ok
10+
case _ => quotes.reflect.report.error("not ok :(")
11+
'{}
12+
}
13+
14+
inline def run[T <: AnyKind] = ${ impl[T] }
15+
}

tests/pos-macros/i10864/Test_2.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def test =
2+
T.run[List]
3+
T.run[Map]
4+
T.run[Tuple22]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import scala.quoted._
2+
3+
case class T(t: Type[_])
4+
5+
object T {
6+
def impl[T <: AnyKind](using tt: Type[T])(using Quotes): Expr[Unit] = {
7+
val t = T(tt)
8+
t.t match
9+
case '[type x; `x`] =>
10+
assert(Type.show[x] == "scala.Int", Type.show[x])
11+
case '[type f[X]; `f`] =>
12+
assert(Type.show[f] == "[A >: scala.Nothing <: scala.Any] => scala.collection.immutable.List[A]", Type.show[f])
13+
case '[type f <: AnyKind; `f`] =>
14+
assert(Type.show[f] == "[K >: scala.Nothing <: scala.Any, V >: scala.Nothing <: scala.Any] => scala.collection.immutable.Map[K, V]", Type.show[f])
15+
'{}
16+
}
17+
18+
inline def run[T <: AnyKind] = ${ impl[T] }
19+
}

tests/pos-macros/i10864a/Test_2.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@main
2+
def run =
3+
T.run[Int]
4+
T.run[List]
5+
T.run[Map]

tests/pos-macros/i11738.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.quoted.*
2+
3+
def blah[A](using Quotes, Type[A]): Expr[Unit] =
4+
Type.of[A] match
5+
case '[h *: t] => println(s"h = ${Type.show[h]}, t = ${Type.show[t]}") // ok
6+
case '[type f[X]; `f`[a]] => println(s"f = ${Type.show[f]}, a = ${Type.show[a]}") // error
7+
case _ =>
8+
'{()}

tests/pos-macros/i7264.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ class Foo {
33
def f[T2](t: Type[T2])(using Quotes) = t match {
44
case '[ *:[Int, t2] ] =>
55
Type.of[ *:[Int, t2] ]
6+
case '[ type t <: Tuple; *:[`t`, `t`] ] =>
67
}
78
}

0 commit comments

Comments
 (0)