Skip to content

Fix colons in printer #1742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 24, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,14 @@ class PlainPrinter(_ctx: Context) extends Printer {
"<noprefix>"
case tp: MethodType =>
def paramText(name: TermName, tp: Type) = toText(name) ~ ": " ~ toText(tp)
def typeColon(resultType: Type): Text = resultType match {
case _: TypeRef => ": "
case _ => "" // eg. methods with implicit parameters go here in first pass
}
changePrec(GlobalPrec) {
(if (tp.isImplicit) "(implicit " else "(") ~
Text((tp.paramNames, tp.paramTypes).zipped map paramText, ", ") ~
")" ~ toText(tp.resultType)
")" ~ typeColon(tp.resultType) ~ toText(tp.resultType)
}
case tp: ExprType =>
changePrec(GlobalPrec) { "=> " ~ toText(tp.resultType) }
Expand Down
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,12 @@ class CompilingInterpreter(
val varType = string2code(req.typeOf(varName))
val fullPath = req.fullPath(varName)

s""" + "$prettyName: $varType = " + {
val varOrVal = statement match {
case v: ValDef if v.mods is Flags.Mutable => "var"
case _ => "val"
}

s""" + "$varOrVal $prettyName: $varType = " + {
| if ($fullPath.asInstanceOf[AnyRef] != null) {
| (if ($fullPath.toString().contains('\\n')) "\\n" else "") +
| $fullPath.toString() + "\\n"
Expand Down Expand Up @@ -736,7 +741,7 @@ class CompilingInterpreter(

override def resultExtractionCode(req: Request, code: PrintWriter): Unit = {
if (!defDef.mods.is(Flags.AccessFlags))
code.print("+\"" + string2code(defDef.name.toString) + ": " +
code.print("+\"def " + string2code(defDef.name.toString) +
string2code(req.typeOf(defDef.name)) + "\\n\"")
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/repl/import.check
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
scala> import collection.mutable._
import collection.mutable._
scala> val buf = new ListBuffer[Int]
buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
val buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
scala> buf += 22
res0: scala.collection.mutable.ListBuffer[Int] = ListBuffer(22)
val res0: scala.collection.mutable.ListBuffer[Int] = ListBuffer(22)
scala> buf ++= List(1, 2, 3)
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(22, 1, 2, 3)
val res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(22, 1, 2, 3)
scala> buf.toList
res2: scala.collection.immutable.List[Int] = List(22, 1, 2, 3)
val res2: scala.collection.immutable.List[Int] = List(22, 1, 2, 3)
scala> :quit
4 changes: 2 additions & 2 deletions tests/repl/imports.check
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
scala> import scala.collection.mutable
import scala.collection.mutable
scala> val buf = mutable.ListBuffer[Int]()
buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
val buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
scala> object o { val xs = List(1, 2, 3) }
defined module o
scala> import o._
Expand All @@ -14,7 +14,7 @@ scala> buf += xs
| required: String
|
scala> buf ++= xs
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
val res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
scala> import util.foo
-- Error: <console> ------------------------------------------------------------
8 |import util.foo
Expand Down
2 changes: 1 addition & 1 deletion tests/repl/onePlusOne.check
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
scala> 1+1
res0: Int = 2
val res0: Int = 2
scala> :quit
14 changes: 7 additions & 7 deletions tests/repl/patdef.check
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
scala> val Const,x = 0
Const: Int = 0
x: Int = 0
scala> val Const, x = 0
val Const: Int = 0
val x: Int = 0
scala> val (Const, List(`x`, _, a), b) = (0, List(0, 1337, 1), 2)
a: Int = 1
b: Int = 2
val a: Int = 1
val b: Int = 2
scala> val a@b = 0
a: Int @unchecked = 0
b: Int @unchecked = 0
val a: Int @unchecked = 0
val b: Int @unchecked = 0
scala> :quit
2 changes: 1 addition & 1 deletion tests/repl/toplevelTry.check
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
scala> try { 0 } catch { _: Throwable => 1 }
res0: Int = 0
val res0: Int = 0
scala> :quit
4 changes: 2 additions & 2 deletions tests/repl/vars.check
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
scala> var x = 0
x: Int = 0
var x: Int = 0
scala> x = x + 1
x: Int = 1
scala> x *= 2
scala> x
res2: Int = 2
val res2: Int = 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hesitant to do this. Looks very odd to me given that we initially created x as a var.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, the REPL currently has semantics that are not well defined. There's no spec for it 😅

But, if you want to have a closer look at its semantics, by all means! We'd be happy to get something that is clearer :)

scala> :quit