diff options
author | Eugene Burmako <xeno.by@gmail.com> | 2013-10-02 16:47:11 +0200 |
---|---|---|
committer | Eugene Burmako <xeno.by@gmail.com> | 2013-10-02 22:46:01 +0200 |
commit | fe074bbca1a9a3ae7d46301d652c5bf0f34e2c6e (patch) | |
tree | 402d565771638ee12625b1a0fdb7d441e434bc5d | |
parent | 5db26e5136c635af062801218fc7edec280e63ff (diff) | |
download | scala-fe074bbca1a9a3ae7d46301d652c5bf0f34e2c6e.tar.gz scala-fe074bbca1a9a3ae7d46301d652c5bf0f34e2c6e.tar.bz2 scala-fe074bbca1a9a3ae7d46301d652c5bf0f34e2c6e.zip |
macro bundles are now usable in repl
One of the previous commits relaxed the top-level restriction for bundles,
turning it into a requirement of staticness (i.e. bundles nested in static
objects are also okay now).
This means that we can now define bundles in repl. Almost.
There's still a little problem remaining that arises from the fact that
when compiling a line of input, repl doesn't automatically import all
previously defined symbols, but rather uses an heuristic to scan the
input and guess what symbols need to be imported.
Unfortunately for bundles, this heuristic fails, because when scanning
a macro definition that looks like `def foo = macro Macros.foo`, it thinks
that it's only necessary to import a term symbol called Macros (a vanilla
way of defining macro impls), but not a type symbol called Macros (a new
way of writing macro impls in bundles).
This commit fixes the problem by making the repl look for both term and
type symbols corresponding to the identifiers used in macro definitions.
-rw-r--r-- | src/repl/scala/tools/nsc/interpreter/MemberHandlers.scala | 10 | ||||
-rw-r--r-- | test/files/run/macro-bundle-repl.check | 24 | ||||
-rw-r--r-- | test/files/run/macro-bundle-repl.scala | 13 |
3 files changed, 43 insertions, 4 deletions
diff --git a/src/repl/scala/tools/nsc/interpreter/MemberHandlers.scala b/src/repl/scala/tools/nsc/interpreter/MemberHandlers.scala index 28b95aa442..f4cbcb50fe 100644 --- a/src/repl/scala/tools/nsc/interpreter/MemberHandlers.scala +++ b/src/repl/scala/tools/nsc/interpreter/MemberHandlers.scala @@ -87,10 +87,11 @@ trait MemberHandlers { def definesTerm = Option.empty[TermName] def definesType = Option.empty[TypeName] - lazy val referencedNames = ImportVarsTraverser(member) - def importedNames = List[Name]() - def definedNames = definesTerm.toList ++ definesType.toList - def definedSymbols = List[Symbol]() + private lazy val _referencedNames = ImportVarsTraverser(member) + def referencedNames = _referencedNames + def importedNames = List[Name]() + def definedNames = definesTerm.toList ++ definesType.toList + def definedSymbols = List[Symbol]() def extraCodeToEvaluate(req: Request): String = "" def resultExtractionCode(req: Request): String = "" @@ -130,6 +131,7 @@ trait MemberHandlers { } abstract class MacroHandler(member: DefDef) extends MemberDefHandler(member) { + override def referencedNames = super.referencedNames.flatMap(name => List(name.toTermName, name.toTypeName)) override def definesValue = false override def definesTerm: Option[TermName] = Some(name.toTermName) override def definesType: Option[TypeName] = None diff --git a/test/files/run/macro-bundle-repl.check b/test/files/run/macro-bundle-repl.check new file mode 100644 index 0000000000..b9c809f037 --- /dev/null +++ b/test/files/run/macro-bundle-repl.check @@ -0,0 +1,24 @@ +Type in expressions to have them evaluated. +Type :help for more information. + +scala> import scala.language.experimental.macros +import scala.language.experimental.macros + +scala> import scala.reflect.macros.Macro +import scala.reflect.macros.Macro + +scala> trait Bar extends Macro { def impl = c.literalUnit };def bar = macro Bar.impl +defined trait Bar +defined term macro bar: Unit + +scala> bar + +scala> trait Foo extends Macro { def impl = c.literalUnit } +defined trait Foo + +scala> def foo = macro Foo.impl +defined term macro foo: Unit + +scala> foo + +scala> diff --git a/test/files/run/macro-bundle-repl.scala b/test/files/run/macro-bundle-repl.scala new file mode 100644 index 0000000000..50783c8cba --- /dev/null +++ b/test/files/run/macro-bundle-repl.scala @@ -0,0 +1,13 @@ +import scala.tools.partest.ReplTest + +object Test extends ReplTest { + def code = """ +import scala.language.experimental.macros +import scala.reflect.macros.Macro +trait Bar extends Macro { def impl = c.literalUnit };def bar = macro Bar.impl +bar +trait Foo extends Macro { def impl = c.literalUnit } +def foo = macro Foo.impl +foo + """ +}
\ No newline at end of file |