Skip to content

Commit dc238f4

Browse files
committed
New match kinds: inline, implicit
Introduce trees for inline and implicit matches
1 parent c8398e8 commit dc238f4

File tree

6 files changed

+64
-41
lines changed

6 files changed

+64
-41
lines changed

compiler/src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,11 +511,18 @@ object Trees {
511511
}
512512

513513
/** selector match { cases } */
514-
case class Match[-T >: Untyped] private[ast] (selector: Tree[T], cases: List[CaseDef[T]])
514+
case class Match[-T >: Untyped] private[ast] (selector: Tree[T], cases: List[CaseDef[T]])(val kind: MatchKind)
515515
extends TermTree[T] {
516516
type ThisTree[-T >: Untyped] = Match[T]
517517
}
518518

519+
type MatchKind = Int
520+
object MatchKind {
521+
val Regular = 0
522+
val Inline = 1
523+
val Implicit = 2
524+
}
525+
519526
/** case pat if guard => body; only appears as child of a Match */
520527
case class CaseDef[-T >: Untyped] private[ast] (pat: Tree[T], guard: Tree[T], body: Tree[T])
521528
extends Tree[T] {
@@ -1036,7 +1043,9 @@ object Trees {
10361043
case _ => finalize(tree, untpd.Closure(env, meth, tpt))
10371044
}
10381045
def Match(tree: Tree)(selector: Tree, cases: List[CaseDef])(implicit ctx: Context): Match = tree match {
1039-
case tree: Match if (selector eq tree.selector) && (cases eq tree.cases) => tree
1046+
case tree: Match =>
1047+
if ((selector eq tree.selector) && (cases eq tree.cases)) tree
1048+
else finalize(tree, untpd.Match(selector, cases, tree.kind))
10401049
case _ => finalize(tree, untpd.Match(selector, cases))
10411050
}
10421051
def CaseDef(tree: Tree)(pat: Tree, guard: Tree, body: Tree)(implicit ctx: Context): CaseDef = tree match {

compiler/src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
118118
def CaseDef(pat: Tree, guard: Tree, body: Tree)(implicit ctx: Context): CaseDef =
119119
ta.assignType(untpd.CaseDef(pat, guard, body), pat, body)
120120

121-
def Match(selector: Tree, cases: List[CaseDef])(implicit ctx: Context): Match =
121+
def Match(selector: Tree, cases: List[CaseDef], kind: MatchKind = MatchKind.Regular)(implicit ctx: Context): Match =
122122
ta.assignType(untpd.Match(selector, cases), selector, cases)
123123

124124
def Labeled(bind: Bind, expr: Tree)(implicit ctx: Context): Labeled =

compiler/src/dotty/tools/dotc/ast/untpd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
270270
def Block(stats: List[Tree], expr: Tree): Block = new Block(stats, expr)
271271
def If(cond: Tree, thenp: Tree, elsep: Tree): If = new If(cond, thenp, elsep)
272272
def Closure(env: List[Tree], meth: Tree, tpt: Tree): Closure = new Closure(env, meth, tpt)
273-
def Match(selector: Tree, cases: List[CaseDef]): Match = new Match(selector, cases)
273+
def Match(selector: Tree, cases: List[CaseDef], kind: MatchKind = MatchKind.Regular): Match = new Match(selector, cases)(kind)
274274
def CaseDef(pat: Tree, guard: Tree, body: Tree): CaseDef = new CaseDef(pat, guard, body)
275275
def Labeled(bind: Bind, expr: Tree): Labeled = new Labeled(bind, expr)
276276
def Return(expr: Tree, from: Tree): Return = new Return(expr, from)

compiler/src/dotty/tools/dotc/core/tasty/TastyFormat.scala

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Standard-Section: "ASTs" TopLevelStat*
9090
LAMBDA Length meth_Term target_Type?
9191
IF Length cond_Term then_Term else_Term
9292
MATCH Length sel_Term CaseDef*
93+
INLINEMATCH Length (sel_Term | IMPLICIT) CaseDef*
9394
TRY Length expr_Term CaseDef* finalizer_Term?
9495
RETURN Length meth_ASTRef expr_Term?
9596
WHILE Length cond_Term body_Term
@@ -236,7 +237,7 @@ Standard Section: "Comments" Comment*
236237
object TastyFormat {
237238

238239
final val header: Array[Int] = Array(0x5C, 0xA1, 0xAB, 0x1F)
239-
val MajorVersion: Int = 11
240+
val MajorVersion: Int = 13
240241
val MinorVersion: Int = 0
241242

242243
/** Tags used to serialize names */
@@ -380,39 +381,41 @@ object TastyFormat {
380381
final val IF = 141
381382
final val LAMBDA = 142
382383
final val MATCH = 143
383-
final val RETURN = 144
384-
final val WHILE = 145
385-
final val TRY = 146
386-
final val INLINED = 147
387-
final val SELECTouter = 148
388-
final val REPEATED = 149
389-
final val BIND = 150
390-
final val ALTERNATIVE = 151
391-
final val UNAPPLY = 152
392-
final val ANNOTATEDtype = 153
393-
final val ANNOTATEDtpt = 154
394-
final val CASEDEF = 155
395-
final val TEMPLATE = 156
396-
final val SUPER = 157
397-
final val SUPERtype = 158
398-
final val REFINEDtype = 159
399-
final val REFINEDtpt = 160
400-
final val APPLIEDtype = 161
401-
final val APPLIEDtpt = 162
402-
final val TYPEBOUNDS = 163
403-
final val TYPEBOUNDStpt = 164
404-
final val ANDtype = 165
405-
final val ANDtpt = 166
406-
final val ORtype = 167
407-
final val ORtpt = 168
408-
final val POLYtype = 169
409-
final val TYPELAMBDAtype = 170
410-
final val LAMBDAtpt = 171
411-
final val PARAMtype = 172
412-
final val ANNOTATION = 173
413-
final val TERMREFin = 174
414-
final val TYPEREFin = 175
415-
final val OBJECTDEF = 176
384+
final val INLINEMATCH = 144
385+
386+
final val RETURN = 146
387+
final val WHILE = 147
388+
final val TRY = 148
389+
final val INLINED = 149
390+
final val SELECTouter = 150
391+
final val REPEATED = 151
392+
final val BIND = 152
393+
final val ALTERNATIVE = 153
394+
final val UNAPPLY = 154
395+
final val ANNOTATEDtype = 155
396+
final val ANNOTATEDtpt = 156
397+
final val CASEDEF = 157
398+
final val TEMPLATE = 158
399+
final val SUPER = 159
400+
final val SUPERtype = 160
401+
final val REFINEDtype = 161
402+
final val REFINEDtpt = 162
403+
final val APPLIEDtype = 163
404+
final val APPLIEDtpt = 164
405+
final val TYPEBOUNDS = 165
406+
final val TYPEBOUNDStpt = 166
407+
final val ANDtype = 167
408+
final val ANDtpt = 168
409+
final val ORtype = 169
410+
final val ORtpt = 170
411+
final val POLYtype = 171
412+
final val TYPELAMBDAtype = 172
413+
final val LAMBDAtpt = 173
414+
final val PARAMtype = 174
415+
final val ANNOTATION = 175
416+
final val TERMREFin = 176
417+
final val TYPEREFin = 177
418+
final val OBJECTDEF = 178
416419

417420
// In binary: 101101EI
418421
// I = implicit method type
@@ -587,6 +590,7 @@ object TastyFormat {
587590
case IF => "IF"
588591
case LAMBDA => "LAMBDA"
589592
case MATCH => "MATCH"
593+
case INLINEMATCH => "INLINEMATCH"
590594
case RETURN => "RETURN"
591595
case WHILE => "WHILE"
592596
case INLINED => "INLINED"

compiler/src/dotty/tools/dotc/core/tasty/TreePickler.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,13 @@ class TreePickler(pickler: TastyPickler) {
424424
pickleTree(meth)
425425
if (tpt.tpe.exists) pickleTpt(tpt)
426426
}
427-
case Match(selector, cases) =>
428-
writeByte(MATCH)
429-
withLength { pickleTree(selector); cases.foreach(pickleTree) }
427+
case mtch @ Match(selector, cases) =>
428+
writeByte(if (mtch.kind == MatchKind.Regular) MATCH else INLINEMATCH)
429+
withLength {
430+
if (mtch.kind == MatchKind.Implicit) writeByte(IMPLICIT)
431+
else pickleTree(selector)
432+
cases.foreach(pickleTree)
433+
}
430434
case CaseDef(pat, guard, rhs) =>
431435
writeByte(CASEDEF)
432436
withLength { pickleTree(pat); pickleTree(rhs); pickleTreeUnlessEmpty(guard) }

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,12 @@ class TreeUnpickler(reader: TastyReader,
10931093
Closure(Nil, meth, tpt)
10941094
case MATCH =>
10951095
Match(readTerm(), readCases(end))
1096+
case INLINEMATCH =>
1097+
if (nextByte == IMPLICIT) {
1098+
readByte()
1099+
Match(EmptyTree, readCases(end), MatchKind.Implicit)
1100+
}
1101+
else Match(readTerm(), readCases(end), MatchKind.Inline)
10961102
case RETURN =>
10971103
val from = readSymRef()
10981104
val expr = ifBefore(end)(readTerm(), EmptyTree)

0 commit comments

Comments
 (0)