Skip to content

Commit 08e1733

Browse files
committed
Fix defs not being printed correctly
1 parent b33c962 commit 08e1733

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -740,9 +740,28 @@ class CompilingInterpreter(
740740
override def defNames = boundNames
741741

742742
override def resultExtractionCode(req: Request, code: PrintWriter): Unit = {
743-
if (!defDef.mods.is(Flags.AccessFlags))
744-
code.print("+\"def " + string2code(defDef.name.toString) +
745-
string2code(req.typeOf(defDef.name)) + "\\n\"")
743+
/** TODO: This is the result of the state of the REPL - this would be
744+
* entirely unnecessary with a better structure where we could just
745+
* use the type printer
746+
*
747+
* @see `def findTypes` for an explanation of what should be done
748+
*/
749+
if (!defDef.mods.is(Flags.AccessFlags)) {
750+
val tpt = defDef.tpt match {
751+
// ascribed TypeExpr e.g: `def foo: Int = 5`
752+
case Ident(tpt) if defDef.vparamss.isEmpty =>
753+
": " + tpt.show
754+
// inferred TypeExpr e.g: `def foo = 5`
755+
case tpt if defDef.vparamss.isEmpty =>
756+
": " + req.typeOf(defDef.name)
757+
// Inferred or ascribed MethodType with parameter list
758+
case _ =>
759+
req.typeOf(defDef.name)
760+
}
761+
code.print {
762+
"+\"def " + string2code(defDef.name.toString) + tpt + "\\n\""
763+
}
764+
}
746765
}
747766
}
748767

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

0 commit comments

Comments
 (0)