summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/compiler/scala/tools/nsc/interpreter/IMain.scala58
-rw-r--r--test/files/run/repl-parens.check19
-rw-r--r--test/files/run/repl-parens.scala8
3 files changed, 61 insertions, 24 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/IMain.scala b/src/compiler/scala/tools/nsc/interpreter/IMain.scala
index 83a9669427..6a61f107c8 100644
--- a/src/compiler/scala/tools/nsc/interpreter/IMain.scala
+++ b/src/compiler/scala/tools/nsc/interpreter/IMain.scala
@@ -440,34 +440,44 @@ class IMain(val settings: Settings, protected val out: PrintWriter) extends Impo
trees.last match {
case _:Assign => // we don't want to include assignments
case _:TermTree | _:Ident | _:Select => // ... but do want other unnamed terms.
- // The position of the last tree
- val lastpos0 = earliestPosition(trees.last)
- // Oh boy, the parser throws away parens so "(2+2)" is mispositioned.
- // So until we can fix the parser we'll have to go trawling.
- val lastpos = {
- val adjustment = (content take lastpos0).reverse takeWhile (ch => ch.isWhitespace || ch == '(') length;
- lastpos0 - adjustment
- }
- // the source code split at the laboriously determined position.
- val (l1, l2) = content splitAt lastpos
- val prefix = if (l1.trim == "") "" else l1 + ";\n"
val varName = if (synthetic) freshInternalVarName() else freshUserVarName()
- // Note to self: val source needs to have this precise structure so that
- // error messages print the user-submitted part without the "val res0 = " part.
- val combined = prefix + "val " + varName + " =\n" + l2
-
- repldbg(List(
- " line" -> line,
- " content" -> content,
- " was" -> l2,
- "combined" -> combined) map {
- case (label, s) => label + ": '" + s + "'"
- } mkString "\n"
+ val rewrittenLine = (
+ // In theory this would come out the same without the 1-specific test, but
+ // it's a cushion against any more sneaky parse-tree position vs. code mismatches:
+ // this way such issues will only arise on multiple-statement repl input lines,
+ // which most people don't use.
+ if (trees.size == 1) "val " + varName + " =\n" + content
+ else {
+ // The position of the last tree
+ val lastpos0 = earliestPosition(trees.last)
+ // Oh boy, the parser throws away parens so "(2+2)" is mispositioned.
+ // So until we can fix the parser we'll have to go trawling.
+ val adjustment = ((content take lastpos0).reverse takeWhile { ch =>
+ ch.isWhitespace || ch == '(' || ch == ')'
+ }).length
+ val lastpos = lastpos0 - adjustment
+
+ // the source code split at the laboriously determined position.
+ val (l1, l2) = content splitAt lastpos
+ val prefix = if (l1.trim == "") "" else l1 + ";\n"
+ // Note to self: val source needs to have this precise structure so that
+ // error messages print the user-submitted part without the "val res0 = " part.
+ val combined = prefix + "val " + varName + " =\n" + l2
+
+ repldbg(List(
+ " line" -> line,
+ " content" -> content,
+ " was" -> l2,
+ "combined" -> combined) map {
+ case (label, s) => label + ": '" + s + "'"
+ } mkString "\n"
+ )
+ combined
+ }
)
-
// Rewriting "foo ; bar ; 123"
// to "foo ; bar ; val resXX = 123"
- requestFromLine(combined, synthetic) match {
+ requestFromLine(rewrittenLine, synthetic) match {
case Right(req) => return Right(req withOriginalLine line)
case x => return x
}
diff --git a/test/files/run/repl-parens.check b/test/files/run/repl-parens.check
index eeb21c67d0..79db06f272 100644
--- a/test/files/run/repl-parens.check
+++ b/test/files/run/repl-parens.check
@@ -25,3 +25,22 @@ scala> 55 ; ((2 + 2)) ; (1, 2, 3)
res6: (Int, Int, Int) = (1,2,3)
scala>
+
+scala> () => 5
+res7: () => Int = <function0>
+
+scala> 55 ; () => 5
+res8: () => Int = <function0>
+
+scala> () => { class X ; new X }
+res9: () => java.lang.Object with ScalaObject = <function0>
+
+scala>
+
+scala> def foo(x: Int)(y: Int)(z: Int) = x+y+z
+foo: (x: Int)(y: Int)(z: Int)Int
+
+scala> foo(5)(10)(15)+foo(5)(10)(15)
+res10: Int = 60
+
+scala>
diff --git a/test/files/run/repl-parens.scala b/test/files/run/repl-parens.scala
index cdfe53efe7..3b2740837c 100644
--- a/test/files/run/repl-parens.scala
+++ b/test/files/run/repl-parens.scala
@@ -10,5 +10,13 @@ object Test extends ReplTest {
((((2 + 2)), ((2 + 2)), 2).productIterator ++ Iterator(3) mkString)
55 ; ((2 + 2)) ; (1, 2, 3)
+
+() => 5
+55 ; () => 5
+() => { class X ; new X }
+
+def foo(x: Int)(y: Int)(z: Int) = x+y+z
+foo(5)(10)(15)+foo(5)(10)(15)
+
""".trim
} \ No newline at end of file