Skip to content

Commit b26af1b

Browse files
authored
Merge pull request #1742 from dotty-staging/topic/colon-in-printer
Fix colons in printer
2 parents 3588832 + c52ffd7 commit b26af1b

File tree

8 files changed

+60
-22
lines changed

8 files changed

+60
-22
lines changed

compiler/src/dotty/tools/dotc/repl/CompilingInterpreter.scala

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,10 @@ class CompilingInterpreter(
443443
}
444444

445445
// the types are all =>T; remove the =>
446-
val cleanedType = rawType.widenExpr
446+
val cleanedType = rawType.widenExpr match {
447+
case tp: MethodType => tp.resultType
448+
case tp => tp
449+
}
447450

448451
map + (name ->
449452
ctx.atPhase(ctx.typerPhase.next) { implicit ctx =>
@@ -685,7 +688,12 @@ class CompilingInterpreter(
685688
val varType = string2code(req.typeOf(varName))
686689
val fullPath = req.fullPath(varName)
687690

688-
s""" + "$prettyName: $varType = " + {
691+
val varOrVal = statement match {
692+
case v: ValDef if v.mods is Flags.Mutable => "var"
693+
case _ => "val"
694+
}
695+
696+
s""" + "$varOrVal $prettyName: $varType = " + {
689697
| if ($fullPath.asInstanceOf[AnyRef] != null) {
690698
| (if ($fullPath.toString().contains('\\n')) "\\n" else "") +
691699
| $fullPath.toString() + "\\n"
@@ -735,9 +743,30 @@ class CompilingInterpreter(
735743
override def defNames = boundNames
736744

737745
override def resultExtractionCode(req: Request, code: PrintWriter): Unit = {
738-
if (!defDef.mods.is(Flags.AccessFlags))
739-
code.print("+\"" + string2code(defDef.name.toString) + ": " +
740-
string2code(req.typeOf(defDef.name)) + "\\n\"")
746+
/** TODO: This is the result of the state of the REPL - this would be
747+
* entirely unnecessary with a better structure where we could just
748+
* use the type printer
749+
*
750+
* @see `def findTypes` for an explanation of what should be done
751+
*/
752+
if (!defDef.mods.is(Flags.AccessFlags)) {
753+
// Take the DefDef and remove the `rhs` and ascribed type `tpt`
754+
val copy = ast.untpd.cpy.DefDef(defDef)(
755+
rhs = EmptyTree,
756+
tpt = TypeTree
757+
)
758+
759+
val tpt = defDef.tpt match {
760+
// ascribed TypeExpr e.g: `def foo: Int = 5`
761+
case Ident(tpt) if defDef.vparamss.isEmpty =>
762+
": " + tpt.show
763+
case tpt =>
764+
": " + req.typeOf(defDef.name)
765+
}
766+
code.print {
767+
"+\"" + string2code(copy.show) + tpt + "\\n\""
768+
}
769+
}
741770
}
742771
}
743772

tests/repl/def.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
scala> def foo = 5
2+
def foo: Int
3+
scala> def bar: String = "1"
4+
def bar: String
5+
scala> def baz() = 2
6+
def baz(): Int
7+
scala> def qux(): Int = 2
8+
def qux(): Int
9+
scala> :quit

tests/repl/import.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
scala> import collection.mutable._
22
import collection.mutable._
33
scala> val buf = new ListBuffer[Int]
4-
buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
4+
val buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
55
scala> buf += 22
6-
res0: scala.collection.mutable.ListBuffer[Int] = ListBuffer(22)
6+
val res0: scala.collection.mutable.ListBuffer[Int] = ListBuffer(22)
77
scala> buf ++= List(1, 2, 3)
8-
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(22, 1, 2, 3)
8+
val res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(22, 1, 2, 3)
99
scala> buf.toList
10-
res2: scala.collection.immutable.List[Int] = List(22, 1, 2, 3)
10+
val res2: scala.collection.immutable.List[Int] = List(22, 1, 2, 3)
1111
scala> :quit

tests/repl/imports.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
scala> import scala.collection.mutable
22
import scala.collection.mutable
33
scala> val buf = mutable.ListBuffer[Int]()
4-
buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
4+
val buf: scala.collection.mutable.ListBuffer[Int] = ListBuffer()
55
scala> object o { val xs = List(1, 2, 3) }
66
defined module o
77
scala> import o._
@@ -14,7 +14,7 @@ scala> buf += xs
1414
| required: String
1515
|
1616
scala> buf ++= xs
17-
res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
17+
val res1: scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3)
1818
scala> import util.foo
1919
-- Error: <console> ------------------------------------------------------------
2020
8 |import util.foo

tests/repl/onePlusOne.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
scala> 1+1
2-
res0: Int = 2
2+
val res0: Int = 2
33
scala> :quit

tests/repl/patdef.check

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
scala> val Const,x = 0
2-
Const: Int = 0
3-
x: Int = 0
1+
scala> val Const, x = 0
2+
val Const: Int = 0
3+
val x: Int = 0
44
scala> val (Const, List(`x`, _, a), b) = (0, List(0, 1337, 1), 2)
5-
a: Int = 1
6-
b: Int = 2
5+
val a: Int = 1
6+
val b: Int = 2
77
scala> val a@b = 0
8-
a: Int @unchecked = 0
9-
b: Int @unchecked = 0
8+
val a: Int @unchecked = 0
9+
val b: Int @unchecked = 0
1010
scala> :quit

tests/repl/toplevelTry.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
scala> try { 0 } catch { _: Throwable => 1 }
2-
res0: Int = 0
2+
val res0: Int = 0
33
scala> :quit

tests/repl/vars.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
scala> var x = 0
2-
x: Int = 0
2+
var x: Int = 0
33
scala> x = x + 1
44
x: Int = 1
55
scala> x *= 2
66
scala> x
7-
res2: Int = 2
7+
val res2: Int = 2
88
scala> :quit

0 commit comments

Comments
 (0)