-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2814 from xeno-by/topic/auto-duplicate-expansions
[nomaster] macro expansions are now auto-duplicated
- Loading branch information
Showing
11 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
42 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import scala.reflect.macros.Context | ||
import language.experimental.macros | ||
|
||
object Macros { | ||
def impl(c: Context) = { | ||
import c.universe._ | ||
val x = Ident(newTermName("x")) | ||
def defAndUseX(rhs: Tree) = { | ||
Block(List(ValDef(NoMods, newTermName("x"), TypeTree(), rhs)), x) | ||
} | ||
val xi4 = defAndUseX(Literal(Constant(4))) | ||
val xs2 = defAndUseX(Literal(Constant("2"))) | ||
c.Expr[String](Apply(Select(xi4, newTermName("$plus")), List(xs2))) | ||
} | ||
|
||
def foo = macro impl | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
object Test extends App { | ||
println(Macros.foo) | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-language:experimental.macros |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import scala.reflect.macros.Context | ||
|
||
object Macros { | ||
def impl(c: Context) = { | ||
import c.universe._ | ||
val Expr(Block((cdef: ClassDef) :: Nil, _)) = reify { class C { def x = 2 } } | ||
val cdef1 = | ||
new Transformer { | ||
override def transform(tree: Tree): Tree = tree match { | ||
case Template(_, _, ctor :: defs) => | ||
val defs1 = defs collect { | ||
case ddef @ DefDef(mods, name, tparams, vparamss, tpt, body) => | ||
val future = Select(Select(Select(Ident(newTermName("scala")), newTermName("concurrent")), newTermName("package")), newTermName("future")) | ||
val Future = Select(Select(Ident(newTermName("scala")), newTermName("concurrent")), newTypeName("Future")) | ||
val tpt1 = if (tpt.isEmpty) tpt else AppliedTypeTree(Future, List(tpt)) | ||
val body1 = Apply(future, List(body)) | ||
val name1 = newTermName("async" + name.toString.capitalize) | ||
DefDef(mods, name1, tparams, vparamss, tpt1, body1) | ||
} | ||
Template(Nil, emptyValDef, ctor +: defs ::: defs1) | ||
case _ => | ||
super.transform(tree) | ||
} | ||
} transform cdef | ||
c.Expr[Unit](Block(cdef1 :: Nil, Literal(Constant(())))) | ||
} | ||
|
||
def foo = macro impl | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import scala.concurrent._ | ||
import ExecutionContext.Implicits.global | ||
|
||
object Test extends App { | ||
Macros.foo | ||
} |