summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Macros.scala26
-rw-r--r--test/files/neg/macro-blackbox-structural.check4
-rw-r--r--test/files/neg/macro-blackbox-structural/Impls_Macros_1.scala15
-rw-r--r--test/files/neg/macro-blackbox-structural/Test_2.scala5
-rw-r--r--test/files/neg/t5903a/Macros_1.scala4
-rw-r--r--test/files/neg/t5903e/Macros_1.scala4
-rw-r--r--test/files/run/macro-bodyexpandstoimpl/Impls_1.scala4
-rw-r--r--test/files/run/macro-expand-unapply-a/Impls_Macros_1.scala4
-rw-r--r--test/files/run/macro-invalidusage-badret.check2
-rw-r--r--test/files/run/macro-system-properties.check6
-rw-r--r--test/files/run/macro-whitebox-structural.check1
-rw-r--r--test/files/run/macro-whitebox-structural/Impls_Macros_1.scala16
-rw-r--r--test/files/run/macro-whitebox-structural/Test_2.scala5
-rw-r--r--test/files/run/t5903a/Macros_1.scala4
-rw-r--r--test/files/run/t5903b/Macros_1.scala4
-rw-r--r--test/files/run/t5903c/Macros_1.scala4
-rw-r--r--test/files/run/t5903d/Macros_1.scala4
-rw-r--r--test/files/run/t5923a/Macros_1.scala4
-rw-r--r--test/files/run/t5923c/Macros_1.scala4
19 files changed, 89 insertions, 31 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Macros.scala b/src/compiler/scala/tools/nsc/typechecker/Macros.scala
index f1b9bd4833..57358f72f5 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Macros.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Macros.scala
@@ -604,21 +604,27 @@ trait Macros extends FastTrack with MacroRuntimes with Traces with Helpers {
*/
def macroExpandApply(typer: Typer, expandee: Tree, mode: Mode, pt: Type): Tree = {
object expander extends TermMacroExpander(APPLY_ROLE, typer, expandee, mode, pt) {
- override def onSuccess(expanded: Tree) = {
+ override def onSuccess(expanded0: Tree) = {
+ def approximate(tp: Type) = {
+ // approximation is necessary for whitebox macros to guide type inference
+ // read more in the comments for onDelayed below
+ if (isBlackbox(expandee)) tp
+ else {
+ val undetparams = tp collect { case tp if tp.typeSymbol.isTypeParameter => tp.typeSymbol }
+ deriveTypeWithWildcards(undetparams)(tp)
+ }
+ }
+ val macroPt = approximate(if (isNullaryInvocation(expandee)) expandee.tpe.finalResultType else expandee.tpe)
+ val expanded = if (isBlackbox(expandee)) atPos(enclosingMacroPosition.focus)(Typed(expanded0, TypeTree(macroPt))) else expanded0
+
// prematurely annotate the tree with a macro expansion attachment
// so that adapt called indirectly by typer.typed knows that it needs to apply the existential fixup
linkExpandeeAndExpanded(expandee, expanded)
- // approximation is necessary for whitebox macros to guide type inference
- // read more in the comments for onDelayed below
- def approximate(tp: Type) = {
- val undetparams = tp collect { case tp if tp.typeSymbol.isTypeParameter => tp.typeSymbol }
- deriveTypeWithWildcards(undetparams)(tp)
- }
- val macroPtApprox = approximate(if (isNullaryInvocation(expandee)) expandee.tpe.finalResultType else expandee.tpe)
+
// `macroExpandApply` is called from `adapt`, where implicit conversions are disabled
// therefore we need to re-enable the conversions back temporarily
- if (macroDebugVerbose) println(s"typecheck #1 (against macroPtApprox = $macroPtApprox): $expanded")
- val expanded1 = typer.context.withImplicitsEnabled(typer.typed(expanded, mode, macroPtApprox))
+ if (macroDebugVerbose) println(s"typecheck #1 (against macroPt = $macroPt): $expanded")
+ val expanded1 = typer.context.withImplicitsEnabled(typer.typed(expanded, mode, macroPt))
if (expanded1.isErrorTyped) {
if (macroDebugVerbose) println(s"typecheck #1 has failed: ${typer.context.reportBuffer.errors}")
expanded1
diff --git a/test/files/neg/macro-blackbox-structural.check b/test/files/neg/macro-blackbox-structural.check
new file mode 100644
index 0000000000..86a218559c
--- /dev/null
+++ b/test/files/neg/macro-blackbox-structural.check
@@ -0,0 +1,4 @@
+Test_2.scala:4: error: value x is not a member of Any
+ println(Macros.foo.x)
+ ^
+one error found
diff --git a/test/files/neg/macro-blackbox-structural/Impls_Macros_1.scala b/test/files/neg/macro-blackbox-structural/Impls_Macros_1.scala
new file mode 100644
index 0000000000..08f1c21e89
--- /dev/null
+++ b/test/files/neg/macro-blackbox-structural/Impls_Macros_1.scala
@@ -0,0 +1,15 @@
+import scala.language.experimental.macros
+
+object Macros {
+ def impl(c: scala.reflect.macros.BlackboxContext) = {
+ import c.universe._
+ q"""
+ trait Foo {
+ def x = 2
+ }
+ new Foo {}
+ """
+ }
+
+ def foo = macro impl
+} \ No newline at end of file
diff --git a/test/files/neg/macro-blackbox-structural/Test_2.scala b/test/files/neg/macro-blackbox-structural/Test_2.scala
new file mode 100644
index 0000000000..ea6a817e34
--- /dev/null
+++ b/test/files/neg/macro-blackbox-structural/Test_2.scala
@@ -0,0 +1,5 @@
+import Macros._
+
+object Test extends App {
+ println(Macros.foo.x)
+} \ No newline at end of file
diff --git a/test/files/neg/t5903a/Macros_1.scala b/test/files/neg/t5903a/Macros_1.scala
index 9fc3bfe261..7888b888e1 100644
--- a/test/files/neg/t5903a/Macros_1.scala
+++ b/test/files/neg/t5903a/Macros_1.scala
@@ -1,4 +1,4 @@
-import scala.reflect.macros.BlackboxContext
+import scala.reflect.macros.WhiteboxContext
import language.experimental.macros
trait Tree
@@ -13,7 +13,7 @@ object NewQuasiquotes {
}
object QuasiquoteMacros {
- def unapplyImpl(c: BlackboxContext)(t: c.Tree) = {
+ def unapplyImpl(c: WhiteboxContext)(t: c.Tree) = {
import c.universe._
q"""
new {
diff --git a/test/files/neg/t5903e/Macros_1.scala b/test/files/neg/t5903e/Macros_1.scala
index 13e168d529..997e6fd073 100644
--- a/test/files/neg/t5903e/Macros_1.scala
+++ b/test/files/neg/t5903e/Macros_1.scala
@@ -1,4 +1,4 @@
-import scala.reflect.macros.BlackboxContext
+import scala.reflect.macros.WhiteboxContext
import language.experimental.macros
object Interpolation {
@@ -10,7 +10,7 @@ object Interpolation {
}
object Macros {
- def unapplyImpl(c: BlackboxContext)(x: c.Tree) = {
+ def unapplyImpl(c: WhiteboxContext)(x: c.Tree) = {
import c.universe._
q"""
new {
diff --git a/test/files/run/macro-bodyexpandstoimpl/Impls_1.scala b/test/files/run/macro-bodyexpandstoimpl/Impls_1.scala
index 9af3b18ccb..0a9f9a0ced 100644
--- a/test/files/run/macro-bodyexpandstoimpl/Impls_1.scala
+++ b/test/files/run/macro-bodyexpandstoimpl/Impls_1.scala
@@ -1,11 +1,11 @@
import scala.language.experimental.macros
-import scala.reflect.macros.BlackboxContext
+import scala.reflect.macros.{BlackboxContext, WhiteboxContext}
object Impls {
def foo(c: BlackboxContext)(x: c.Expr[Int]) = x
def refToFoo(dummy: Int) = macro refToFoo_impl
- def refToFoo_impl(c: BlackboxContext)(dummy: c.Expr[Int]) = {
+ def refToFoo_impl(c: WhiteboxContext)(dummy: c.Expr[Int]) = {
import c.universe._
val body = Select(Ident(TermName("Impls")), TermName("foo"))
val global = c.universe.asInstanceOf[scala.tools.nsc.Global]
diff --git a/test/files/run/macro-expand-unapply-a/Impls_Macros_1.scala b/test/files/run/macro-expand-unapply-a/Impls_Macros_1.scala
index b837ffc43b..7bfff374e2 100644
--- a/test/files/run/macro-expand-unapply-a/Impls_Macros_1.scala
+++ b/test/files/run/macro-expand-unapply-a/Impls_Macros_1.scala
@@ -1,11 +1,11 @@
-import scala.reflect.macros.BlackboxContext
+import scala.reflect.macros.WhiteboxContext
object Helper {
def unapplySeq[T](x: List[T]): Option[Seq[T]] = List.unapplySeq[T](x)
}
object Macros {
- def impl[T: c.WeakTypeTag](c: BlackboxContext)(x: c.Expr[List[T]]) = {
+ def impl[T: c.WeakTypeTag](c: WhiteboxContext)(x: c.Expr[List[T]]) = {
c.universe.reify(Helper.unapplySeq(x.splice))
}
diff --git a/test/files/run/macro-invalidusage-badret.check b/test/files/run/macro-invalidusage-badret.check
index 9225b716d6..e79550043f 100644
--- a/test/files/run/macro-invalidusage-badret.check
+++ b/test/files/run/macro-invalidusage-badret.check
@@ -1,5 +1,5 @@
reflective compilation has failed:
type mismatch;
- found : Int(42)
+ found : Int
required: String
diff --git a/test/files/run/macro-system-properties.check b/test/files/run/macro-system-properties.check
index ea4c5a664a..b102d319ec 100644
--- a/test/files/run/macro-system-properties.check
+++ b/test/files/run/macro-system-properties.check
@@ -14,9 +14,15 @@ scala> object GrabContext {
defined object GrabContext
scala> object Test { class C(implicit a: Any) { GrabContext.grab } }
+<console>:12: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses
+ object Test { class C(implicit a: Any) { GrabContext.grab } }
+ ^
defined object Test
scala> object Test { class C(implicit a: Any) { GrabContext.grab } }
+<console>:12: warning: a pure expression does nothing in statement position; you may be omitting necessary parentheses
+ object Test { class C(implicit a: Any) { GrabContext.grab } }
+ ^
defined object Test
scala>
diff --git a/test/files/run/macro-whitebox-structural.check b/test/files/run/macro-whitebox-structural.check
new file mode 100644
index 0000000000..0cfbf08886
--- /dev/null
+++ b/test/files/run/macro-whitebox-structural.check
@@ -0,0 +1 @@
+2
diff --git a/test/files/run/macro-whitebox-structural/Impls_Macros_1.scala b/test/files/run/macro-whitebox-structural/Impls_Macros_1.scala
new file mode 100644
index 0000000000..1b975ca850
--- /dev/null
+++ b/test/files/run/macro-whitebox-structural/Impls_Macros_1.scala
@@ -0,0 +1,16 @@
+import scala.reflect.macros.WhiteboxContext
+import scala.language.experimental.macros
+
+object Macros {
+ def impl(c: WhiteboxContext) = {
+ import c.universe._
+ q"""
+ trait Foo {
+ def x = 2
+ }
+ new Foo {}
+ """
+ }
+
+ def foo = macro impl
+} \ No newline at end of file
diff --git a/test/files/run/macro-whitebox-structural/Test_2.scala b/test/files/run/macro-whitebox-structural/Test_2.scala
new file mode 100644
index 0000000000..ea6a817e34
--- /dev/null
+++ b/test/files/run/macro-whitebox-structural/Test_2.scala
@@ -0,0 +1,5 @@
+import Macros._
+
+object Test extends App {
+ println(Macros.foo.x)
+} \ No newline at end of file
diff --git a/test/files/run/t5903a/Macros_1.scala b/test/files/run/t5903a/Macros_1.scala
index 9fc3bfe261..7888b888e1 100644
--- a/test/files/run/t5903a/Macros_1.scala
+++ b/test/files/run/t5903a/Macros_1.scala
@@ -1,4 +1,4 @@
-import scala.reflect.macros.BlackboxContext
+import scala.reflect.macros.WhiteboxContext
import language.experimental.macros
trait Tree
@@ -13,7 +13,7 @@ object NewQuasiquotes {
}
object QuasiquoteMacros {
- def unapplyImpl(c: BlackboxContext)(t: c.Tree) = {
+ def unapplyImpl(c: WhiteboxContext)(t: c.Tree) = {
import c.universe._
q"""
new {
diff --git a/test/files/run/t5903b/Macros_1.scala b/test/files/run/t5903b/Macros_1.scala
index ae491e2207..8c03e5579d 100644
--- a/test/files/run/t5903b/Macros_1.scala
+++ b/test/files/run/t5903b/Macros_1.scala
@@ -1,4 +1,4 @@
-import scala.reflect.macros.BlackboxContext
+import scala.reflect.macros.WhiteboxContext
import language.experimental.macros
object Interpolation {
@@ -10,7 +10,7 @@ object Interpolation {
}
object Macros {
- def unapplyImpl[T: c.WeakTypeTag](c: BlackboxContext)(x: c.Tree) = {
+ def unapplyImpl[T: c.WeakTypeTag](c: WhiteboxContext)(x: c.Tree) = {
import c.universe._
q"""
new {
diff --git a/test/files/run/t5903c/Macros_1.scala b/test/files/run/t5903c/Macros_1.scala
index da7dd5281c..c9dfe9d60c 100644
--- a/test/files/run/t5903c/Macros_1.scala
+++ b/test/files/run/t5903c/Macros_1.scala
@@ -1,4 +1,4 @@
-import scala.reflect.macros.BlackboxContext
+import scala.reflect.macros.WhiteboxContext
import language.experimental.macros
object Interpolation {
@@ -10,7 +10,7 @@ object Interpolation {
}
object Macros {
- def unapplyImpl[T: c.WeakTypeTag](c: BlackboxContext)(x: c.Tree) = {
+ def unapplyImpl[T: c.WeakTypeTag](c: WhiteboxContext)(x: c.Tree) = {
import c.universe._
q"""
new {
diff --git a/test/files/run/t5903d/Macros_1.scala b/test/files/run/t5903d/Macros_1.scala
index e3a9c3b39e..8a57e27602 100644
--- a/test/files/run/t5903d/Macros_1.scala
+++ b/test/files/run/t5903d/Macros_1.scala
@@ -1,4 +1,4 @@
-import scala.reflect.macros.BlackboxContext
+import scala.reflect.macros.WhiteboxContext
import language.experimental.macros
object Interpolation {
@@ -10,7 +10,7 @@ object Interpolation {
}
object Macros {
- def unapplyImpl(c: BlackboxContext)(x: c.Tree) = {
+ def unapplyImpl(c: WhiteboxContext)(x: c.Tree) = {
import c.universe._
q"""
new {
diff --git a/test/files/run/t5923a/Macros_1.scala b/test/files/run/t5923a/Macros_1.scala
index f86e6b1add..445392ff95 100644
--- a/test/files/run/t5923a/Macros_1.scala
+++ b/test/files/run/t5923a/Macros_1.scala
@@ -1,4 +1,4 @@
-import scala.reflect.macros.BlackboxContext
+import scala.reflect.macros.WhiteboxContext
import language.experimental.macros
case class C[T](t: String)
@@ -7,7 +7,7 @@ object C {
}
object Macros {
- def impl[T](c: BlackboxContext)(ttag: c.WeakTypeTag[T]) = {
+ def impl[T](c: WhiteboxContext)(ttag: c.WeakTypeTag[T]) = {
import c.universe._
val ttag0 = ttag;
{
diff --git a/test/files/run/t5923c/Macros_1.scala b/test/files/run/t5923c/Macros_1.scala
index c980f243d4..c86e14966b 100644
--- a/test/files/run/t5923c/Macros_1.scala
+++ b/test/files/run/t5923c/Macros_1.scala
@@ -1,5 +1,5 @@
import language.experimental.macros
-import scala.reflect.macros.BlackboxContext
+import scala.reflect.macros.WhiteboxContext
trait Iso[T, U] {
def to(t : T) : U
@@ -8,7 +8,7 @@ trait Iso[T, U] {
object Iso {
implicit def materializeIso[T, U]: Iso[T, U] = macro impl[T, U]
- def impl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: BlackboxContext): c.Expr[Iso[T, U]] = {
+ def impl[T: c.WeakTypeTag, U: c.WeakTypeTag](c: WhiteboxContext): c.Expr[Iso[T, U]] = {
import c.universe._
import definitions._
import Flag._