Skip to content

Update GivenMatch to SummonFrom #10434

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
24 changes: 12 additions & 12 deletions compiler/src/scala/quoted/runtime/impl/QuoteContextImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -795,28 +795,28 @@ class QuoteContextImpl private (using val ctx: Context) extends QuoteContext, Qu
end extension
end MatchMethodsImpl

type GivenMatch = tpd.Match
type SummonFrom = tpd.Match

object GivenMatchTypeTest extends TypeTest[Tree, GivenMatch]:
def unapply(x: Tree): Option[GivenMatch & x.type] = x match
object SummonFromTypeTest extends TypeTest[Tree, SummonFrom]:
def unapply(x: Tree): Option[SummonFrom & x.type] = x match
case x: (tpd.Match & x.type) if x.selector.isEmpty => Some(x)
case _ => None
end GivenMatchTypeTest
end SummonFromTypeTest

object GivenMatch extends GivenMatchModule:
def apply(cases: List[CaseDef]): GivenMatch =
object SummonFrom extends SummonFromModule:
def apply(cases: List[CaseDef]): SummonFrom =
withDefaultPos(tpd.Match(tpd.EmptyTree, cases))
def copy(original: Tree)(cases: List[CaseDef]): GivenMatch =
def copy(original: Tree)(cases: List[CaseDef]): SummonFrom =
tpd.cpy.Match(original)(tpd.EmptyTree, cases)
def unapply(x: GivenMatch): Option[List[CaseDef]] =
def unapply(x: SummonFrom): Option[List[CaseDef]] =
Some(x.cases)
end GivenMatch
end SummonFrom

object GivenMatchMethodsImpl extends GivenMatchMethods:
extension (self: GivenMatch):
object SummonFromMethodsImpl extends SummonFromMethods:
extension (self: SummonFrom):
def cases: List[CaseDef] = self.cases
end extension
end GivenMatchMethodsImpl
end SummonFromMethodsImpl

type Try = tpd.Try

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ object Extractors {
this += "Closure(" += meth += ", " += tpt += ")"
case Match(selector, cases) =>
this += "Match(" += selector += ", " ++= cases += ")"
case GivenMatch(cases) =>
this += "GivenMatch(" ++= cases += ")"
case SummonFrom(cases) =>
this += "SummonFrom(" ++= cases += ")"
case Return(expr, from) =>
this += "Return(" += expr += ", " += from += ")"
case While(cond, body) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,8 @@ object SourceCode {
this += highlightKeyword(" match")
inBlock(printCases(cases, lineBreak()))

case GivenMatch(cases) =>
this += highlightKeyword("given match") // TODO: drop
case SummonFrom(cases) =>
this += highlightKeyword("summonFrom ")
inBlock(printCases(cases, lineBreak()))

case Try(body, cases, finallyOpt) =>
Expand Down
31 changes: 15 additions & 16 deletions library/src/scala/quoted/QuoteContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ trait QuoteContext { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
* | +- Closure
* | +- If
* | +- Match
* | +- GivenMatch
* | +- SummonFrom
* | +- Try
* | +- Return
* | +- Repeated
Expand Down Expand Up @@ -578,7 +578,6 @@ trait QuoteContext { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
*/
def unique(qualifier: Term, name: String): Select

// TODO rename, this returns an Apply and not a Select
/** Call an overloaded method with the given type and term parameters */
def overloaded(qualifier: Term, name: String, targs: List[TypeRepr], args: List[Term]): Apply

Expand Down Expand Up @@ -1031,34 +1030,34 @@ trait QuoteContext { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
end extension
end MatchMethods

/** Tree representing a pattern match `given match { ... }` in the source code */ // TODO: drop
type GivenMatch <: Term
/** Tree representing a summoning match `summonFrom { ... }` in the source code */
type SummonFrom <: Term

given TypeTest[Tree, GivenMatch] = GivenMatchTypeTest
protected val GivenMatchTypeTest: TypeTest[Tree, GivenMatch]
given TypeTest[Tree, SummonFrom] = SummonFromTypeTest
protected val SummonFromTypeTest: TypeTest[Tree, SummonFrom]

/** Scala implicit `match` term */
val GivenMatch: GivenMatchModule
val SummonFrom: SummonFromModule

trait GivenMatchModule { this: GivenMatch.type =>
trait SummonFromModule { this: SummonFrom.type =>

/** Creates a pattern match `given match { <cases: List[CaseDef]> }` */
def apply(cases: List[CaseDef]): GivenMatch
def apply(cases: List[CaseDef]): SummonFrom

def copy(original: Tree)(cases: List[CaseDef]): GivenMatch
def copy(original: Tree)(cases: List[CaseDef]): SummonFrom

/** Matches a pattern match `given match { <cases: List[CaseDef]> }` */
def unapply(x: GivenMatch): Option[List[CaseDef]]
def unapply(x: SummonFrom): Option[List[CaseDef]]
}

given GivenMatchMethods as GivenMatchMethods = GivenMatchMethodsImpl
protected val GivenMatchMethodsImpl: GivenMatchMethods
given SummonFromMethods as SummonFromMethods = SummonFromMethodsImpl
protected val SummonFromMethodsImpl: SummonFromMethods

trait GivenMatchMethods:
extension (self: GivenMatch):
trait SummonFromMethods:
extension (self: SummonFrom):
def cases: List[CaseDef]
end extension
end GivenMatchMethods
end SummonFromMethods

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