diff options
author | Som Snytt <som.snytt@gmail.com> | 2016-05-16 16:24:50 -0700 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2016-05-16 21:08:44 -0700 |
commit | 73ca44be579e5100706d174f18025fc4487e9cb9 (patch) | |
tree | c68b732b86ac053419d859b8cb107b24bac4586b | |
parent | b3f8332cf399cd15067c879c8297c25598045883 (diff) | |
download | scala-73ca44be579e5100706d174f18025fc4487e9cb9.tar.gz scala-73ca44be579e5100706d174f18025fc4487e9cb9.tar.bz2 scala-73ca44be579e5100706d174f18025fc4487e9cb9.zip |
SI-4625 Recognize App in script
Cheap name test: if the script object extends "App",
take it for a main-bearing parent.
Note that if `-Xscript` is not `Main`, the default,
then the source is taken as a snippet and there is
no attempt to locate an existing `main` method.
-rw-r--r-- | src/compiler/scala/tools/nsc/ast/parser/Parsers.scala | 6 | ||||
-rw-r--r-- | test/files/run/t4625.check | 1 | ||||
-rw-r--r-- | test/files/run/t4625.scala | 7 | ||||
-rw-r--r-- | test/files/run/t4625.script | 5 |
4 files changed, 18 insertions, 1 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index c04d305f9e..7af5c505de 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -380,11 +380,15 @@ self => case DefDef(_, nme.main, Nil, List(_), _, _) => true case _ => false } + def isApp(t: Tree) = t match { + case Template(ps, _, _) => ps.exists { case Ident(x) if x.decoded == "App" => true ; case _ => false } + case _ => false + } /* For now we require there only be one top level object. */ var seenModule = false val newStmts = stmts collect { case t @ Import(_, _) => t - case md @ ModuleDef(mods, name, template) if !seenModule && (md exists isMainMethod) => + case md @ ModuleDef(mods, name, template) if !seenModule && (isApp(template) || md.exists(isMainMethod)) => seenModule = true /* This slightly hacky situation arises because we have no way to communicate * back to the scriptrunner what the name of the program is. Even if we were diff --git a/test/files/run/t4625.check b/test/files/run/t4625.check new file mode 100644 index 0000000000..e4a4d15b87 --- /dev/null +++ b/test/files/run/t4625.check @@ -0,0 +1 @@ +Test ran. diff --git a/test/files/run/t4625.scala b/test/files/run/t4625.scala new file mode 100644 index 0000000000..44f6225220 --- /dev/null +++ b/test/files/run/t4625.scala @@ -0,0 +1,7 @@ + +import scala.tools.partest.ScriptTest + +object Test extends ScriptTest { + // must be called Main to get probing treatment in parser + override def testmain = "Main" +} diff --git a/test/files/run/t4625.script b/test/files/run/t4625.script new file mode 100644 index 0000000000..600ceacbb6 --- /dev/null +++ b/test/files/run/t4625.script @@ -0,0 +1,5 @@ + +object Main extends Runnable with App { + def run() = println("Test ran.") + run() +} |