Skip to content

Commit daa77d1

Browse files
committed
Merge pull request scala#3602 from xeno-by/topic/backport-vampire-warning
[backport] no longer warns on calls to vampire macros
2 parents 43e06be + db300d4 commit daa77d1

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4934,7 +4934,7 @@ trait Typers extends Modes with Adaptations with Tags {
49344934

49354935
if (tree.isInstanceOf[PostfixSelect])
49364936
checkFeature(tree.pos, PostfixOpsFeature, name.decode)
4937-
if (tree1.symbol != null && tree1.symbol.isOnlyRefinementMember)
4937+
if (tree1.symbol != null && tree1.symbol.isOnlyRefinementMember && !tree1.symbol.isMacro)
49384938
checkFeature(tree1.pos, ReflectiveCallsFeature, tree1.symbol.toString)
49394939

49404940
if (qual1.hasSymbolWhich(_.isRootPackage)) treeCopy.Ident(tree1, name)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2
2+
3
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-Xfatal-warnings
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// As per http://meta.plasm.us/posts/2013/08/31/feeding-our-vampires/
2+
3+
import scala.annotation.StaticAnnotation
4+
import scala.reflect.macros.Context
5+
import scala.language.experimental.macros
6+
7+
class body(tree: Any) extends StaticAnnotation
8+
9+
object Macros {
10+
def selFieldImpl(c: Context) = {
11+
import c.universe._
12+
val field = c.macroApplication.symbol
13+
val bodyAnn = field.annotations.filter(_.tpe <:< typeOf[body]).head
14+
c.Expr[Any](bodyAnn.scalaArgs.head)
15+
}
16+
17+
def mkObjectImpl(c: Context)(xs: c.Expr[Any]*) = {
18+
import c.universe._
19+
import Flag._
20+
// val kvps = xs.toList map { case q"${_}(${Literal(Constant(name: String))}).->[${_}]($value)" => name -> value }
21+
val kvps = xs.map(_.tree).toList map { case Apply(TypeApply(Select(Apply(_, List(Literal(Constant(name: String)))), _), _), List(value)) => name -> value }
22+
// val fields = kvps map { case (k, v) => q"@body($v) def ${TermName(k)} = macro Macros.selFieldImpl" }
23+
val fields = kvps map { case (k, v) => DefDef(
24+
Modifiers(MACRO, tpnme.EMPTY, List(Apply(Select(New(Ident(newTypeName("body"))), nme.CONSTRUCTOR), List(v)))),
25+
newTermName(k), Nil, Nil, TypeTree(), Select(Ident(newTermName("Macros")), newTermName("selFieldImpl"))) }
26+
// q"import scala.language.experimental.macros; class Workaround { ..$fields }; new Workaround{}"
27+
c.Expr[Any](Block(
28+
List(
29+
Import(Select(Select(Ident(newTermName("scala")), newTermName("language")), newTermName("experimental")), List(ImportSelector(newTermName("macros"), 51, newTermName("macros"), 51))),
30+
ClassDef(
31+
NoMods, newTypeName("Workaround"), Nil,
32+
Template(
33+
List(Select(Ident(newTermName("scala")), newTypeName("AnyRef"))), emptyValDef,
34+
DefDef(
35+
NoMods, nme.CONSTRUCTOR, Nil, List(Nil), TypeTree(),
36+
Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(()))))
37+
+: fields)),
38+
ClassDef(
39+
Modifiers(FINAL), newTypeName("$anon"), Nil,
40+
Template(
41+
List(Ident(newTypeName("Workaround"))), emptyValDef,
42+
List(
43+
DefDef(
44+
NoMods, nme.CONSTRUCTOR, Nil, List(Nil), TypeTree(),
45+
Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))))))),
46+
Apply(Select(New(Ident(newTypeName("$anon"))), nme.CONSTRUCTOR), List())))
47+
}
48+
}
49+
50+
object mkObject {
51+
def apply(xs: Any*) = macro Macros.mkObjectImpl
52+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Test extends App {
2+
val foo = mkObject("x" -> "2", "y" -> 3)
3+
println(foo.x)
4+
println(foo.y)
5+
// println(foo.z) => will result in a compilation error
6+
}

0 commit comments

Comments
 (0)