Skip to content

Commit b6e4acf

Browse files
authored
Merge pull request #6165 from dotty-staging/fix-tasty-reflect-implicit-match
Add missing TASTy reflect ImplicitMatch
2 parents e0b3c81 + 0443c55 commit b6e4acf

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
@@ -522,7 +522,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
522522
type Match = tpd.Match
523523

524524
def matchMatch(x: Term)(implicit ctx: Context): Option[Match] = x match {
525-
case x: tpd.Match => Some(x)
525+
case x: tpd.Match if !x.selector.isEmpty => Some(x)
526526
case _ => None
527527
}
528528

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

538+
type ImplicitMatch = tpd.Match
539+
540+
def matchImplicitMatch(x: Term)(implicit ctx: Context): Option[Match] = x match {
541+
case x: tpd.Match if x.selector.isEmpty => Some(x)
542+
case _ => None
543+
}
544+
545+
def ImplicitMatch_cases(self: Match)(implicit ctx: Context): List[CaseDef] = self.cases
546+
547+
def ImplicitMatch_apply(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
548+
withDefaultPos(ctx => tpd.Match(tpd.EmptyTree, cases)(ctx))
549+
550+
def ImplicitMatch_copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
551+
tpd.cpy.Match(original)(tpd.EmptyTree, cases)
552+
538553
type Try = tpd.Try
539554

540555
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
@@ -461,6 +462,16 @@ trait Kernel {
461462
def Match_apply(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match
462463
def Match_copy(original: Tree)(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match
463464

465+
/** Tree representing a pattern match `implicit match { ... }` in the source code */
466+
type ImplicitMatch <: Term
467+
468+
def matchImplicitMatch(tree: Tree)(implicit ctx: Context): Option[ImplicitMatch]
469+
470+
def ImplicitMatch_cases(self: ImplicitMatch)(implicit ctx: Context): List[CaseDef]
471+
472+
def ImplicitMatch_apply(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch
473+
def ImplicitMatch_copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch
474+
464475
/** Tree representing a tyr catch `try x catch { ... } finally { ... }` in the source code */
465476
type Try <: Term
466477

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)