From 00d7cd2d5300524aeb885d8d51b2123aa0b44f6e Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Mon, 10 Sep 2012 20:00:22 +0200 Subject: improvements for reification of free symbols 1) Free symbols no longer carry signatures in their constructors. Previously to reify a free symbol (i.e. to generate a ValDef that creates it) one had to reify sym.info first. However reification of sym.info might lead to unexpected side effects, including stack overflows, if reification of sym.info recursively required reifying sym itself. Now it's not a problem. First we reify a "header" of a free symbol by emitting something like: val free$Foo1 = build.newFreeTerm("Foo", Foo.this, NoFlags)` Afterwards, when doing code generation for the reification symbol table, we populate free symbols by inserting calls to `build.setTypeSignature($sym.info)` This techniques transforms recursion into memoized iteration, because even if reifying sym.info indirectly requires reification of sym itself, we remember that we already reified sym and simply return things like Ident(free$Foo1). 2) Unfortunately I haven't been able to get rid of recursion completely. Since some symbols (e.g. local classes) aren't pickled, we need to recreate them during reification (this is necessary e.g. to reify RefinedTypes). Reifier uses a special function, named `reifySymDef`, for that purpose. Here's an example of how it works: val symdef$_1 = build.newNestedSymbol(free$U1, newTypeName("_"), NoPosition, DEFERRED | PARAM, false); `reifySymDef` expands into a call to `newNestedSymbol`, which requires an owner This essentially turns `reifySymDef` into a recursion of `reifySymDef` calls, so that the entire owner chain get reified. This is an implementation strategy that was employed in the first revision of the reifier written by Martin, and personally I have no clue whether it's really necessary to reify the parents. I leave this as a future work. 3) When working with free symbols, it's necessary to attach free symbols to their reification. This is required in obscure nested reification scenarios, when a symbol that was free for an inner reifee is no longer free for an outer reifee. In that case we need to remove that free symbol from the symbol table of the inner reification. Back then we didn't have tree attachments, so I had to introduce a phantom "value" parameter for `newFreeType` to keep track of the original symbols for free types. Now when we have attachments, this is no longer necessary and allowed me to clean up the code. --- test/files/run/existentials3-new.check | 16 ++++++++-------- test/files/run/existentials3-new.scala | 16 ++++++++-------- .../macro-typecheck-macrosdisabled/Impls_Macros_1.scala | 3 ++- .../macro-typecheck-macrosdisabled2/Impls_Macros_1.scala | 3 ++- test/files/run/t1195-new.scala | 2 +- test/files/run/toolbox_typecheck_macrosdisabled.scala | 3 ++- test/files/run/toolbox_typecheck_macrosdisabled2.scala | 3 ++- 7 files changed, 25 insertions(+), 21 deletions(-) (limited to 'test/files/run') diff --git a/test/files/run/existentials3-new.check b/test/files/run/existentials3-new.check index a036d92a24..2550f4dae0 100644 --- a/test/files/run/existentials3-new.check +++ b/test/files/run/existentials3-new.check @@ -1,24 +1,24 @@ -Bar.type, t=TypeRef, s= <: scala.runtime.AbstractFunction0[Bar] with Serializable{case def unapply(x$0: Bar): Boolean} with Singleton -Bar, t=TypeRef, s= <: Test.ToS with Product with Serializable{def copy(): Bar} +Bar.type, t=TypeRef, s=type Bar.type +Bar, t=TypeRef, s=type Bar Test.ToS, t=RefinedType, s=f3 Test.ToS, t=RefinedType, s=f4 Test.ToS, t=RefinedType, s=f5 () => Test.ToS, t=TypeRef, s=class Function0 () => Test.ToS, t=TypeRef, s=class Function0 -$anon, t=TypeRef, s= <: B with Test.ToS -$anon, t=TypeRef, s= <: B with A with Test.ToS +$anon, t=TypeRef, s=type $anon +$anon, t=TypeRef, s=type $anon List[java.lang.Object{type T1}#T1], t=TypeRef, s=class List List[Seq[Int]], t=TypeRef, s=class List List[Seq[U forSome { type U <: Int }]], t=TypeRef, s=class List -Bar.type, t=TypeRef, s= <: scala.runtime.AbstractFunction0[Bar] with Serializable{case def unapply(x$0: Bar): Boolean} with Singleton -Bar, t=TypeRef, s= <: Test.ToS with Product with Serializable{def copy(): Bar} +Bar.type, t=TypeRef, s=type Bar.type +Bar, t=TypeRef, s=type Bar Test.ToS, t=RefinedType, s=g3 Test.ToS, t=RefinedType, s=g4 Test.ToS, t=RefinedType, s=g5 () => Test.ToS, t=TypeRef, s=class Function0 () => Test.ToS, t=TypeRef, s=class Function0 -$anon, t=TypeRef, s= <: B with Test.ToS -$anon, t=TypeRef, s= <: B with A with Test.ToS +$anon, t=TypeRef, s=type $anon +$anon, t=TypeRef, s=type $anon List[java.lang.Object{type T1}#T1], t=TypeRef, s=class List List[Seq[Int]], t=TypeRef, s=class List List[Seq[U forSome { type U <: Int }]], t=TypeRef, s=class List diff --git a/test/files/run/existentials3-new.scala b/test/files/run/existentials3-new.scala index 16735eab4f..31ddc8cc9e 100644 --- a/test/files/run/existentials3-new.scala +++ b/test/files/run/existentials3-new.scala @@ -42,27 +42,27 @@ object Test { // tags do work for f10/g10 def main(args: Array[String]): Unit = { - m(f1) - m(f2) + m2(f1) + m2(f2) m(f3) m(f4) m(f5) m(f6) m(f7) - m(f8) - m(f9) + m2(f8) + m2(f9) m2(f10) m(f11) m(f12) - m(g1) - m(g2) + m2(g1) + m2(g2) m(g3) m(g4) m(g5) m(g6) m(g7) - m(g8) - m(g9) + m2(g8) + m2(g9) m2(g10) m(g11) m(g12) diff --git a/test/files/run/macro-typecheck-macrosdisabled/Impls_Macros_1.scala b/test/files/run/macro-typecheck-macrosdisabled/Impls_Macros_1.scala index b2f6f7d50e..f693ad78cc 100644 --- a/test/files/run/macro-typecheck-macrosdisabled/Impls_Macros_1.scala +++ b/test/files/run/macro-typecheck-macrosdisabled/Impls_Macros_1.scala @@ -18,7 +18,8 @@ object Macros { val rupkg = c.mirror.staticModule("scala.reflect.runtime.package") val rusym = build.selectTerm(rupkg, "universe") val NullaryMethodType(rutpe) = rusym.typeSignature - val ru = build.newFreeTerm("ru", rutpe, scala.reflect.runtime.universe) + val ru = build.newFreeTerm("ru", scala.reflect.runtime.universe) + build.setTypeSignature(ru, rutpe) val tree2 = Apply(Select(Ident(ru), newTermName("reify")), List(Literal(Constant(2)))) val ttree2 = c.typeCheck(tree2, withMacrosDisabled = true) diff --git a/test/files/run/macro-typecheck-macrosdisabled2/Impls_Macros_1.scala b/test/files/run/macro-typecheck-macrosdisabled2/Impls_Macros_1.scala index 948c047351..1dbf5a1a87 100644 --- a/test/files/run/macro-typecheck-macrosdisabled2/Impls_Macros_1.scala +++ b/test/files/run/macro-typecheck-macrosdisabled2/Impls_Macros_1.scala @@ -18,7 +18,8 @@ object Macros { val rupkg = c.mirror.staticModule("scala.reflect.runtime.package") val rusym = build.selectTerm(rupkg, "universe") val NullaryMethodType(rutpe) = rusym.typeSignature - val ru = build.newFreeTerm("ru", rutpe, scala.reflect.runtime.universe) + val ru = build.newFreeTerm("ru", scala.reflect.runtime.universe) + build.setTypeSignature(ru, rutpe) val tree2 = Apply(Select(Ident(ru), newTermName("reify")), List(Apply(Select(Ident(newTermName("scala")), newTermName("Array")), List(Literal(Constant(2)))))) val ttree2 = c.typeCheck(tree2, withMacrosDisabled = true) diff --git a/test/files/run/t1195-new.scala b/test/files/run/t1195-new.scala index 4edfb5073f..690c6213cf 100644 --- a/test/files/run/t1195-new.scala +++ b/test/files/run/t1195-new.scala @@ -9,7 +9,7 @@ object Test { val g1 = g() val h1 = h() - def m[T: TypeTag](x: T) = println(typeOf[T] + ", underlying = " + typeOf[T].typeSymbol.typeSignature) + def m[T: AbsTypeTag](x: T) = println(absTypeOf[T] + ", underlying = " + absTypeOf[T].typeSymbol.typeSignature) def main(args: Array[String]): Unit = { m(f) diff --git a/test/files/run/toolbox_typecheck_macrosdisabled.scala b/test/files/run/toolbox_typecheck_macrosdisabled.scala index 01a418d42e..51eb63f294 100644 --- a/test/files/run/toolbox_typecheck_macrosdisabled.scala +++ b/test/files/run/toolbox_typecheck_macrosdisabled.scala @@ -12,7 +12,8 @@ object Test extends App { val rupkg = cm.staticModule("scala.reflect.runtime.package") val rusym = build.selectTerm(rupkg, "universe") val NullaryMethodType(rutpe) = rusym.typeSignature - val ru = build.newFreeTerm("ru", rutpe, scala.reflect.runtime.universe) + val ru = build.newFreeTerm("ru", scala.reflect.runtime.universe) + build.setTypeSignature(ru, rutpe) val tree1 = Apply(Select(Ident(ru), newTermName("reify")), List(Literal(Constant(2)))) val ttree1 = toolbox.typeCheck(tree1, withMacrosDisabled = false) diff --git a/test/files/run/toolbox_typecheck_macrosdisabled2.scala b/test/files/run/toolbox_typecheck_macrosdisabled2.scala index 462dc9fa91..74fd09d9fd 100644 --- a/test/files/run/toolbox_typecheck_macrosdisabled2.scala +++ b/test/files/run/toolbox_typecheck_macrosdisabled2.scala @@ -12,7 +12,8 @@ object Test extends App { val rupkg = cm.staticModule("scala.reflect.runtime.package") val rusym = build.selectTerm(rupkg, "universe") val NullaryMethodType(rutpe) = rusym.typeSignature - val ru = build.newFreeTerm("ru", rutpe, scala.reflect.runtime.universe) + val ru = build.newFreeTerm("ru", scala.reflect.runtime.universe) + build.setTypeSignature(ru, rutpe) val tree1 = Apply(Select(Ident(ru), newTermName("reify")), List(Apply(Select(Ident(newTermName("scala")), newTermName("Array")), List(Literal(Constant(2)))))) val ttree1 = toolbox.typeCheck(tree1, withMacrosDisabled = false) -- cgit v1.2.3