Skip to content

Commit e0fcb25

Browse files
committed
Fix #9518: Instantiate HKTypeLambda and replace AppliedType with appliedTo
1 parent 0d22c74 commit e0fcb25

File tree

7 files changed

+44
-13
lines changed

7 files changed

+44
-13
lines changed

compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,11 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte
12241224
def Type_select(self: Type)(sym: Symbol)(using Context): Type =
12251225
self.select(sym)
12261226

1227+
def Type_appliedTo(self: Type)(targs: List[Type]): Type =
1228+
self match
1229+
case self: Types.HKTypeLambda => self.instantiate(targs)
1230+
case _ => Types.AppliedType(self, targs)
1231+
12271232
type ConstantType = Types.ConstantType
12281233

12291234
def ConstantType_TypeTest(using Context): TypeTest[TypeOrBounds, ConstantType] = new {
@@ -1333,9 +1338,7 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte
13331338
}
13341339

13351340
def AppliedType_tycon(self: AppliedType)(using Context): Type = self.tycon
1336-
def AppliedType_args(self: AppliedType)(using Context): List[TypeOrBounds] = self.args
1337-
1338-
def AppliedType_apply(tycon: Type, args: List[TypeOrBounds])(using Context): AppliedType = Types.AppliedType(tycon, args)
1341+
def AppliedType_args(self: AppliedType)(using Context): List[Type] = self.args
13391342

13401343
type AnnotatedType = Types.AnnotatedType
13411344

library/src/scala/internal/tasty/CompilerInterface.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,9 @@ trait CompilerInterface extends scala.tasty.reflect.Types {
604604
/** The type <this . sym>, reduced if possible */
605605
def Type_select(self: Type)(sym: Symbol)(using ctx: Context): Type
606606

607+
/** The current type applied to given type arguments: `this[targ0, ..., targN]` */
608+
def Type_appliedTo(self: Type)(targs: List[Type]): Type
609+
607610
def ConstantType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, ConstantType]
608611

609612
def ConstantType_apply(const : Constant)(using ctx : Context) : ConstantType
@@ -642,9 +645,7 @@ trait CompilerInterface extends scala.tasty.reflect.Types {
642645
def AppliedType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, AppliedType]
643646

644647
def AppliedType_tycon(self: AppliedType)(using ctx: Context): Type
645-
def AppliedType_args(self: AppliedType)(using ctx: Context): List[TypeOrBounds]
646-
647-
def AppliedType_apply(tycon: Type, args: List[TypeOrBounds])(using ctx: Context) : AppliedType
648+
def AppliedType_args(self: AppliedType)(using ctx: Context): List[Type]
648649

649650
def AnnotatedType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, AnnotatedType]
650651

library/src/scala/tasty/Reflection.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,6 +1462,13 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
14621462

14631463
/** The type <this . sym>, reduced if possible */
14641464
def select(sym: Symbol)(using ctx: Context): Type = reflectSelf.Type_select(self)(sym)
1465+
1466+
/** The current type applied to given type arguments: `this[targ]` */
1467+
def appliedTo(targ: Type): Type = reflectSelf.Type_appliedTo(self)(List(targ))
1468+
1469+
/** The current type applied to given type arguments: `this[targ0, ..., targN]` */
1470+
def appliedTo(targs: List[Type]): Type = reflectSelf.Type_appliedTo(self)(targs)
1471+
14651472
end extension
14661473
end Type
14671474

@@ -1549,14 +1556,12 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface =>
15491556
given AppliedTypeOps as AppliedType.type = AppliedType
15501557

15511558
object AppliedType:
1552-
def apply(tycon: Type, args: List[TypeOrBounds])(using ctx: Context): AppliedType =
1553-
reflectSelf.AppliedType_apply(tycon, args)
1554-
def unapply(x: AppliedType)(using ctx: Context): Option[(Type, List[TypeOrBounds /* Type | TypeBounds */])] =
1559+
def unapply(x: AppliedType)(using ctx: Context): Option[(Type, List[Type])] =
15551560
Some((x.tycon, x.args))
15561561

15571562
extension (self: AppliedType):
15581563
def tycon(using ctx: Context): Type = reflectSelf.AppliedType_tycon(self)
1559-
def args(using ctx: Context): List[TypeOrBounds /* Type | TypeBounds */] = reflectSelf.AppliedType_args(self)
1564+
def args(using ctx: Context): List[Type] = reflectSelf.AppliedType_args(self)
15601565
end extension
15611566
end AppliedType
15621567

tests/pos-macros/i9251/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ object Async {
2828
case AppliedType(tp,tparams1) =>
2929
val fType = summon[quoted.Type[F]]
3030
val ptp = tparams1.tail.head
31-
val ptpTree = Inferred(AppliedType(fType.unseal.tpe,List(ptp)))
31+
val ptpTree = Inferred(fType.unseal.tpe.appliedTo(ptp))
3232
'{ println(${Expr(ptpTree.show)}) }
3333

3434
}

tests/pos-macros/i9518/Macro_1.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
import scala.quoted._
3+
4+
trait CB[T]
5+
6+
inline def shift : Unit = ${ shiftTerm }
7+
8+
def shiftTerm(using QuoteContext): Expr[Unit] = {
9+
import qctx.tasty._
10+
val nTree = '{ ??? : CB[Int] }.unseal
11+
val tp1 = '[CB[Int]].unseal.tpe
12+
val tp2 = '[([X] =>> CB[X])[Int]].unseal.tpe
13+
val ta = '[[X] =>> CB[X]]
14+
val tp3 = '[ta.T[Int]].unseal.tpe
15+
val tp4 = '[CB].unseal.tpe.appliedTo(typeOf[Int])
16+
assert(nTree.tpe <:< tp1)
17+
assert(nTree.tpe <:< tp2)
18+
assert(nTree.tpe <:< tp3)
19+
assert(nTree.tpe <:< tp4)
20+
'{}
21+
}

tests/pos-macros/i9518/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def test: Unit = shift

tests/run-macros/tasty-construct-types/Macro_1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object Macros {
2727
typeOf[RefineMe],
2828
"T",
2929
TypeBounds(typeOf[Int], typeOf[Int]))
30-
val x6T = AppliedType(Type(classOf[List[_]]), List(typeOf[Int]))
30+
val x6T = Type(classOf[List[_]]).appliedTo(List(typeOf[Int]))
3131
val x7T = AnnotatedType(ConstantType(Constant(7)), '{ new TestAnnotation }.unseal)
3232
val x8T =
3333
MatchType(
@@ -37,7 +37,7 @@ object Macros {
3737
TypeLambda(
3838
List("t"),
3939
_ => List(TypeBounds(typeOf[Nothing], typeOf[Any])),
40-
tl => AppliedType(MatchCaseType, List(AppliedType(Type(classOf[List[_]]), List(tl.param(0))), tl.param(0)))))
40+
tl => MatchCaseType.appliedTo(List(Type(classOf[List[_]]).appliedTo(tl.param(0)), tl.param(0)))))
4141
)
4242

4343
assert(x1T =:= '[1].unseal.tpe)

0 commit comments

Comments
 (0)