Skip to content

Commit 7eb84be

Browse files
committed
Expose GadtExpr/Constraint in Quotes?
Seems like a test around Exactors requires it? Can/should we leave out the GadtConstraint part? Unsure, never been here before.
1 parent 0ab760b commit 7eb84be

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,23 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
776776
end extension
777777
end BlockMethods
778778

779+
type GadtExpr = tpd.GadtExpr
780+
object GadtExprTypeTest extends TypeTest[Tree, GadtExpr]:
781+
def unapply(x: Tree): Option[GadtExpr & x.type] = x match
782+
case x: (tpd.GadtExpr & x.type) => Some(x)
783+
case _ => None
784+
object GadtExpr extends GadtExprModule:
785+
def apply(gadt: GadtConstraint, expr: Term): GadtExpr =
786+
withDefaultPos(tpd.GadtExpr(gadt, expr))
787+
def copy(original: Tree)(gadt: GadtConstraint, expr: Term): GadtExpr =
788+
tpd.cpy.GadtExpr(original)(gadt, expr)
789+
def unapply(x: GadtExpr): (GadtConstraint, Term) =
790+
(x.gadt, x.expr)
791+
given GadtExprMethods: GadtExprMethods with
792+
extension (self: GadtExpr)
793+
def gadt: GadtConstraint = self.gadt
794+
def expr: Term = self.expr
795+
779796
type Closure = tpd.Closure
780797

781798
object ClosureTypeTest extends TypeTest[Tree, Closure]:
@@ -2668,6 +2685,13 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
26682685
sym.isTerm && !sym.is(dotc.core.Flags.Method)
26692686
end SymbolMethods
26702687

2688+
type GadtConstraint = dotc.core.GadtConstraint
2689+
2690+
given GadtConstraintMethods: GadtConstraintMethods with
2691+
extension (self: GadtConstraint)
2692+
def symbols: List[Symbol] = self.symbols
2693+
def fullBounds(sym: Symbol): TypeBounds | Null = self.fullBounds(sym)
2694+
26712695
type Signature = dotc.core.Signature
26722696

26732697
object Signature extends SignatureModule:

compiler/src/scala/quoted/runtime/impl/printers/Extractors.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ object Extractors {
100100
this += "Assign(" += lhs += ", " += rhs += ")"
101101
case Block(stats, expr) =>
102102
this += "Block(" ++= stats += ", " += expr += ")"
103+
case GadtExpr(gadt, expr) =>
104+
this += "GadtExpr(" += gadt += ", " += expr += ")"
103105
case If(cond, thenp, elsep) =>
104106
this += "If(" += cond += ", " += thenp += ", " += elsep += ")"
105107
case Closure(meth, tpt) =>
@@ -316,6 +318,17 @@ object Extractors {
316318
def ++=(x: List[ParamClause]): self.type = { visitList(x, visitParamClause); buff }
317319
}
318320

321+
private implicit class GadtConstraintOps(buff: self.type) {
322+
def +=(x: GadtConstraint): self.type =
323+
visitList(for sym <- x.symbols yield (sym, x.fullBounds(sym).nn), (sym, bounds) => {
324+
buff += "("
325+
visitSymbol(sym)
326+
buff += ", "
327+
visitType(bounds)
328+
buff += ")"
329+
})
330+
}
331+
319332
private def visitOption[U](opt: Option[U], visit: U => this.type): this.type = opt match {
320333
case Some(x) =>
321334
this += "Some("

library/src/scala/quoted/Quotes.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,20 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
12971297
end extension
12981298
end BlockMethods
12991299

1300+
type GadtExpr <: Term
1301+
given GadtExprTypeTest: TypeTest[Tree, GadtExpr]
1302+
val GadtExpr: GadtExprModule
1303+
trait GadtExprModule:
1304+
this: GadtExpr.type =>
1305+
def apply(gadt: GadtConstraint, expr: Term): GadtExpr
1306+
def copy(original: Tree)(gadt: GadtConstraint, expr: Term): GadtExpr
1307+
def unapply(x: GadtExpr): (GadtConstraint, Term)
1308+
given GadtExprMethods: GadtExprMethods
1309+
trait GadtExprMethods:
1310+
extension (self: GadtExpr)
1311+
def gadt: GadtConstraint
1312+
def expr: Term
1313+
13001314
/** `TypeTest` that allows testing at runtime in a pattern match if a `Tree` is a `Closure` */
13011315
given ClosureTypeTest: TypeTest[Tree, Closure]
13021316

@@ -3985,6 +3999,17 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
39853999
end extension
39864000
}
39874001

4002+
/////////////////////
4003+
// GADT CONSTRAINT //
4004+
/////////////////////
4005+
4006+
type GadtConstraint <: AnyRef
4007+
given GadtConstraintMethods: GadtConstraintMethods
4008+
trait GadtConstraintMethods:
4009+
extension (self: GadtConstraint)
4010+
def symbols: List[Symbol]
4011+
def fullBounds(sym: Symbol): TypeBounds | Null
4012+
39884013
////////////////
39894014
// SIGNATURES //
39904015
////////////////
@@ -4581,6 +4606,8 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
45814606
foldTree(foldTree(x, lhs)(owner), rhs)(owner)
45824607
case Block(stats, expr) =>
45834608
foldTree(foldTrees(x, stats)(owner), expr)(owner)
4609+
case GadtExpr(gadt, expr) =>
4610+
foldTree(x, expr)(owner)
45844611
case If(cond, thenp, elsep) =>
45854612
foldTree(foldTree(foldTree(x, cond)(owner), thenp)(owner), elsep)(owner)
45864613
case While(cond, body) =>

project/MiMaFilters.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ object MiMaFilters {
2929
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#SymbolMethods.termRef"),
3030
ProblemFilters.exclude[ReversedMissingMethodProblem]("scala.quoted.Quotes#reflectModule#TypeTreeModule.ref"),
3131

32+
ProblemFilters.exclude[MissingClassProblem]("scala.quoted.Quotes$reflectModule$GadtExprMethods"),
33+
ProblemFilters.exclude[MissingClassProblem]("scala.quoted.Quotes$reflectModule$GadtExprModule"),
34+
ProblemFilters.exclude[MissingMethodProblem]("scala.quoted.Quotes#reflectModule.GadtExpr"),
35+
ProblemFilters.exclude[MissingMethodProblem]("scala.quoted.Quotes#reflectModule.GadtExprMethods"),
36+
ProblemFilters.exclude[MissingMethodProblem]("scala.quoted.Quotes#reflectModule.GadtExprTypeTest"),
37+
ProblemFilters.exclude[MissingClassProblem]("scala.quoted.Quotes$reflectModule$GadtConstraintMethods"),
38+
ProblemFilters.exclude[MissingMethodProblem]("scala.quoted.Quotes#reflectModule.GadtConstraintMethods"),
39+
3240
ProblemFilters.exclude[MissingClassProblem]("scala.annotation.since"),
3341
)
3442

0 commit comments

Comments
 (0)