From ad309faafa1ea095ca241293807018d18a70d495 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 1 Sep 2020 17:28:07 +0200 Subject: [PATCH] Fix #9518: Instantiate HKTypeLambda and replace AppliedType with appliedTo --- .../community-projects/dotty-cps-async | 2 +- community-build/community-projects/shapeless | 2 +- .../reflect/ReflectionCompilerInterface.scala | 5 +++-- .../internal/tasty/CompilerInterface.scala | 5 +++-- library/src/scala/tasty/Reflection.scala | 13 ++++++++---- tests/pos-macros/i9251/Macro_1.scala | 2 +- tests/pos-macros/i9518/Macro_1.scala | 21 +++++++++++++++++++ tests/pos-macros/i9518/Test_2.scala | 1 + .../tasty-construct-types/Macro_1.scala | 4 ++-- 9 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 tests/pos-macros/i9518/Macro_1.scala create mode 100644 tests/pos-macros/i9518/Test_2.scala diff --git a/community-build/community-projects/dotty-cps-async b/community-build/community-projects/dotty-cps-async index 5e36dc4a93ed..cf5fd400643f 160000 --- a/community-build/community-projects/dotty-cps-async +++ b/community-build/community-projects/dotty-cps-async @@ -1 +1 @@ -Subproject commit 5e36dc4a93eded3898548d2cd449309e70051b21 +Subproject commit cf5fd400643f6b61b2c81c49e080e59034a37fd9 diff --git a/community-build/community-projects/shapeless b/community-build/community-projects/shapeless index 9bc5c422d219..fcd7ad513a60 160000 --- a/community-build/community-projects/shapeless +++ b/community-build/community-projects/shapeless @@ -1 +1 @@ -Subproject commit 9bc5c422d219cfce3040eb252e5a418b347da2bd +Subproject commit fcd7ad513a603520bfaf1390a751bbc5bb7c6484 diff --git a/compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala b/compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala index dd3d6fefe48f..ebb34f2fb892 100644 --- a/compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala +++ b/compiler/src/dotty/tools/dotc/quoted/reflect/ReflectionCompilerInterface.scala @@ -1224,6 +1224,9 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte def Type_select(self: Type)(sym: Symbol)(using Context): Type = self.select(sym) + def Type_appliedTo(self: Type)(targs: List[TypeOrBounds]): Type = + self.appliedTo(targs) + type ConstantType = Types.ConstantType def ConstantType_TypeTest(using Context): TypeTest[TypeOrBounds, ConstantType] = new { @@ -1335,8 +1338,6 @@ class ReflectionCompilerInterface(val rootContext: Context) extends CompilerInte def AppliedType_tycon(self: AppliedType)(using Context): Type = self.tycon def AppliedType_args(self: AppliedType)(using Context): List[TypeOrBounds] = self.args - def AppliedType_apply(tycon: Type, args: List[TypeOrBounds])(using Context): AppliedType = Types.AppliedType(tycon, args) - type AnnotatedType = Types.AnnotatedType def AnnotatedType_TypeTest(using Context): TypeTest[TypeOrBounds, AnnotatedType] = new { diff --git a/library/src/scala/internal/tasty/CompilerInterface.scala b/library/src/scala/internal/tasty/CompilerInterface.scala index 7420c18cdea4..b3146276b0b2 100644 --- a/library/src/scala/internal/tasty/CompilerInterface.scala +++ b/library/src/scala/internal/tasty/CompilerInterface.scala @@ -604,6 +604,9 @@ trait CompilerInterface extends scala.tasty.reflect.Types { /** The type , reduced if possible */ def Type_select(self: Type)(sym: Symbol)(using ctx: Context): Type + /** The current type applied to given type arguments: `this[targ0, ..., targN]` */ + def Type_appliedTo(self: Type)(targs: List[TypeOrBounds]): Type + def ConstantType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, ConstantType] def ConstantType_apply(const : Constant)(using ctx : Context) : ConstantType @@ -644,8 +647,6 @@ trait CompilerInterface extends scala.tasty.reflect.Types { def AppliedType_tycon(self: AppliedType)(using ctx: Context): Type def AppliedType_args(self: AppliedType)(using ctx: Context): List[TypeOrBounds] - def AppliedType_apply(tycon: Type, args: List[TypeOrBounds])(using ctx: Context) : AppliedType - def AnnotatedType_TypeTest(using ctx: Context): TypeTest[TypeOrBounds, AnnotatedType] def AnnotatedType_apply(underlying: Type, annot: Term)(using ctx: Context): AnnotatedType diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index 77760cc36dcb..ba6daf3fa3ca 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -1462,6 +1462,13 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => /** The type , reduced if possible */ def select(sym: Symbol)(using ctx: Context): Type = reflectSelf.Type_select(self)(sym) + + /** The current type applied to given type arguments: `this[targ]` */ + def appliedTo(targ: TypeOrBounds): Type = reflectSelf.Type_appliedTo(self)(List(targ)) + + /** The current type applied to given type arguments: `this[targ0, ..., targN]` */ + def appliedTo(targs: List[TypeOrBounds]): Type = reflectSelf.Type_appliedTo(self)(targs) + end extension end Type @@ -1549,14 +1556,12 @@ trait Reflection extends reflect.Types { reflectSelf: CompilerInterface => given AppliedTypeOps as AppliedType.type = AppliedType object AppliedType: - def apply(tycon: Type, args: List[TypeOrBounds])(using ctx: Context): AppliedType = - reflectSelf.AppliedType_apply(tycon, args) - def unapply(x: AppliedType)(using ctx: Context): Option[(Type, List[TypeOrBounds /* Type | TypeBounds */])] = + def unapply(x: AppliedType)(using ctx: Context): Option[(Type, List[TypeOrBounds])] = Some((x.tycon, x.args)) extension (self: AppliedType): def tycon(using ctx: Context): Type = reflectSelf.AppliedType_tycon(self) - def args(using ctx: Context): List[TypeOrBounds /* Type | TypeBounds */] = reflectSelf.AppliedType_args(self) + def args(using ctx: Context): List[TypeOrBounds] = reflectSelf.AppliedType_args(self) end extension end AppliedType diff --git a/tests/pos-macros/i9251/Macro_1.scala b/tests/pos-macros/i9251/Macro_1.scala index 2e9ccf5685cf..b4548cee5ef9 100644 --- a/tests/pos-macros/i9251/Macro_1.scala +++ b/tests/pos-macros/i9251/Macro_1.scala @@ -28,7 +28,7 @@ object Async { case AppliedType(tp,tparams1) => val fType = summon[quoted.Type[F]] val ptp = tparams1.tail.head - val ptpTree = Inferred(AppliedType(fType.unseal.tpe,List(ptp))) + val ptpTree = Inferred(fType.unseal.tpe.appliedTo(ptp)) '{ println(${Expr(ptpTree.show)}) } } diff --git a/tests/pos-macros/i9518/Macro_1.scala b/tests/pos-macros/i9518/Macro_1.scala new file mode 100644 index 000000000000..0867f149f559 --- /dev/null +++ b/tests/pos-macros/i9518/Macro_1.scala @@ -0,0 +1,21 @@ + +import scala.quoted._ + +trait CB[T] + +inline def shift : Unit = ${ shiftTerm } + +def shiftTerm(using QuoteContext): Expr[Unit] = { + import qctx.tasty._ + val nTree = '{ ??? : CB[Int] }.unseal + val tp1 = '[CB[Int]].unseal.tpe + val tp2 = '[([X] =>> CB[X])[Int]].unseal.tpe + val ta = '[[X] =>> CB[X]] + val tp3 = '[ta.T[Int]].unseal.tpe + val tp4 = '[CB].unseal.tpe.appliedTo(typeOf[Int]) + assert(nTree.tpe <:< tp1) + assert(nTree.tpe <:< tp2) + assert(nTree.tpe <:< tp3) + assert(nTree.tpe <:< tp4) + '{} +} diff --git a/tests/pos-macros/i9518/Test_2.scala b/tests/pos-macros/i9518/Test_2.scala new file mode 100644 index 000000000000..4f1fe3071c3c --- /dev/null +++ b/tests/pos-macros/i9518/Test_2.scala @@ -0,0 +1 @@ +def test: Unit = shift diff --git a/tests/run-macros/tasty-construct-types/Macro_1.scala b/tests/run-macros/tasty-construct-types/Macro_1.scala index 9367922e1728..7b780b4038a6 100644 --- a/tests/run-macros/tasty-construct-types/Macro_1.scala +++ b/tests/run-macros/tasty-construct-types/Macro_1.scala @@ -27,7 +27,7 @@ object Macros { typeOf[RefineMe], "T", TypeBounds(typeOf[Int], typeOf[Int])) - val x6T = AppliedType(Type(classOf[List[_]]), List(typeOf[Int])) + val x6T = Type(classOf[List[_]]).appliedTo(List(typeOf[Int])) val x7T = AnnotatedType(ConstantType(Constant(7)), '{ new TestAnnotation }.unseal) val x8T = MatchType( @@ -37,7 +37,7 @@ object Macros { TypeLambda( List("t"), _ => List(TypeBounds(typeOf[Nothing], typeOf[Any])), - tl => AppliedType(MatchCaseType, List(AppliedType(Type(classOf[List[_]]), List(tl.param(0))), tl.param(0))))) + tl => MatchCaseType.appliedTo(List(Type(classOf[List[_]]).appliedTo(tl.param(0)), tl.param(0))))) ) assert(x1T =:= '[1].unseal.tpe)