From 31b52ed651e58a68d478aabe41c62287b6d4d718 Mon Sep 17 00:00:00 2001 From: Eugene Burmako Date: Fri, 21 Feb 2014 21:48:16 +0100 Subject: bundles now reject invalid context types Vanilla macros only allow blackbox.Context, whitebox.Context and PrefixType refinements thereof. Bundles should behave in the same way. --- src/reflect/scala/reflect/internal/Definitions.scala | 14 +++++--------- test/files/neg/macro-bundle-wrongcontext-a.check | 4 ++++ test/files/neg/macro-bundle-wrongcontext-a.scala | 13 +++++++++++++ test/files/neg/macro-bundle-wrongcontext-b.check | 4 ++++ test/files/neg/macro-bundle-wrongcontext-b.scala | 11 +++++++++++ 5 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 test/files/neg/macro-bundle-wrongcontext-a.check create mode 100644 test/files/neg/macro-bundle-wrongcontext-a.scala create mode 100644 test/files/neg/macro-bundle-wrongcontext-b.check create mode 100644 test/files/neg/macro-bundle-wrongcontext-b.scala diff --git a/src/reflect/scala/reflect/internal/Definitions.scala b/src/reflect/scala/reflect/internal/Definitions.scala index 20f6d51fcf..f8f7673530 100644 --- a/src/reflect/scala/reflect/internal/Definitions.scala +++ b/src/reflect/scala/reflect/internal/Definitions.scala @@ -609,12 +609,8 @@ trait Definitions extends api.StandardDefinitions { private def macroBundleParamInfo(tp: Type) = { val ctor = tp.erasure.typeSymbol.primaryConstructor ctor.paramss match { - case List(List(c)) => - val sym = c.info.typeSymbol - val isContextCompatible = sym.isNonBottomSubClass(BlackboxContextClass) || sym.isNonBottomSubClass(WhiteboxContextClass) - if (isContextCompatible) c.info else NoType - case _ => - NoType + case List(List(c)) => if (isMacroContextType(c.info)) c.info else NoType + case _ => NoType } } @@ -630,9 +626,9 @@ trait Definitions extends api.StandardDefinitions { def isBlackboxMacroBundleType(tp: Type) = { val isBundle = isMacroBundleType(tp) - val isBlackbox = (macroBundleParamInfo(tp) <:< BlackboxContextClass.tpe) - val notWhitebox = !(macroBundleParamInfo(tp) <:< WhiteboxContextClass.tpe) - isBundle && isBlackbox && notWhitebox + val unwrappedContext = MacroContextType.unapply(macroBundleParamInfo(tp)).getOrElse(NoType) + val isBlackbox = unwrappedContext =:= BlackboxContextClass.tpe + isBundle && isBlackbox } def isListType(tp: Type) = tp <:< classExistentialType(ListClass) diff --git a/test/files/neg/macro-bundle-wrongcontext-a.check b/test/files/neg/macro-bundle-wrongcontext-a.check new file mode 100644 index 0000000000..7a48dbfd3a --- /dev/null +++ b/test/files/neg/macro-bundle-wrongcontext-a.check @@ -0,0 +1,4 @@ +macro-bundle-wrongcontext-a.scala:12: error: not found: value Bundle + def foo: Any = macro Bundle.impl + ^ +one error found diff --git a/test/files/neg/macro-bundle-wrongcontext-a.scala b/test/files/neg/macro-bundle-wrongcontext-a.scala new file mode 100644 index 0000000000..ed566fd977 --- /dev/null +++ b/test/files/neg/macro-bundle-wrongcontext-a.scala @@ -0,0 +1,13 @@ +import scala.reflect.macros.whitebox._ +import scala.language.experimental.macros + +abstract class MyContext extends Context + +class Bundle(val c: MyContext) { + import c.universe._ + def impl = q"()" +} + +object Macros { + def foo: Any = macro Bundle.impl +} \ No newline at end of file diff --git a/test/files/neg/macro-bundle-wrongcontext-b.check b/test/files/neg/macro-bundle-wrongcontext-b.check new file mode 100644 index 0000000000..9c94c3bc34 --- /dev/null +++ b/test/files/neg/macro-bundle-wrongcontext-b.check @@ -0,0 +1,4 @@ +macro-bundle-wrongcontext-b.scala:10: error: not found: value Bundle + def foo: Any = macro Bundle.impl + ^ +one error found diff --git a/test/files/neg/macro-bundle-wrongcontext-b.scala b/test/files/neg/macro-bundle-wrongcontext-b.scala new file mode 100644 index 0000000000..0b4ff7e17c --- /dev/null +++ b/test/files/neg/macro-bundle-wrongcontext-b.scala @@ -0,0 +1,11 @@ +import scala.reflect.macros.whitebox._ +import scala.language.experimental.macros + +class Bundle(val c: Context { type Foo <: Int }) { + import c.universe._ + def impl = q"()" +} + +object Macros { + def foo: Any = macro Bundle.impl +} \ No newline at end of file -- cgit v1.2.3