Skip to content

Commit

Permalink
Fix pretty printer to handle using and erased modifier
Browse files Browse the repository at this point in the history
[Cherry-picked 056fbc5][modified]
  • Loading branch information
Kordyjan committed Dec 7, 2023
1 parent 4ca3bf9 commit 74bdf0f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
47 changes: 29 additions & 18 deletions compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ object SourceCode {
for paramClause <- paramss do
paramClause match
case TermParamClause(params) =>
printArgsDefs(params)
printMethdArgsDefs(params)
case TypeParamClause(params) =>
printTargsDefs(stats.collect { case targ: TypeDef => targ }.filter(_.symbol.isTypeParam).zip(params))
}
Expand Down Expand Up @@ -313,7 +313,7 @@ object SourceCode {
this += highlightKeyword("def ") += highlightValDef(name1)
for clause <- paramss do
clause match
case TermParamClause(params) => printArgsDefs(params)
case TermParamClause(params) => printMethdArgsDefs(params)
case TypeParamClause(params) => printTargsDefs(params.zip(params))
if (!isConstructor) {
this += ": "
Expand Down Expand Up @@ -460,7 +460,7 @@ object SourceCode {

case tree @ Lambda(params, body) => // must come before `Block`
inParens {
printArgsDefs(params)
printLambdaArgsDefs(params)
this += (if tree.tpe.isContextFunctionType then " ?=> " else " => ")
printTree(body)
}
Expand Down Expand Up @@ -804,29 +804,37 @@ object SourceCode {
}
}

private def printArgsDefs(args: List[ValDef])(using elideThis: Option[Symbol]): Unit = {
private def printSeparatedParamDefs(list: List[ValDef])(using elideThis: Option[Symbol]): Unit = list match {
case Nil =>
case x :: Nil => printParamDef(x)
case x :: xs =>
printParamDef(x)
this += ", "
printSeparatedParamDefs(xs)
}

private def printMethdArgsDefs(args: List[ValDef])(using elideThis: Option[Symbol]): Unit = {
val argFlags = args match {
case Nil => Flags.EmptyFlags
case arg :: _ => arg.symbol.flags
}
if (argFlags.is(Flags.Erased | Flags.Given)) {
if (argFlags.is(Flags.Given)) this += " given"
if (argFlags.is(Flags.Erased)) this += " erased"
this += " "
}
inParens {
if (argFlags.is(Flags.Implicit) && !argFlags.is(Flags.Given)) this += "implicit "
if (argFlags.is(Flags.Given)) this += "using "

def printSeparated(list: List[ValDef]): Unit = list match {
case Nil =>
case x :: Nil => printParamDef(x)
case x :: xs =>
printParamDef(x)
this += ", "
printSeparated(xs)
}
printSeparatedParamDefs(args)
}
}

private def printLambdaArgsDefs(args: List[ValDef])(using elideThis: Option[Symbol]): Unit = {
val argFlags = args match {
case Nil => Flags.EmptyFlags
case arg :: _ => arg.symbol.flags
}
inParens {
if (argFlags.is(Flags.Implicit) && !argFlags.is(Flags.Given)) this += "implicit "

printSeparated(args)
printSeparatedParamDefs(args)
}
}

Expand All @@ -846,6 +854,9 @@ object SourceCode {
private def printParamDef(arg: ValDef)(using elideThis: Option[Symbol]): Unit = {
val name = splicedName(arg.symbol).getOrElse(arg.symbol.name)
val sym = arg.symbol.owner

if (arg.symbol.flags.is(Flags.Erased)) this += "erased "

if sym.isDefDef && sym.name == "<init>" then
val ClassDef(_, _, _, _, body) = sym.owner.tree: @unchecked
body.collectFirst {
Expand Down
21 changes: 21 additions & 0 deletions tests/run-macros/term-show.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
class C() {
def a: scala.Int = 0
private[this] def b: scala.Int = 0
private[this] def c: scala.Int = 0
private[C] def d: scala.Int = 0
protected def e: scala.Int = 0
protected[this] def f: scala.Int = 0
protected[C] def g: scala.Int = 0
}
()
}
@scala.annotation.internal.SourceFile("tests/run-macros/term-show/Test_2.scala") trait A() extends java.lang.Object {
def imp(x: scala.Int)(implicit str: scala.Predef.String): scala.Int
def use(`x₂`: scala.Int)(using `str₂`: scala.Predef.String): scala.Int
def era(`x₃`: scala.Int)(erased `str₃`: scala.Predef.String): scala.Int
def f1(x1: scala.Int, erased x2: scala.Int): scala.Int
def f2(erased `x1₂`: scala.Int, erased `x2₂`: scala.Int): scala.Int
def f3(using `x1₃`: scala.Int, erased `x2₃`: scala.Int): scala.Int
def f4(using erased `x1₄`: scala.Int, erased `x2₄`: scala.Int): scala.Int
}

0 comments on commit 74bdf0f

Please sign in to comment.