Skip to content
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

Fix pretty printer to handle using and erased modifier #17952

Merged
merged 1 commit into from
Jul 28, 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 "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not print this if these are the parameters of a lambda. See tests/run-staging/quote-nested-1.check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not entirely sure to understand this one. Are you saying that if the arg flag includes given but the outer type is a lambda then the keyword using is invalid because it should be a contextual function type ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, you meant that the test case failed. I should have checked CI before asking the question above. Will take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0910940 is an attempt to ignore lambdas, although it feels very targeted to me. Let me know what you think of it.

Copy link
Contributor

@nicolasstucki nicolasstucki Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to split into printMethdArgsDefs and printLambdaArgsDefs as those are different in the grammar.

Copy link
Contributor Author

@fmonniot fmonniot Jul 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 34c7058. Let me know if that seems more readable to you or not.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 34c7058. Let me know if that seems more readable to you or not.

That seems fine to me, but I would deduplicate the two printSeparated local defs into one private def reused by both methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done via 25f78ea.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simplify this to

- if (argFlags.is(Flags.Implicit) && !argFlags.is(Flags.Given)) this += "implicit "
- if (argFlags.is(Flags.Given)) this += "using "
+ if argFlags.is(Flags.Given) then this += "using "
+ else if argFlags.is(Flags.Implicit) then this += "implicit "


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
}
7 changes: 7 additions & 0 deletions tests/run-macros/term-show/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ object TypeToolbox {
private def showImpl(using Quotes)(v: Expr[Any]): Expr[String] =
import quotes.reflect.*
Expr(v.show)

inline def showTree(inline className: String): String = ${ showTreeImpl('className) }
private def showTreeImpl(className: Expr[String])(using Quotes) : Expr[String] =
import quotes.reflect.*
val name = className.valueOrAbort
val res = Symbol.requiredClass(name).tree.show
Expr(res)
}
31 changes: 16 additions & 15 deletions tests/run-macros/term-show/Test_2.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import scala.language.experimental.erasedDefinitions

trait A:
def imp(x: Int)(implicit str: String): Int
def use(x: Int)(using str: String): Int
def era(x: Int)(erased str: String): Int
fmonniot marked this conversation as resolved.
Show resolved Hide resolved

def f1(x1: Int, erased x2: Int): Int
def f2(erased x1: Int, erased x2: Int): Int
def f3(using x1: Int, erased x2: Int): Int
def f4(using erased x1: Int, erased x2: Int): Int

object Test {
import TypeToolbox.*
def main(args: Array[String]): Unit = {
assert(show {
println(show {
class C {
def a = 0
private def b = 0
Expand All @@ -11,19 +23,8 @@ object Test {
protected[this] def f = 0
protected[C] def g = 0
}
}
==
"""{
| 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
| }
| ()
|}""".stripMargin)
})

println(showTree("A"))
}
}