Skip to content

Commit

Permalink
REPL print remove comments from definitions, print macro as def
Browse files Browse the repository at this point in the history
also removed `= <method>` from def and `= <lazy>` from lazy val
refactored implementation of withoutImplicit
  • Loading branch information
bishabosha committed Aug 7, 2019
1 parent 6f3835b commit 71823e9
Show file tree
Hide file tree
Showing 74 changed files with 231 additions and 232 deletions.
10 changes: 5 additions & 5 deletions src/repl/scala/tools/nsc/interpreter/IMain.scala
Expand Up @@ -717,19 +717,19 @@ class IMain(val settings: Settings, parentClassLoaderOverride: Option[ClassLoade

private def methodTypeAsDef(tp: Type): String = {

def withoutImplicit(sym: Symbol): Symbol = sym.cloneSymbol(sym.owner, sym.flags - Flag.IMPLICIT)
def withoutImplicit(sym: Symbol): Symbol = sym.cloneSymbol(sym.owner, sym.flags & ~Flag.IMPLICIT)

def formatParams(params: List[Symbol]): String = {
if (params.headOption.exists(_.isImplicit)) implicitParens(params.view.map(withoutImplicit(_).defString))
else parens(params.view.map(_.defString))
if (params.headOption.exists(_.isImplicit)) implicitParens(params.map(withoutImplicit(_).defString))
else parens(params.map(_.defString))
}

@tailrec
def loop(tp: Type, acc: StringBuilder): StringBuilder = tp match {
def loop(tpe: Type, acc: StringBuilder): StringBuilder = tpe match {
case NullaryMethodType(resultType) => acc ++= s": $resultType"
case PolyType(tyParams, resultType) => loop(resultType, acc ++= tyParens(tyParams.map(_.defString)))
case MethodType(params, resultType) => loop(resultType, acc ++= formatParams(params))
case _ => acc ++= s": $tp"
case other => acc ++= s": $other"
}

loop(tp, new StringBuilder).toString
Expand Down
17 changes: 8 additions & 9 deletions src/repl/scala/tools/nsc/interpreter/MemberHandlers.scala
Expand Up @@ -133,8 +133,8 @@ trait MemberHandlers {
else {
// if this is a lazy val we avoid evaluating it here
val resultString =
if (mods.isLazy) codegenln(false, "<lazy>")
else any2stringOf(path, maxStringElements)
if (mods.isLazy) """"""""
else """" = " + """ + any2stringOf(path, maxStringElements)

val varOrValOrLzy =
if (mods.isMutable) "var"
Expand All @@ -148,16 +148,15 @@ trait MemberHandlers {
)
}

val typeString = string2code(req typeOf name)
s""" + "$varOrValOrLzy $nameString: $typeString = " + $resultString"""
s""" + "$varOrValOrLzy $nameString: ${req.typeOf(name)}" + $resultString"""
}
}
}

class DefHandler(member: DefDef) extends MemberDefHandler(member) {
override def definesValue = flattensToEmpty(member.vparamss) // true if 0-arity
override def resultExtractionCode(req: Request) =
if (mods.isPublic) codegenln(s"def ${req.defTypeOf(name)} = <method>") else ""
if (mods.isPublic) codegenln(s"def ${req.defTypeOf(name)}") else ""
}

abstract class MacroHandler(member: DefDef) extends MemberDefHandler(member) {
Expand All @@ -170,7 +169,7 @@ trait MemberHandlers {
}

class TermMacroHandler(member: DefDef) extends MacroHandler(member) {
def notification(req: Request) = s"// defined term macro ${req.defTypeOf(name)}"
def notification(req: Request) = s"def ${req.defTypeOf(name)}"
}

class AssignHandler(member: Assign) extends MemberHandler(member) {
Expand All @@ -182,22 +181,22 @@ trait MemberHandlers {
override def definesTerm = Some(name.toTermName)
override def definesValue = true

override def resultExtractionCode(req: Request) = codegenln("// defined object ", name)
override def resultExtractionCode(req: Request) = codegenln(s"object $name")
}

class ClassHandler(member: ClassDef) extends MemberDefHandler(member) {
override def definedSymbols = List(symbol, symbol.companionSymbol) filterNot (_ == NoSymbol)
override def definesType = Some(name.toTypeName)
override def definesTerm = Some(name.toTermName) filter (_ => mods.isCase)

override def resultExtractionCode(req: Request) = codegenln(s"// defined $keyword $name")
override def resultExtractionCode(req: Request) = codegenln(s"$keyword $name")
}

class TypeAliasHandler(member: TypeDef) extends MemberDefHandler(member) {
private def isAlias = mods.isPublic && treeInfo.isAliasTypeDef(member)
override def definesType = Some(name.toTypeName) filter (_ => isAlias)

override def resultExtractionCode(req: Request) = codegenln(s"// defined type alias $name")
override def resultExtractionCode(req: Request) = codegenln(s"type $name")
}

class ImportHandler(imp: Import) extends MemberHandler(imp) {
Expand Down
2 changes: 1 addition & 1 deletion src/repl/scala/tools/nsc/interpreter/ReplStrings.scala
Expand Up @@ -41,7 +41,7 @@ object ReplStrings {
"\"" + string2code(str) + "\""

def any2stringOf(x: Any, maxlen: Int) =
"_root_.scala.runtime.ScalaRunTime.replStringOf(%s, %s)".format(x, maxlen)
s"_root_.scala.runtime.ScalaRunTime.replStringOf($x, $maxlen)"

// no escaped or nested quotes
private[this] val inquotes = """(['"])(.*?)\1""".r
Expand Down
26 changes: 13 additions & 13 deletions test/files/jvm/interpreter.check
Expand Up @@ -9,7 +9,7 @@ scala> def gcd(x: Int, y: Int): Int = {
else if (y == 0) x
else gcd(y%x, x)
}
def gcd(x: Int, y: Int): Int = <method>
def gcd(x: Int, y: Int): Int

scala> val five = gcd(15,35)
val five: Int = 5
Expand All @@ -24,7 +24,7 @@ scala> val three = x+1
val three: Int = 3

scala> type anotherint = Int
// defined type alias anotherint
type anotherint

scala> val four: anotherint = 4
val four: anotherint = 4
Expand All @@ -37,7 +37,7 @@ scala> val bogus: anotherint = "hello"
(which expands to) Int

scala> trait PointlessTrait
// defined trait PointlessTrait
trait PointlessTrait

scala> val (x,y) = (2,3)
val x: Int = 2
Expand All @@ -56,7 +56,7 @@ val t1513: Array[Null] = Array(null)
scala> // overriding toString problem from #1404

scala> class S(override val toString : String)
// defined class S
class S

scala> val fish = new S("fish")
val fish: S = fish
Expand All @@ -81,14 +81,14 @@ scala>
scala> // implicit conversions

scala> case class Foo(n: Int)
// defined class Foo
class Foo

scala> case class Bar(n: Int)
// defined class Bar
class Bar

scala> implicit def foo2bar(foo: Foo) = Bar(foo.n)
warning: there was one feature warning; for details, enable `:setting -feature' or `:replay -feature'
def foo2bar(foo: Foo): Bar = <method>
def foo2bar(foo: Foo): Bar

scala> val bar: Bar = Foo(3)
val bar: Bar = Bar(3)
Expand Down Expand Up @@ -314,7 +314,7 @@ You typed two blank lines. Starting a new command.
scala> // defining and using quoted names should work (ticket #323)

scala> def `match` = 1
def match: Int = <method>
def match: Int

scala> val x = `match`
val x: Int = 1
Expand All @@ -324,20 +324,20 @@ scala>
scala> // multiple classes defined on one line

scala> sealed class Exp; class Fact extends Exp; class Term extends Exp
// defined class Exp
// defined class Fact
// defined class Term
class Exp
class Fact
class Term

scala> def f(e: Exp) = e match { // non-exhaustive warning here
case _:Fact => 3
}
^
warning: match may not be exhaustive.
It would fail on the following inputs: Exp(), Term()
def f(e: Exp): Int = <method>
def f(e: Exp): Int

scala> :quit
def plusOne(x: Int): Int = <method>
def plusOne(x: Int): Int
val res0: Int = 6
val res0: String = after reset
^
Expand Down
2 changes: 1 addition & 1 deletion test/files/run/class-symbol-contravariant.check
Expand Up @@ -12,7 +12,7 @@ import u._
import scala.reflect.internal.Flags

scala> class C
// defined class C
class C

scala> val sym = u.typeOf[C].typeSymbol
val sym: u.Symbol = class C
Expand Down
22 changes: 11 additions & 11 deletions test/files/run/constrained-types.check
@@ -1,6 +1,6 @@

scala> class Annot(obj: Any) extends annotation.Annotation with annotation.TypeConstraint
// defined class Annot
class Annot

scala>

Expand All @@ -9,7 +9,7 @@ scala> class A {
val y: Int @Annot(x) = 10
override def toString = "an A"
}
// defined class A
class A

scala>

Expand All @@ -31,7 +31,7 @@ scala> object Stuff {
val x = "hello"
val y : Int @Annot(x) = 10
}
// defined object Stuff
object Stuff

scala>

Expand All @@ -44,7 +44,7 @@ scala> class B {
val y: Int @Annot(Stuff.x) = 10
override def toString = "a B"
}
// defined class B
class B

scala>

Expand All @@ -55,7 +55,7 @@ scala> val y = b.y // should keep the annotation
val y: Int @Annot(Stuff.x) = 10

scala> def m(x: String): String @Annot(x) = x
def m(x: String): String @Annot(x) = <method>
def m(x: String): String @Annot(x)

scala>

Expand All @@ -82,12 +82,12 @@ val stuff: String @Annot("stuff") = stuff
scala>

scala> class peer extends annotation.Annotation // should not crash
// defined class peer
class peer

scala>

scala> class NPE[T <: NPE[T] @peer] // should not crash
// defined class NPE
class NPE

scala>

Expand All @@ -97,7 +97,7 @@ scala> def m = {
y
} // x should not escape the local scope with a narrow type
warning: there was one feature warning; for details, enable `:setting -feature' or `:replay -feature'
def m: String @Annot(x) forSome { val x: String } = <method>
def m: String @Annot(x) forSome { val x: String }

scala>

Expand All @@ -111,17 +111,17 @@ scala> def n(y: String) = {
m("stuff".stripMargin)
} // x should be existentially bound
warning: there was one feature warning; for details, enable `:setting -feature' or `:replay -feature'
def n(y: String): String @Annot(x) forSome { val x: String } = <method>
def n(y: String): String @Annot(x) forSome { val x: String }

scala>

scala> class rep extends annotation.Annotation { }
// defined class rep
class rep

scala>

scala> object A { val x = "hello" : String @ rep }
// defined object A
object A
warning: previously defined class A is not a companion to object A.
Companions must be defined together; you may wish to use :paste mode for this.

Expand Down
2 changes: 1 addition & 1 deletion test/files/run/invalid-lubs.check
@@ -1,6 +1,6 @@

scala> def foo(a: Boolean, b: List[Any], c: collection.mutable.ListBuffer[Any]) = if (a) b else c
def foo(a: Boolean, b: List[Any], c: scala.collection.mutable.ListBuffer[Any]): scala.collection.AbstractSeq[Any] with scala.collection.StrictOptimizedSeqOps[Any,[_]scala.collection.AbstractSeq[_] with scala.collection.StrictOptimizedSeqOps[_,[_]scala.collection.AbstractSeq[_] with scala.collection.StrictOptimizedSeqOps[_,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[_] with scala.collection.StrictOptimizedSeqOps[_,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable{def iterableFactory: scala.collection.SeqFactory[[A]scala.collection.AbstractSeq[A] with scala.collection.generic.DefaultSerializable]},scala.collection.AbstractSeq[Any] with scala.collection.StrictOptimizedSeqOps[Any,[_]scala.collection.AbstractSeq[_] with scala.collection.StrictOptimizedSeqOps[_,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[Any] with scala.collection.StrictOptimizedSeqOps[Any,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[Any] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable{def iterableFactory: scala.collection.SeqFactory[[A]scala.collection.AbstractSeq[A] with scala.collection.generic.DefaultSerializable]}] with scala.collection.generic.DefaultSerializable{def iterableFactory: scala.collection.SeqFactory[[A]scala.collection.AbstractSeq[A] with scala.collection.StrictOptimizedSeqOps[A,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[A] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable]} = <method>
def foo(a: Boolean, b: List[Any], c: scala.collection.mutable.ListBuffer[Any]): scala.collection.AbstractSeq[Any] with scala.collection.StrictOptimizedSeqOps[Any,[_]scala.collection.AbstractSeq[_] with scala.collection.StrictOptimizedSeqOps[_,[_]scala.collection.AbstractSeq[_] with scala.collection.StrictOptimizedSeqOps[_,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[_] with scala.collection.StrictOptimizedSeqOps[_,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable{def iterableFactory: scala.collection.SeqFactory[[A]scala.collection.AbstractSeq[A] with scala.collection.generic.DefaultSerializable]},scala.collection.AbstractSeq[Any] with scala.collection.StrictOptimizedSeqOps[Any,[_]scala.collection.AbstractSeq[_] with scala.collection.StrictOptimizedSeqOps[_,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[Any] with scala.collection.StrictOptimizedSeqOps[Any,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[Any] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable{def iterableFactory: scala.collection.SeqFactory[[A]scala.collection.AbstractSeq[A] with scala.collection.generic.DefaultSerializable]}] with scala.collection.generic.DefaultSerializable{def iterableFactory: scala.collection.SeqFactory[[A]scala.collection.AbstractSeq[A] with scala.collection.StrictOptimizedSeqOps[A,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[A] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable]}

scala> List(List[Any](), collection.mutable.ListBuffer[Any]())
val res0: List[scala.collection.AbstractSeq[Any] with scala.collection.StrictOptimizedSeqOps[Any,[_]scala.collection.AbstractSeq[_] with scala.collection.StrictOptimizedSeqOps[_,[_]scala.collection.AbstractSeq[_] with scala.collection.StrictOptimizedSeqOps[_,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[_] with scala.collection.StrictOptimizedSeqOps[_,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable{def iterableFactory: scala.collection.SeqFactory[[A]scala.collection.AbstractSeq[A] with scala.collection.generic.DefaultSerializable]},scala.collection.AbstractSeq[Any] with scala.collection.StrictOptimizedSeqOps[Any,[_]scala.collection.AbstractSeq[_] with scala.collection.StrictOptimizedSeqOps[_,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[Any] with scala.collection.StrictOptimizedSeqOps[Any,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[Any] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable{def iterableFactory: scala.collection.SeqFactory[[A]scala.collection.AbstractSeq[A] with scala.collection.generic.DefaultSerializable]}] with scala.collection.generic.DefaultSerializable{def iterableFactory: scala.collection.SeqFactory[[A]scala.collection.AbstractSeq[A] with scala.collection.StrictOptimizedSeqOps[A,[_]scala.collection.AbstractSeq[_] with scala.collection.generic.DefaultSerializable,scala.collection.AbstractSeq[A] with scala.collection.generic.DefaultSerializable] with scala.collection.generic.DefaultSerializable]}] = List(List(), ListBuffer())
Expand Down
8 changes: 4 additions & 4 deletions test/files/run/macro-bundle-repl.check
Expand Up @@ -6,16 +6,16 @@ scala> import scala.reflect.macros.blackbox.Context
import scala.reflect.macros.blackbox.Context

scala> class Bar(val c: Context) { def impl = { import c.universe._; c.Expr[Unit](q"()") } };def bar: Unit = macro Bar.impl
// defined class Bar
// defined term macro bar: Unit
class Bar
def bar: Unit

scala> bar

scala> class Foo(val c: Context) { def impl = { import c.universe._; c.Expr[Unit](q"()") } }
// defined class Foo
class Foo

scala> def foo: Unit = macro Foo.impl
// defined term macro foo: Unit
def foo: Unit

scala> foo

Expand Down
6 changes: 3 additions & 3 deletions test/files/run/macro-repl-basic.check
Expand Up @@ -26,7 +26,7 @@ scala> object Impls {
c.Expr[Int](body)
}
}
// defined object Impls
object Impls

scala> object Macros {
object Shmacros {
Expand All @@ -36,8 +36,8 @@ scala> object Macros {
}; class Macros {
def quux(x: Int): Int = macro Impls.quux
}
// defined object Macros
// defined class Macros
object Macros
class Macros

scala>

Expand Down
8 changes: 4 additions & 4 deletions test/files/run/macro-repl-dontexpand.check
@@ -1,14 +1,14 @@

scala> def bar1(c: scala.reflect.macros.blackbox.Context) = ???
def bar1(c: scala.reflect.macros.blackbox.Context): Nothing = <method>
def bar1(c: scala.reflect.macros.blackbox.Context): Nothing

scala> def foo1: Nothing = macro bar1
// defined term macro foo1: Nothing
def foo1: Nothing

scala> def bar2(c: scala.reflect.macros.whitebox.Context) = ???
def bar2(c: scala.reflect.macros.whitebox.Context): Nothing = <method>
def bar2(c: scala.reflect.macros.whitebox.Context): Nothing

scala> def foo2: Nothing = macro bar2
// defined term macro foo2: Nothing
def foo2: Nothing

scala> :quit
6 changes: 3 additions & 3 deletions test/files/run/macro-system-properties.check
Expand Up @@ -9,12 +9,12 @@ scala> object GrabContext {
def impl(c: Context)() = { import c.universe._; System.getProperties.put("lastContext", c); c.Expr[Unit](q"()") }
def grab(): Unit = macro impl
}
// defined object GrabContext
object GrabContext

scala> object Test { class C(implicit a: Any) { GrabContext.grab } }
// defined object Test
object Test

scala> object Test { class C(implicit a: Any) { GrabContext.grab } }
// defined object Test
object Test

scala> :quit
4 changes: 2 additions & 2 deletions test/files/run/reflection-equality.check
Expand Up @@ -2,7 +2,7 @@
scala> class X {
def methodIntIntInt(x: Int, y: Int) = x+y
}
// defined class X
class X

scala>

Expand All @@ -13,7 +13,7 @@ scala> import scala.reflect.runtime.{ currentMirror => cm }
import scala.reflect.runtime.{currentMirror=>cm}

scala> def im: InstanceMirror = cm.reflect(new X)
def im: reflect.runtime.universe.InstanceMirror = <method>
def im: reflect.runtime.universe.InstanceMirror

scala> val cs: ClassSymbol = im.symbol
val cs: reflect.runtime.universe.ClassSymbol = class X
Expand Down

0 comments on commit 71823e9

Please sign in to comment.