diff --git a/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala b/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala index a6a773adc9ba..9ec4e626b597 100644 --- a/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala +++ b/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala @@ -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)) } @@ -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 += ": " @@ -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) } @@ -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) } } @@ -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 == "" then val ClassDef(_, _, _, _, body) = sym.owner.tree: @unchecked body.collectFirst { diff --git a/tests/run-macros/term-show.check b/tests/run-macros/term-show.check new file mode 100644 index 000000000000..91ba0308e3db --- /dev/null +++ b/tests/run-macros/term-show.check @@ -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 +}