diff options
author | Som Snytt <som.snytt@gmail.com> | 2016-05-17 00:00:52 -0700 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2016-05-17 00:00:52 -0700 |
commit | e753135f02a8177e809937e56fed5c054091691f (patch) | |
tree | 534ba911ccf61ae0f1db820e880cba06b44125c1 /src/compiler/scala/tools/nsc/ast/parser/Parsers.scala | |
parent | ee365cccaf740d5ec353718556b010137f4cdd4d (diff) | |
download | scala-e753135f02a8177e809937e56fed5c054091691f.tar.gz scala-e753135f02a8177e809937e56fed5c054091691f.tar.bz2 scala-e753135f02a8177e809937e56fed5c054091691f.zip |
SI-4625 Warn when discarding script object
It's pretty confusing when your script object becomes a local
and then nothing happens. Such as when you're writing a test and
it takes forever to figure out what's going on.
Diffstat (limited to 'src/compiler/scala/tools/nsc/ast/parser/Parsers.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/ast/parser/Parsers.scala | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala index 1ece580b96..c2f2141fd3 100644 --- a/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala +++ b/src/compiler/scala/tools/nsc/ast/parser/Parsers.scala @@ -389,8 +389,8 @@ self => } /* For now we require there only be one top level object. */ var seenModule = false + var disallowed = EmptyTree: Tree val newStmts = stmts collect { - case t @ Import(_, _) => t 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 @@ -404,11 +404,18 @@ self => else treeCopy.ModuleDef(md, mods, mainModuleName, template) case md @ ModuleDef(_, _, _) => md case cd @ ClassDef(_, _, _, _) => cd - case _ => + case t @ Import(_, _) => t + case t => /* If we see anything but the above, fail. */ - return None + disallowed = t + EmptyTree + } + if (disallowed.isEmpty) Some(makeEmptyPackage(0, newStmts)) + else { + if (seenModule) + warning(disallowed.pos.point, "Script has a main object but statement is disallowed") + None } - Some(makeEmptyPackage(0, newStmts)) } if (mainModuleName == newTermName(ScriptRunner.defaultScriptMain)) |