Skip to content

Commit

Permalink
blackbox restriction #4: can't customize pattern matching
Browse files Browse the repository at this point in the history
When an application of a blackbox macro is used as an extractor in a
pattern match, it triggers an unconditional compiler error,
preventing customizations of pattern matching implemented with macros.
  • Loading branch information
xeno-by authored and adriaanm committed Nov 13, 2013
1 parent 0d5c2f7 commit f832965
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/compiler/scala/tools/nsc/typechecker/PatternTypers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,10 @@ trait PatternTypers {

if (fun1.tpe.isErroneous)
duplErrTree
else if (unapplyMethod.isMacro && !fun1.isInstanceOf[Apply])
duplErrorTree(WrongShapeExtractorExpansion(tree))
else
else if (unapplyMethod.isMacro && !fun1.isInstanceOf[Apply]) {
if (isBlackbox(unapplyMethod)) duplErrorTree(BlackboxExtractorExpansion(tree))
else duplErrorTree(WrongShapeExtractorExpansion(tree))
} else
makeTypedUnApply()
}

Expand Down
4 changes: 4 additions & 0 deletions test/files/neg/macro-blackbox-extractor.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Test_2.scala:3: error: extractor macros can only be whitebox
case Extractor(x) => println(x)
^
one error found
21 changes: 21 additions & 0 deletions test/files/neg/macro-blackbox-extractor/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import scala.reflect.macros.BlackboxContext
import language.experimental.macros

object Extractor {
def unapply(x: Int) = macro Macros.unapplyImpl
}

object Macros {
def unapplyImpl(c: BlackboxContext)(x: c.Tree) = {
import c.universe._
q"""
new {
class Match(x: Int) {
def isEmpty = false
def get = x
}
def unapply(x: Int) = new Match(x)
}.unapply($x)
"""
}
}
5 changes: 5 additions & 0 deletions test/files/neg/macro-blackbox-extractor/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test extends App {
42 match {
case Extractor(x) => println(x)
}
}
2 changes: 1 addition & 1 deletion test/files/neg/t5903d.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Test_2.scala:4: error: extractor macros can only expand into extractor calls
Test_2.scala:4: error: extractor macros can only be whitebox
case t"$x" => println(x)
^
one error found
1 change: 1 addition & 0 deletions test/files/run/macro-whitebox-extractor.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
21 changes: 21 additions & 0 deletions test/files/run/macro-whitebox-extractor/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import scala.reflect.macros.WhiteboxContext
import language.experimental.macros

object Extractor {
def unapply(x: Int) = macro Macros.unapplyImpl
}

object Macros {
def unapplyImpl(c: WhiteboxContext)(x: c.Tree) = {
import c.universe._
q"""
new {
class Match(x: Int) {
def isEmpty = false
def get = x
}
def unapply(x: Int) = new Match(x)
}.unapply($x)
"""
}
}
5 changes: 5 additions & 0 deletions test/files/run/macro-whitebox-extractor/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test extends App {
42 match {
case Extractor(x) => println(x)
}
}

0 comments on commit f832965

Please sign in to comment.