Skip to content

Backport "Fix pretty printer to handle using and erased modifier" to LTS #19137

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 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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
}