From 70afd0544e86456515caee62a3eaac41882d892f Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Sat, 28 Jun 2014 19:12:48 +0200 Subject: relaxes attachment-matching rules It came as a surprise recently, but attachments.contains/get/update/remove require the class of the payload to match the provided tag exactly, not taking subclassing into account. This commit fixes the oversight. --- src/reflect/scala/reflect/macros/Attachments.scala | 2 +- test/files/pos/macro-attachments/Macros_1.scala | 19 +++++++++++++++++++ test/files/pos/macro-attachments/Test_2.scala | 3 +++ test/files/run/reflection-attachments.check | 0 4 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/files/pos/macro-attachments/Macros_1.scala create mode 100644 test/files/pos/macro-attachments/Test_2.scala create mode 100644 test/files/run/reflection-attachments.check diff --git a/src/reflect/scala/reflect/macros/Attachments.scala b/src/reflect/scala/reflect/macros/Attachments.scala index 5ccdc15a03..b5c340645a 100644 --- a/src/reflect/scala/reflect/macros/Attachments.scala +++ b/src/reflect/scala/reflect/macros/Attachments.scala @@ -35,7 +35,7 @@ abstract class Attachments { self => def all: Set[Any] = Set.empty private def matchesTag[T: ClassTag](datum: Any) = - classTag[T].runtimeClass == datum.getClass + classTag[T].runtimeClass.isInstance(datum) /** An underlying payload of the given class type `T`. */ def get[T: ClassTag]: Option[T] = diff --git a/test/files/pos/macro-attachments/Macros_1.scala b/test/files/pos/macro-attachments/Macros_1.scala new file mode 100644 index 0000000000..38d05d5b85 --- /dev/null +++ b/test/files/pos/macro-attachments/Macros_1.scala @@ -0,0 +1,19 @@ +import scala.language.experimental.macros +import scala.reflect.macros.whitebox.Context + +trait Base +class Att extends Base + +object Macros { + def impl(c: Context) = { + import c.universe._ + import c.internal._ + import decorators._ + val dummy = q"x" + dummy.updateAttachment(new Att) + if (dummy.attachments.get[Base].isEmpty) c.abort(c.enclosingPosition, "that's not good") + q"()" + } + + def foo: Any = macro impl +} \ No newline at end of file diff --git a/test/files/pos/macro-attachments/Test_2.scala b/test/files/pos/macro-attachments/Test_2.scala new file mode 100644 index 0000000000..acfddae942 --- /dev/null +++ b/test/files/pos/macro-attachments/Test_2.scala @@ -0,0 +1,3 @@ +object Test extends App { + Macros.foo +} \ No newline at end of file diff --git a/test/files/run/reflection-attachments.check b/test/files/run/reflection-attachments.check new file mode 100644 index 0000000000..e69de29bb2 -- cgit v1.2.3