Skip to content

Commit 3768984

Browse files
Merge pull request #7961 from fhackett/fhackett-work-on-6280
Add constructors for TypeOrBounds that are not refs or PolyType
2 parents cc22a36 + 5b75df9 commit 3768984

File tree

6 files changed

+123
-0
lines changed

6 files changed

+123
-0
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
11171117
case _ => None
11181118
}
11191119

1120+
def TypeBounds_apply(low: Type, hi: Type)(given ctx: Context): TypeBounds =
1121+
Types.TypeBounds(low, hi)
1122+
11201123
def TypeBounds_low(self: TypeBounds)(given Context): Type = self.lo
11211124
def TypeBounds_hi(self: TypeBounds)(given Context): Type = self.hi
11221125

@@ -1201,6 +1204,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
12011204
case tpe: Types.ConstantType => Some(tpe)
12021205
case _ => None
12031206
}
1207+
1208+
def ConstantType_apply(const: Constant)(given Context): ConstantType =
1209+
Types.ConstantType(const)
12041210

12051211
def ConstantType_constant(self: ConstantType)(given Context): Constant = self.value
12061212

@@ -1305,6 +1311,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
13051311
case _ => None
13061312
}
13071313

1314+
def AnnotatedType_apply(underlying: Type, annot: Term)(given Context): AnnotatedType =
1315+
Types.AnnotatedType(underlying, Annotations.Annotation(annot))
1316+
13081317
def AnnotatedType_underlying(self: AnnotatedType)(given Context): Type = self.underlying.stripTypeVar
13091318
def AnnotatedType_annot(self: AnnotatedType)(given Context): Term = self.annot.tree
13101319

@@ -1317,6 +1326,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
13171326
case _ => None
13181327
}
13191328

1329+
def AndType_apply(lhs: Type, rhs: Type)(given Context): AndType =
1330+
Types.AndType(lhs, rhs)
1331+
13201332
def AndType_left(self: AndType)(given Context): Type = self.tp1.stripTypeVar
13211333
def AndType_right(self: AndType)(given Context): Type = self.tp2.stripTypeVar
13221334

@@ -1329,6 +1341,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
13291341
case _ => None
13301342
}
13311343

1344+
def OrType_apply(lhs: Type, rhs: Type)(given Context): OrType =
1345+
Types.OrType(lhs, rhs)
1346+
13321347
def OrType_left(self: OrType)(given Context): Type = self.tp1.stripTypeVar
13331348
def OrType_right(self: OrType)(given Context): Type = self.tp2.stripTypeVar
13341349

@@ -1341,6 +1356,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
13411356
case _ => None
13421357
}
13431358

1359+
def MatchType_apply(bound: Type, scrutinee: Type, cases: List[Type])(given Context): MatchType =
1360+
Types.MatchType(bound, scrutinee, cases)
1361+
13441362
def MatchType_bound(self: MatchType)(given Context): Type = self.bound
13451363
def MatchType_scrutinee(self: MatchType)(given Context): Type = self.scrutinee
13461364
def MatchType_cases(self: MatchType)(given Context): List[Type] = self.cases
@@ -1445,8 +1463,13 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
14451463
case _ => None
14461464
}
14471465

1466+
def TypeLambda_apply(paramNames: List[String], boundsFn: TypeLambda => List[TypeBounds], bodyFn: TypeLambda => Type): TypeLambda =
1467+
Types.HKTypeLambda(paramNames.map(_.toTypeName))(boundsFn, bodyFn)
1468+
14481469
def TypeLambda_paramNames(self: TypeLambda)(given Context): List[String] = self.paramNames.map(_.toString)
14491470
def TypeLambda_paramBounds(self: TypeLambda)(given Context): List[TypeBounds] = self.paramInfos
1471+
def TypeLambda_param(self: TypeLambda, idx: Int)(given Context): Type =
1472+
self.newParamRef(idx)
14501473
def TypeLambda_resType(self: TypeLambda)(given Context): Type = self.resType
14511474

14521475
//

library/src/scala/tasty/reflect/CompilerInterface.scala

