Skip to content

Commit 8aba6f4

Browse files
committed
Fix printing private and protected modifiers
1 parent fbeeda3 commit 8aba6f4

File tree

3 files changed

+53
-20
lines changed

3 files changed

+53
-20
lines changed

compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,28 +1368,24 @@ object SourceCode {
13681368

13691369
private def printProtectedOrPrivate(definition: Definition): Boolean = {
13701370
var prefixWasPrinted = false
1371-
def printWithin(within: TypeRepr) = within match {
1372-
case TypeRef(_, name) => this += name
1373-
case _ => printFullClassName(within)
1374-
}
1375-
if (definition.symbol.flags.is(Flags.Protected)) {
1371+
def printWithin(within: Option[TypeRepr]) = within match
1372+
case _ if definition.symbol.flags.is(Flags.Local) => inSquare(this += "this")
1373+
case Some(TypeRef(_, name)) => inSquare(this += name)
1374+
case Some(within) => inSquare(printFullClassName(within))
1375+
case _ =>
1376+
1377+
if definition.symbol.flags.is(Flags.Protected) then
13761378
this += highlightKeyword("protected")
1377-
definition.symbol.protectedWithin match {
1378-
case Some(within) =>
1379-
inSquare(printWithin(within))
1380-
case _ =>
1381-
}
1379+
printWithin(definition.symbol.protectedWithin)
13821380
prefixWasPrinted = true
1383-
} else {
1384-
definition.symbol.privateWithin match {
1385-
case Some(within) =>
1386-
this += highlightKeyword("private")
1387-
inSquare(printWithin(within))
1388-
prefixWasPrinted = true
1389-
case _ =>
1390-
}
1391-
}
1392-
if (prefixWasPrinted)
1381+
else
1382+
val privateWithin = definition.symbol.privateWithin
1383+
if privateWithin.isDefined || definition.symbol.flags.is(Flags.Private) then
1384+
this += highlightKeyword("private")
1385+
printWithin(definition.symbol.privateWithin)
1386+
prefixWasPrinted = true
1387+
1388+
if prefixWasPrinted then
13931389
this += " "
13941390
prefixWasPrinted
13951391
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.quoted.*
2+
3+
object TypeToolbox {
4+
inline def show(inline v: Any): String = ${ showImpl('v) }
5+
private def showImpl(using Quotes)(v: Expr[Any]): Expr[String] =
6+
import quotes.reflect.*
7+
Expr(v.show)
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
object Test {
2+
import TypeToolbox.*
3+
def main(args: Array[String]): Unit = {
4+
assert(show {
5+
class C {
6+
def a = 0
7+
private def b = 0
8+
private[this] def c = 0
9+
private[C] def d = 0
10+
protected def e = 0
11+
protected[this] def f = 0
12+
protected[C] def g = 0
13+
}
14+
}
15+
==
16+
"""{
17+
| class C() {
18+
| def a: scala.Int = 0
19+
| private[this] def b: scala.Int = 0
20+
| private[this] def c: scala.Int = 0
21+
| private[C] def d: scala.Int = 0
22+
| protected def e: scala.Int = 0
23+
| protected[this] def f: scala.Int = 0
24+
| protected[C] def g: scala.Int = 0
25+
| }
26+
| ()
27+
|}""".stripMargin)
28+
}
29+
}

0 commit comments

Comments
 (0)