summaryrefslogtreecommitdiff
path: root/src/repl
diff options
context:
space:
mode:
authorSeth Tisue <seth@tisue.net>2015-08-20 16:06:13 -0400
committerSeth Tisue <seth@tisue.net>2015-08-20 16:15:17 -0400
commitded2d4f2e613aad6670b3e5dccbab0feb711b18b (patch)
tree665fc16d4932a6d8625fe28ae87c100bb1ee96c1 /src/repl
parentcd77e23b76af8538ed7e2c5c90e7845582dff460 (diff)
parenta77e34e4b1ed445368b8412dbbf07340554afd57 (diff)
downloadscala-ded2d4f2e613aad6670b3e5dccbab0feb711b18b.tar.gz
scala-ded2d4f2e613aad6670b3e5dccbab0feb711b18b.tar.bz2
scala-ded2d4f2e613aad6670b3e5dccbab0feb711b18b.zip
Merge remote-tracking branch 'origin/2.11.x' into 2.12.x
all conflicts were because the changes changed code that doesn't exist anymore in 2.12; they were resolved with `git checkout --ours` c201eac changed bincompat-forward.whitelist.conf but I dropped the change in this merge because it refers to AbstractPromise which no longer exists in 2.12
Diffstat (limited to 'src/repl')
-rw-r--r--src/repl/scala/tools/nsc/interpreter/IMain.scala3
-rw-r--r--src/repl/scala/tools/nsc/interpreter/Imports.scala31
2 files changed, 23 insertions, 11 deletions
diff --git a/src/repl/scala/tools/nsc/interpreter/IMain.scala b/src/repl/scala/tools/nsc/interpreter/IMain.scala
index 841b4abfa5..06ae179da9 100644
--- a/src/repl/scala/tools/nsc/interpreter/IMain.scala
+++ b/src/repl/scala/tools/nsc/interpreter/IMain.scala
@@ -888,7 +888,7 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set
/** Code to import bound names from previous lines - accessPath is code to
* append to objectName to access anything bound by request.
*/
- lazy val ComputedImports(importsPreamble, importsTrailer, accessPath) =
+ lazy val ComputedImports(headerPreamble, importsPreamble, importsTrailer, accessPath) =
exitingTyper(importsCode(referencedNames.toSet, ObjectSourceCode, definesClass))
/** the line of code to compute */
@@ -908,6 +908,7 @@ class IMain(@BeanProperty val factory: ScriptEngineFactory, initialSettings: Set
else List("def %s = %s".format("$line", tquoted(originalLine)), "def %s = Nil".format("$trees"))
}
def preamble = s"""
+ |$headerPreamble
|${preambleHeader format lineRep.readName}
|${envLines mkString (" ", ";\n ", ";\n")}
|$importsPreamble
diff --git a/src/repl/scala/tools/nsc/interpreter/Imports.scala b/src/repl/scala/tools/nsc/interpreter/Imports.scala
index 3ec77e46f1..5b231d94b6 100644
--- a/src/repl/scala/tools/nsc/interpreter/Imports.scala
+++ b/src/repl/scala/tools/nsc/interpreter/Imports.scala
@@ -70,7 +70,10 @@ trait Imports {
/** Compute imports that allow definitions from previous
* requests to be visible in a new request. Returns
- * three pieces of related code:
+ * three or four pieces of related code:
+ *
+ * 0. Header code fragment that should go at the beginning
+ * of the compilation unit, specifically, import Predef.
*
* 1. An initial code fragment that should go before
* the code of the new request.
@@ -91,30 +94,34 @@ trait Imports {
* (3) It imports multiple same-named implicits, but only the
* last one imported is actually usable.
*/
- case class ComputedImports(prepend: String, append: String, access: String)
+ case class ComputedImports(header: String, prepend: String, append: String, access: String)
protected def importsCode(wanted: Set[Name], wrapper: Request#Wrapper, definesClass: Boolean): ComputedImports = {
+ val header, code, trailingBraces, accessPath = new StringBuilder
+ val currentImps = mutable.HashSet[Name]()
+ var predefEscapes = false // only emit predef import header if name not resolved in history, loosely
+
/** Narrow down the list of requests from which imports
* should be taken. Removes requests which cannot contribute
* useful imports for the specified set of wanted names.
*/
- case class ReqAndHandler(req: Request, handler: MemberHandler) { }
+ case class ReqAndHandler(req: Request, handler: MemberHandler)
def reqsToUse: List[ReqAndHandler] = {
/** Loop through a list of MemberHandlers and select which ones to keep.
- * 'wanted' is the set of names that need to be imported.
+ * 'wanted' is the set of names that need to be imported.
*/
def select(reqs: List[ReqAndHandler], wanted: Set[Name]): List[ReqAndHandler] = {
// Single symbol imports might be implicits! See bug #1752. Rather than
// try to finesse this, we will mimic all imports for now.
def keepHandler(handler: MemberHandler) = handler match {
- /* While defining classes in class based mode - implicits are not needed. */
+ // While defining classes in class based mode - implicits are not needed.
case h: ImportHandler if isClassBased && definesClass => h.importedNames.exists(x => wanted.contains(x))
case _: ImportHandler => true
case x => x.definesImplicit || (x.definedNames exists wanted)
}
reqs match {
- case Nil => Nil
+ case Nil => predefEscapes = wanted contains PredefModule.name ; Nil
case rh :: rest if !keepHandler(rh.handler) => select(rest, wanted)
case rh :: rest =>
import rh.handler._
@@ -127,9 +134,6 @@ trait Imports {
select(allReqAndHandlers reverseMap { case (r, h) => ReqAndHandler(r, h) }, wanted).reverse
}
- val code, trailingBraces, accessPath = new StringBuilder
- val currentImps = mutable.HashSet[Name]()
-
// add code for a new object to hold some imports
def addWrapper() {
import nme.{ INTERPRETER_IMPORT_WRAPPER => iw }
@@ -146,6 +150,9 @@ trait Imports {
try op finally addWrapper()
}
+ // imports from Predef are relocated to the template header to allow hiding.
+ def checkHeader(h: ImportHandler) = h.referencedNames contains PredefModule.name
+
// loop through previous requests, adding imports for each one
wrapBeforeAndAfter {
// Reusing a single temporary value when import from a line with multiple definitions.
@@ -153,6 +160,9 @@ trait Imports {
for (ReqAndHandler(req, handler) <- reqsToUse) {
val objName = req.lineRep.readPathInstance
handler match {
+ case h: ImportHandler if checkHeader(h) =>
+ header.clear()
+ header append f"${h.member}%n"
// If the user entered an import, then just use it; add an import wrapping
// level if the import might conflict with some other import
case x: ImportHandler if x.importsWildcard =>
@@ -194,7 +204,8 @@ trait Imports {
}
}
- ComputedImports(code.toString, trailingBraces.toString, accessPath.toString)
+ val computedHeader = if (predefEscapes) header.toString else ""
+ ComputedImports(computedHeader, code.toString, trailingBraces.toString, accessPath.toString)
}
private def allReqAndHandlers =