Skip to content

Commit 0443c55

Browse files
committed
Add missing TASTy reflect ImplicitMatch
1 parent f95ac7a commit 0443c55

File tree

8 files changed

+63
-14
lines changed

8 files changed

+63
-14
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
516516
type Match = tpd.Match
517517

518518
def matchMatch(x: Term)(implicit ctx: Context): Option[Match] = x match {
519-
case x: tpd.Match => Some(x)
519+
case x: tpd.Match if !x.selector.isEmpty => Some(x)
520520
case _ => None
521521
}
522522

@@ -529,6 +529,21 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
529529
def Match_copy(original: Tree)(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match =
530530
tpd.cpy.Match(original)(selector, cases)
531531

532+
type ImplicitMatch = tpd.Match
533+
534+
def matchImplicitMatch(x: Term)(implicit ctx: Context): Option[Match] = x match {
535+
case x: tpd.Match if x.selector.isEmpty => Some(x)
536+
case _ => None
537+
}
538+
539+
def ImplicitMatch_cases(self: Match)(implicit ctx: Context): List[CaseDef] = self.cases
540+
541+
def ImplicitMatch_apply(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
542+
withDefaultPos(ctx => tpd.Match(tpd.EmptyTree, cases)(ctx))
543+
544+
def ImplicitMatch_copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
545+
tpd.cpy.Match(original)(tpd.EmptyTree, cases)
546+
532547
type Try = tpd.Try
533548

534549
def matchTry(x: Term)(implicit ctx: Context): Option[Try] = x match {

compiler/test/dotc/pos-from-tasty.blacklist

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,3 @@ i4006c.scala
2424
# Not sure what's wring here
2525
i4203.scala
2626
t6278-synth-def.scala
27-
28-
# Need to print empty tree for implicit match
29-
i5938.scala

compiler/test/dotc/run-from-tasty.blacklist

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,3 @@ eff-dependent.scala
33

44
# It seems we still harmonize types in fromTasty. Not sure where this happens
55
puzzle.scala
6-
7-
# Need to print empty tree for implicit match
8-
implicitMatch.scala
9-
typeclass-derivation1.scala
10-
typeclass-derivation2.scala
11-
typeclass-derivation2a.scala
12-
typeclass-derivation3.scala
13-
derive-generic.scala
14-
deriving-interesting-prefixes.scala
15-
companion-loading.scala

docs/docs/reference/other-new-features/tasty-reflect.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ TASTy Reflect provides the following types:
108108
| +- Lambda
109109
| +- If
110110
| +- Match
111+
| +- ImplicitMatch
111112
| +- Try
112113
| +- Return
113114
| +- Repeated

library/src/scala/tasty/reflect/Core.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ package scala.tasty.reflect
2929
* | +- Lambda
3030
* | +- If
3131
* | +- Match
32+
* | +- ImplicitMatch
3233
* | +- Try
3334
* | +- Return
3435
* | +- Repeated
@@ -213,6 +214,9 @@ trait Core {
213214
/** Tree representing a pattern match `x match { ... }` in the source code */
214215
type Match = kernel.Match
215216

217+
/** Tree representing a pattern match `implicit match { ... }` in the source code */
218+
type ImplicitMatch = kernel.ImplicitMatch
219+
216220
/** Tree representing a tyr catch `try x catch { ... } finally { ... }` in the source code */
217221
type Try = kernel.Try
218222

library/src/scala/tasty/reflect/Kernel.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ package scala.tasty.reflect
2828
* | +- Lambda
2929
* | +- If
3030
* | +- Match
31+
* | +- ImplicitMatch
3132
* | +- Try
3233
* | +- Return
3334
* | +- Repeated
@@ -455,6 +456,16 @@ trait Kernel {
455456
def Match_apply(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match
456457
def Match_copy(original: Tree)(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match
457458

459+
/** Tree representing a pattern match `implicit match { ... }` in the source code */
460+
type ImplicitMatch <: Term
461+
462+
def matchImplicitMatch(tree: Tree)(implicit ctx: Context): Option[ImplicitMatch]
463+
464+
def ImplicitMatch_cases(self: ImplicitMatch)(implicit ctx: Context): List[CaseDef]
465+
466+
def ImplicitMatch_apply(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch
467+
def ImplicitMatch_copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch
468+
458469
/** Tree representing a tyr catch `try x catch { ... } finally { ... }` in the source code */
459470
type Try <: Term
460471

library/src/scala/tasty/reflect/Printers.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ trait Printers
177177
this += "Term.Lambda(" += meth += ", " += tpt += ")"
178178
case Term.Match(selector, cases) =>
179179
this += "Term.Match(" += selector += ", " ++= cases += ")"
180+
case Term.ImplicitMatch(cases) =>
181+
this += "Term.ImplicitMatch(" ++= cases += ")"
180182
case Term.Return(expr) =>
181183
this += "Term.Return(" += expr += ")"
182184
case Term.While(cond, body) =>
@@ -896,6 +898,10 @@ trait Printers
896898
this += highlightKeyword(" match", color)
897899
inBlock(printCases(cases, lineBreak()))
898900

901+
case Term.ImplicitMatch(cases) =>
902+
this += highlightKeyword("implicit match", color)
903+
inBlock(printCases(cases, lineBreak()))
904+
899905
case Term.Try(body, cases, finallyOpt) =>
900906
this += highlightKeyword("try ", color)
901907
printTree(body)

library/src/scala/tasty/reflect/TreeOps.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,27 @@ trait TreeOps extends Core {
511511

512512
}
513513

514+
object IsImplicitMatch {
515+
/** Matches any ImplicitMatch and returns it */
516+
def unapply(tree: Tree)(implicit ctx: Context): Option[ImplicitMatch] = kernel.matchImplicitMatch(tree)
517+
}
518+
519+
/** Scala implicit `match` term */
520+
object ImplicitMatch {
521+
522+
/** Creates a pattern match `implicit match { <cases: List[CaseDef]> }` */
523+
def apply(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
524+
kernel.ImplicitMatch_apply(cases)
525+
526+
def copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
527+
kernel.ImplicitMatch_copy(original)(cases)
528+
529+
/** Matches a pattern match `implicit match { <cases: List[CaseDef]> }` */
530+
def unapply(tree: Tree)(implicit ctx: Context): Option[List[CaseDef]] =
531+
kernel.matchImplicitMatch(tree).map(_.cases)
532+
533+
}
534+
514535
object IsTry {
515536
/** Matches any Try and returns it */
516537
def unapply(tree: Tree)(implicit ctx: Context): Option[Try] = kernel.matchTry(tree)
@@ -701,6 +722,10 @@ trait TreeOps extends Core {
701722
def cases(implicit ctx: Context): List[CaseDef] = kernel.Match_cases(self)
702723
}
703724

725+
implicit class ImplicitMatchAPI(self: Term.ImplicitMatch) {
726+
def cases(implicit ctx: Context): List[CaseDef] = kernel.ImplicitMatch_cases(self)
727+
}
728+
704729
implicit class TryAPI(self: Term.Try) {
705730
def body(implicit ctx: Context): Term = kernel.Try_body(self)
706731
def cases(implicit ctx: Context): List[CaseDef] = kernel.Try_cases(self)

0 commit comments

Comments
 (0)