+15
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,8 @@ trait CompilerInterface {
797797

798798
def isInstanceOfTypeBounds(given ctx: Context): IsInstanceOf[TypeBounds]
799799

800+
def TypeBounds_apply(low: Type, hi: Type)(given ctx: Context): TypeBounds
801+
800802
def TypeBounds_low(self: TypeBounds)(given ctx: Context): Type
801803
def TypeBounds_hi(self: TypeBounds)(given ctx: Context): Type
802804

@@ -887,6 +889,8 @@ trait CompilerInterface {
887889

888890
def isInstanceOfConstantType(given ctx: Context): IsInstanceOf[ConstantType]
889891

892+
def ConstantType_apply(const : Constant)(given ctx : Context) : ConstantType
893+
890894
def ConstantType_constant(self: ConstantType)(given ctx: Context): Constant
891895

892896
/** Type of a reference to a term symbol */
@@ -941,6 +945,8 @@ trait CompilerInterface {
941945

942946
def isInstanceOfAnnotatedType(given ctx: Context): IsInstanceOf[AnnotatedType]
943947

948+
def AnnotatedType_apply(underlying: Type, annot: Term)(given ctx: Context): AnnotatedType
949+
944950
def AnnotatedType_underlying(self: AnnotatedType)(given ctx: Context): Type
945951
def AnnotatedType_annot(self: AnnotatedType)(given ctx: Context): Term
946952

@@ -949,6 +955,8 @@ trait CompilerInterface {
949955

950956
def isInstanceOfAndType(given ctx: Context): IsInstanceOf[AndType]
951957

958+
def AndType_apply(lhs: Type, rhs: Type)(given ctx: Context): AndType
959+
952960
def AndType_left(self: AndType)(given ctx: Context): Type
953961
def AndType_right(self: AndType)(given ctx: Context): Type
954962

@@ -957,6 +965,8 @@ trait CompilerInterface {
957965

958966
def isInstanceOfOrType(given ctx: Context): IsInstanceOf[OrType]
959967

968+
def OrType_apply(lhs : Type, rhs : Type)(given ctx : Context): OrType
969+
960970
def OrType_left(self: OrType)(given ctx: Context): Type
961971
def OrType_right(self: OrType)(given ctx: Context): Type
962972

@@ -965,6 +975,8 @@ trait CompilerInterface {
965975

966976
def isInstanceOfMatchType(given ctx: Context): IsInstanceOf[MatchType]
967977

978+
def MatchType_apply(bound: Type, scrutinee: Type, cases: List[Type])(given ctx: Context): MatchType
979+
968980
def MatchType_bound(self: MatchType)(given ctx: Context): Type
969981
def MatchType_scrutinee(self: MatchType)(given ctx: Context): Type
970982
def MatchType_cases(self: MatchType)(given ctx: Context): List[Type]
@@ -1037,8 +1049,11 @@ trait CompilerInterface {
10371049

10381050
def isInstanceOfTypeLambda(given ctx: Context): IsInstanceOf[TypeLambda]
10391051

1052+
def TypeLambda_apply(paramNames: List[String], boundsFn: TypeLambda => List[TypeBounds], bodyFn: TypeLambda => Type): TypeLambda
1053+
10401054
def TypeLambda_paramNames(self: TypeLambda)(given ctx: Context): List[String]
10411055
def TypeLambda_paramBounds(self: TypeLambda)(given ctx: Context): List[TypeBounds]
1056+
def TypeLambda_param(self: TypeLambda, idx: Int)(given ctx: Context): Type
10421057
def TypeLambda_resType(self: TypeLambda)(given ctx: Context): Type
10431058

10441059
//

library/src/scala/tasty/reflect/TypeOrBoundsOps.scala

+21
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ trait TypeOrBoundsOps extends Core {
103103
def unapply(x: ConstantType)(given ctx: Context): Option[ConstantType] = Some(x)
104104

105105
object ConstantType {
106+
def apply(x : Constant)(given ctx: Context): ConstantType = internal.ConstantType_apply(x)
106107
def unapply(x: ConstantType)(given ctx: Context): Option[Constant] = Some(x.constant)
107108
}
108109

@@ -205,6 +206,8 @@ trait TypeOrBoundsOps extends Core {
205206
def unapply(x: AnnotatedType)(given ctx: Context): Option[AnnotatedType] = Some(x)
206207

207208
object AnnotatedType {
209+
def apply(underlying: Type, annot: Term)(given ctx: Context): AnnotatedType =
210+
internal.AnnotatedType_apply(underlying, annot)
208211
def unapply(x: AnnotatedType)(given ctx: Context): Option[(Type, Term)] =
209212
Some((x.underlying, x.annot))
210213
}
@@ -221,6 +224,8 @@ trait TypeOrBoundsOps extends Core {
221224
def unapply(x: AndType)(given ctx: Context): Option[AndType] = Some(x)
222225

223226
object AndType {
227+
def apply(lhs: Type, rhs: Type)(given ctx: Context): AndType =
228+
internal.AndType_apply(lhs, rhs)
224229
def unapply(x: AndType)(given ctx: Context): Option[(Type, Type)] =
225230
Some((x.left, x.right))
226231
}
@@ -237,6 +242,7 @@ trait TypeOrBoundsOps extends Core {
237242
def unapply(x: OrType)(given ctx: Context): Option[OrType] = Some(x)
238243

239244
object OrType {
245+
def apply(lhs: Type, rhs: Type)(given ctx: Context): OrType = internal.OrType_apply(lhs, rhs)
240246
def unapply(x: OrType)(given ctx: Context): Option[(Type, Type)] =
241247
Some((x.left, x.right))
242248
}
@@ -253,6 +259,8 @@ trait TypeOrBoundsOps extends Core {
253259
def unapply(x: MatchType)(given ctx: Context): Option[MatchType] = Some(x)
254260

255261
object MatchType {
262+
def apply(bound: Type, scrutinee: Type, cases: List[Type])(given ctx: Context): MatchType =
263+
internal.MatchType_apply(bound, scrutinee, cases)
256264
def unapply(x: MatchType)(given ctx: Context): Option[(Type, Type, List[Type])] =
257265
Some((x.bound, x.scrutinee, x.cases))
258266
}
@@ -263,6 +271,14 @@ trait TypeOrBoundsOps extends Core {
263271
def cases(given ctx: Context): List[Type] = internal.MatchType_cases(self)
264272
}
265273

274+
/**
275+
* An accessor for `scala.internal.MatchCase[_,_]`, the representation of a `MatchType` case.
276+
*/
277+
def MatchCaseType(given Context): Type = {
278+
import scala.internal.MatchCase
279+
Type(classOf[MatchCase[_,_]])
280+
}
281+
266282
given (given Context): IsInstanceOf[ByNameType] = internal.isInstanceOfByNameType
267283

268284
object IsByNameType
@@ -381,13 +397,16 @@ trait TypeOrBoundsOps extends Core {
381397
def unapply(x: TypeLambda)(given ctx: Context): Option[TypeLambda] = Some(x)
382398

383399
object TypeLambda {
400+
def apply(paramNames: List[String], boundsFn: TypeLambda => List[TypeBounds], bodyFn: TypeLambda => Type): TypeLambda =
401+
internal.TypeLambda_apply(paramNames, boundsFn, bodyFn)
384402
def unapply(x: TypeLambda)(given ctx: Context): Option[(List[String], List[TypeBounds], Type)] =
385403
Some((x.paramNames, x.paramBounds, x.resType))
386404
}
387405

388406
given TypeLambdaOps: extension (self: TypeLambda) {
389407
def paramNames(given ctx: Context): List[String] = internal.TypeLambda_paramNames(self)
390408
def paramBounds(given ctx: Context): List[TypeBounds] = internal.TypeLambda_paramBounds(self)
409+
def param(idx: Int)(given ctx: Context) : Type = internal.TypeLambda_param(self, idx)
391410
def resType(given ctx: Context): Type = internal.TypeLambda_resType(self)
392411
}
393412

@@ -400,6 +419,8 @@ trait TypeOrBoundsOps extends Core {
400419
def unapply(x: TypeBounds)(given ctx: Context): Option[TypeBounds] = Some(x)
401420

402421
object TypeBounds {
422+
def apply(low: Type, hi: Type)(given ctx: Context): TypeBounds =
423+
internal.TypeBounds_apply(low, hi)
403424
def unapply(x: TypeBounds)(given ctx: Context): Option[(Type, Type)] = Some((x.low, x.hi))
404425
}
405426

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ok
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import tasty._
2+
import quoted._
3+
4+
object Macros {
5+
inline def theTestBlock : Unit = ${ theTestBlockImpl }
6+
7+
trait RefineMe {
8+
type T
9+
def foo : T
10+
}
11+
12+
class TestAnnotation extends scala.annotation.Annotation
13+
14+
def theTestBlockImpl(given qctx : QuoteContext) : Expr[Unit] = {
15+
import qctx.tasty.{_,given}
16+
17+
val x1T = ConstantType(Constant(1))
18+
val x2T = OrType(ConstantType(Constant(1)), ConstantType(Constant(2)))
19+
val x3T = AndType(ConstantType(Constant(3)), typeOf[Any])
20+
val x4T =
21+
TypeLambda(
22+
List("A","B"),
23+
_ => List(TypeBounds(typeOf[Nothing], typeOf[Any]), TypeBounds(typeOf[Nothing], typeOf[Any])),
24+
(tl : TypeLambda) => tl.param(1))
25+
val x5T =
26+
Refinement(
27+
typeOf[RefineMe],
28+
"T",
29+
TypeBounds(typeOf[Int], typeOf[Int]))
30+
val x6T = AppliedType(Type(classOf[List[_]]), List(typeOf[Int]))
31+
val x7T = AnnotatedType(ConstantType(Constant(7)), '{ new TestAnnotation }.unseal)
32+
val x8T =
33+
MatchType(
34+
typeOf[Int],
35+
typeOf[List[8]],
36+
List(
37+
TypeLambda(
38+
List("t"),
39+
_ => List(TypeBounds(typeOf[Nothing], typeOf[Any])),
40+
tl => AppliedType(MatchCaseType, List(AppliedType(Type(classOf[List[_]]), List(tl.param(0))), tl.param(0)))))
41+
)
42+
43+
assert(x1T =:= '[1].unseal.tpe)
44+
assert(x2T =:= '[1|2].unseal.tpe)
45+
assert(x3T =:= '[3&Any].unseal.tpe)
46+
assert(x4T =:= '[[A,B] =>> B].unseal.tpe)
47+
assert(x5T =:= '[RefineMe { type T = Int }].unseal.tpe)
48+
assert(x6T =:= '[List[Int]].unseal.tpe)
49+
assert(x7T =:= '[7 @TestAnnotation].unseal.tpe)
50+
assert(x8T =:= '[List[8] match { case List[t] => t }].unseal.tpe)
51+
52+
'{
53+
println("Ok")
54+
}
55+
}
56+
}
57+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
object Test {
3+
def main(args : Array[String]) : Unit =
4+
Macros.theTestBlock
5+
}
6+

0 commit comments

Comments
 (0)