Skip to content

Add missing TASTy reflect ImplicitMatch #6165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
type Match = tpd.Match

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

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

type ImplicitMatch = tpd.Match

def matchImplicitMatch(x: Term)(implicit ctx: Context): Option[Match] = x match {
case x: tpd.Match if x.selector.isEmpty => Some(x)
case _ => None
}

def ImplicitMatch_cases(self: Match)(implicit ctx: Context): List[CaseDef] = self.cases

def ImplicitMatch_apply(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
withDefaultPos(ctx => tpd.Match(tpd.EmptyTree, cases)(ctx))

def ImplicitMatch_copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
tpd.cpy.Match(original)(tpd.EmptyTree, cases)

type Try = tpd.Try

def matchTry(x: Term)(implicit ctx: Context): Option[Try] = x match {
Expand Down
3 changes: 0 additions & 3 deletions compiler/test/dotc/pos-from-tasty.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,3 @@ i4006c.scala
# Not sure what's wring here
i4203.scala
t6278-synth-def.scala

# Need to print empty tree for implicit match
i5938.scala
10 changes: 0 additions & 10 deletions compiler/test/dotc/run-from-tasty.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,3 @@ eff-dependent.scala

# It seems we still harmonize types in fromTasty. Not sure where this happens
puzzle.scala

# Need to print empty tree for implicit match
implicitMatch.scala
typeclass-derivation1.scala
typeclass-derivation2.scala
typeclass-derivation2a.scala
typeclass-derivation3.scala
derive-generic.scala
deriving-interesting-prefixes.scala
companion-loading.scala
1 change: 1 addition & 0 deletions docs/docs/reference/other-new-features/tasty-reflect.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ TASTy Reflect provides the following types:
| +- Lambda
| +- If
| +- Match
| +- ImplicitMatch
| +- Try
| +- Return
| +- Repeated
Expand Down
4 changes: 4 additions & 0 deletions library/src/scala/tasty/reflect/Core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package scala.tasty.reflect
* | +- Lambda
* | +- If
* | +- Match
* | +- ImplicitMatch
* | +- Try
* | +- Return
* | +- Repeated
Expand Down Expand Up @@ -213,6 +214,9 @@ trait Core {
/** Tree representing a pattern match `x match { ... }` in the source code */
type Match = kernel.Match

/** Tree representing a pattern match `implicit match { ... }` in the source code */
type ImplicitMatch = kernel.ImplicitMatch

/** Tree representing a tyr catch `try x catch { ... } finally { ... }` in the source code */
type Try = kernel.Try

Expand Down
11 changes: 11 additions & 0 deletions library/src/scala/tasty/reflect/Kernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package scala.tasty.reflect
* | +- Lambda
* | +- If
* | +- Match
* | +- ImplicitMatch
* | +- Try
* | +- Return
* | +- Repeated
Expand Down Expand Up @@ -455,6 +456,16 @@ trait Kernel {
def Match_apply(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match
def Match_copy(original: Tree)(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match

/** Tree representing a pattern match `implicit match { ... }` in the source code */
type ImplicitMatch <: Term

def matchImplicitMatch(tree: Tree)(implicit ctx: Context): Option[ImplicitMatch]

def ImplicitMatch_cases(self: ImplicitMatch)(implicit ctx: Context): List[CaseDef]

def ImplicitMatch_apply(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch
def ImplicitMatch_copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch

/** Tree representing a tyr catch `try x catch { ... } finally { ... }` in the source code */
type Try <: Term

Expand Down
6 changes: 6 additions & 0 deletions library/src/scala/tasty/reflect/Printers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ trait Printers
this += "Term.Lambda(" += meth += ", " += tpt += ")"
case Term.Match(selector, cases) =>
this += "Term.Match(" += selector += ", " ++= cases += ")"
case Term.ImplicitMatch(cases) =>
this += "Term.ImplicitMatch(" ++= cases += ")"
case Term.Return(expr) =>
this += "Term.Return(" += expr += ")"
case Term.While(cond, body) =>
Expand Down Expand Up @@ -896,6 +898,10 @@ trait Printers
this += highlightKeyword(" match", color)
inBlock(printCases(cases, lineBreak()))

case Term.ImplicitMatch(cases) =>
this += highlightKeyword("implicit match", color)
inBlock(printCases(cases, lineBreak()))

case Term.Try(body, cases, finallyOpt) =>
this += highlightKeyword("try ", color)
printTree(body)
Expand Down
25 changes: 25 additions & 0 deletions library/src/scala/tasty/reflect/TreeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,27 @@ trait TreeOps extends Core {

}

object IsImplicitMatch {
/** Matches any ImplicitMatch and returns it */
def unapply(tree: Tree)(implicit ctx: Context): Option[ImplicitMatch] = kernel.matchImplicitMatch(tree)
}

/** Scala implicit `match` term */
object ImplicitMatch {

/** Creates a pattern match `implicit match { <cases: List[CaseDef]> }` */
def apply(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
kernel.ImplicitMatch_apply(cases)

def copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
kernel.ImplicitMatch_copy(original)(cases)

/** Matches a pattern match `implicit match { <cases: List[CaseDef]> }` */
def unapply(tree: Tree)(implicit ctx: Context): Option[List[CaseDef]] =
kernel.matchImplicitMatch(tree).map(_.cases)

}

object IsTry {
/** Matches any Try and returns it */
def unapply(tree: Tree)(implicit ctx: Context): Option[Try] = kernel.matchTry(tree)
Expand Down Expand Up @@ -701,6 +722,10 @@ trait TreeOps extends Core {
def cases(implicit ctx: Context): List[CaseDef] = kernel.Match_cases(self)
}

implicit class ImplicitMatchAPI(self: Term.ImplicitMatch) {
def cases(implicit ctx: Context): List[CaseDef] = kernel.ImplicitMatch_cases(self)
}

implicit class TryAPI(self: Term.Try) {
def body(implicit ctx: Context): Term = kernel.Try_body(self)
def cases(implicit ctx: Context): List[CaseDef] = kernel.Try_cases(self)
Expand Down