From 098e8a084adbebca1f2e57e1aacbf6d9a3a87e7d Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Thu, 13 Dec 2012 02:20:47 +0100 Subject: typedIdent no longer destroys attachments When transforming Idents to qualified Selects, typedIdent used to forget about carrying original attachments to the resulting tree. Not anymore. --- src/compiler/scala/tools/nsc/typechecker/Typers.scala | 2 +- src/reflect/scala/reflect/internal/StdAttachments.scala | 1 + test/files/pos/attachments-typed-ident.check | 0 test/files/pos/attachments-typed-ident.flags | 1 + test/files/pos/attachments-typed-ident/Impls_1.scala | 17 +++++++++++++++++ .../pos/attachments-typed-ident/Macros_Test_2.scala | 4 ++++ 6 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/files/pos/attachments-typed-ident.check create mode 100644 test/files/pos/attachments-typed-ident.flags create mode 100644 test/files/pos/attachments-typed-ident/Impls_1.scala create mode 100644 test/files/pos/attachments-typed-ident/Macros_Test_2.scala diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 76fd07f63a..8441d450a6 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -5159,7 +5159,7 @@ trait Typers extends Modes with Adaptations with Tags { val tree1 = ( if (qual == EmptyTree) tree // atPos necessary because qualifier might come from startContext - else atPos(tree.pos)(Select(qual, name)) + else atPos(tree.pos)(Select(qual, name) setAttachments tree.attachments) ) val (tree2, pre2) = makeAccessible(tree1, defSym, pre, qual) // assert(pre.typeArgs isEmpty) // no need to add #2416-style check here, right? diff --git a/src/reflect/scala/reflect/internal/StdAttachments.scala b/src/reflect/scala/reflect/internal/StdAttachments.scala index 1df91a67b0..b782353ed3 100644 --- a/src/reflect/scala/reflect/internal/StdAttachments.scala +++ b/src/reflect/scala/reflect/internal/StdAttachments.scala @@ -10,6 +10,7 @@ trait StdAttachments { trait Attachable { protected var rawatt: scala.reflect.macros.Attachments { type Pos = Position } = NoPosition def attachments = rawatt + def setAttachments(attachments: scala.reflect.macros.Attachments { type Pos = Position }): this.type = { rawatt = attachments; this } def updateAttachment[T: ClassTag](attachment: T): this.type = { rawatt = rawatt.update(attachment); this } def removeAttachment[T: ClassTag]: this.type = { rawatt = rawatt.remove[T]; this } diff --git a/test/files/pos/attachments-typed-ident.check b/test/files/pos/attachments-typed-ident.check new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/files/pos/attachments-typed-ident.flags b/test/files/pos/attachments-typed-ident.flags new file mode 100644 index 0000000000..cd66464f2f --- /dev/null +++ b/test/files/pos/attachments-typed-ident.flags @@ -0,0 +1 @@ +-language:experimental.macros \ No newline at end of file diff --git a/test/files/pos/attachments-typed-ident/Impls_1.scala b/test/files/pos/attachments-typed-ident/Impls_1.scala new file mode 100644 index 0000000000..cc40893a93 --- /dev/null +++ b/test/files/pos/attachments-typed-ident/Impls_1.scala @@ -0,0 +1,17 @@ +import scala.reflect.macros.Context +import language.experimental.macros + +object MyAttachment + +object Macros { + def impl(c: Context) = { + import c.universe._ + val ident = Ident(newTermName("bar")) updateAttachment MyAttachment + assert(ident.attachments.get[MyAttachment.type].isDefined, ident.attachments) + val typed = c.typeCheck(ident) + assert(typed.attachments.get[MyAttachment.type].isDefined, typed.attachments) + c.Expr[Int](typed) + } + + def foo = macro impl +} \ No newline at end of file diff --git a/test/files/pos/attachments-typed-ident/Macros_Test_2.scala b/test/files/pos/attachments-typed-ident/Macros_Test_2.scala new file mode 100644 index 0000000000..37065ead4b --- /dev/null +++ b/test/files/pos/attachments-typed-ident/Macros_Test_2.scala @@ -0,0 +1,4 @@ +object Test extends App { + def bar = 2 + Macros.foo +} \ No newline at end of file -- cgit v1.2.3