Skip to content

Commit 07723d7

Browse files
Fix reflect SuperType.supertpe and printing super types and type lambdas (#16968)
Fixes reflect `SuperType.supertpe` to return `supertpe` instead of `thistpe`. Fixes the printer for supertypes. Maybe this could be improved as it currently ignores the `supertpe` field when printing. But it's aligned with [PlainPrinter](https://github.com/lampepfl/dotty/blob/3.3.0-RC3/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala#L358). Fixes the printer to print type lambdas as `=>>` instead of `=>`. Fixes printing private and protected modifiers (before, `private` was only printed when scoped to an enclosing type definition).
2 parents 8e97a51 + 8aba6f4 commit 07723d7

File tree

10 files changed

+64
-31
lines changed

10 files changed

+64
-31
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
18841884
given SuperTypeMethods: SuperTypeMethods with
18851885
extension (self: SuperType)
18861886
def thistpe: TypeRepr = self.thistpe
1887-
def supertpe: TypeRepr = self.thistpe
1887+
def supertpe: TypeRepr = self.supertpe
18881888
end extension
18891889
end SuperTypeMethods
18901890

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

+18-22
Original file line numberDiff line numberDiff line change
@@ -1189,12 +1189,12 @@ object SourceCode {
11891189
}
11901190

11911191
case SuperType(thistpe, supertpe) =>
1192-
printType(supertpe)
1192+
printType(thistpe)
11931193
this += highlightTypeDef(".super")
11941194

11951195
case TypeLambda(paramNames, tparams, body) =>
11961196
inSquare(printMethodicTypeParams(paramNames, tparams))
1197-
this += highlightTypeDef(" => ")
1197+
this += highlightTypeDef(" =>> ")
11981198
printType(body)
11991199

12001200
case ParamRef(lambda, idx) =>
@@ -1372,28 +1372,24 @@ object SourceCode {
13721372

13731373
private def printProtectedOrPrivate(definition: Definition): Boolean = {
13741374
var prefixWasPrinted = false
1375-
def printWithin(within: TypeRepr) = within match {
1376-
case TypeRef(_, name) => this += name
1377-
case _ => printFullClassName(within)
1378-
}
1379-
if (definition.symbol.flags.is(Flags.Protected)) {
1375+
def printWithin(within: Option[TypeRepr]) = within match
1376+
case _ if definition.symbol.flags.is(Flags.Local) => inSquare(this += "this")
1377+
case Some(TypeRef(_, name)) => inSquare(this += name)
1378+
case Some(within) => inSquare(printFullClassName(within))
1379+
case _ =>
1380+
1381+
if definition.symbol.flags.is(Flags.Protected) then
13801382
this += highlightKeyword("protected")
1381-
definition.symbol.protectedWithin match {
1382-
case Some(within) =>
1383-
inSquare(printWithin(within))
1384-
case _ =>
1385-
}
1383+
printWithin(definition.symbol.protectedWithin)
13861384
prefixWasPrinted = true
1387-
} else {
1388-
definition.symbol.privateWithin match {
1389-
case Some(within) =>
1390-
this += highlightKeyword("private")
1391-
inSquare(printWithin(within))
1392-
prefixWasPrinted = true
1393-
case _ =>
1394-
}
1395-
}
1396-
if (prefixWasPrinted)
1385+
else
1386+
val privateWithin = definition.symbol.privateWithin
1387+
if privateWithin.isDefined || definition.symbol.flags.is(Flags.Private) then
1388+
this += highlightKeyword("private")
1389+
printWithin(definition.symbol.privateWithin)
1390+
prefixWasPrinted = true
1391+
1392+
if prefixWasPrinted then
13971393
this += " "
13981394
prefixWasPrinted
13991395
}

tests/pos-macros/hk-quoted-type-patterns/Macro_1.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ private def impl(x: Expr[Any])(using Quotes): Expr[Unit] = {
55
case '{ foo[x] } =>
66
assert(Type.show[x] == "scala.Int", Type.show[x])
77
case '{ type f[X]; foo[`f`] } =>
8-
assert(Type.show[f] == "[A >: scala.Nothing <: scala.Any] => scala.collection.immutable.List[A]", Type.show[f])
8+
assert(Type.show[f] == "[A >: scala.Nothing <: scala.Any] =>> scala.collection.immutable.List[A]", Type.show[f])
99
case '{ type f <: AnyKind; foo[`f`] } =>
10-
assert(Type.show[f] == "[K >: scala.Nothing <: scala.Any, V >: scala.Nothing <: scala.Any] => scala.collection.immutable.Map[K, V]", Type.show[f])
10+
assert(Type.show[f] == "[K >: scala.Nothing <: scala.Any, V >: scala.Nothing <: scala.Any] =>> scala.collection.immutable.Map[K, V]", Type.show[f])
1111
case x => throw MatchError(x.show)
1212
'{}
1313
}

tests/run-macros/i10863.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[A >: scala.Nothing <: scala.Any] => scala.collection.immutable.List[A]
1+
[A >: scala.Nothing <: scala.Any] =>> scala.collection.immutable.List[A]

tests/run-macros/i8514b.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
B
2-
A[[T >: scala.Nothing <: scala.Any] => P[T], scala.Predef.String]
2+
A[[T >: scala.Nothing <: scala.Any] =>> P[T], scala.Predef.String]
33
java.lang.Object
44
scala.Matchable
55
scala.Any
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Functor[scala.collection.immutable.List]
2-
Unapply[[F >: scala.Nothing <: [_$9 >: scala.Nothing <: scala.Any] => scala.Any] => Functor[F], Wrap[scala.Int]]
3-
Unapply[[F >: scala.Nothing <: [_$9 >: scala.Nothing <: scala.Any] => scala.Any] => Functor[F], Wrap[Dummy]]
2+
Unapply[[F >: scala.Nothing <: [_$9 >: scala.Nothing <: scala.Any] =>> scala.Any] =>> Functor[F], Wrap[scala.Int]]
3+
Unapply[[F >: scala.Nothing <: [_$9 >: scala.Nothing <: scala.Any] =>> scala.Any] =>> Functor[F], Wrap[Dummy]]
44
Functor[scala.Option]
+8
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+
}
+29
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+
}

tests/run-macros/type-show/Test_2.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Test {
99
assert(show[Int => Int] == "scala.Function1[scala.Int, scala.Int]")
1010
assert(show[(Int, String)] == "scala.Tuple2[scala.Int, scala.Predef.String]")
1111
assert(show[[X] =>> X match { case Int => Int }] ==
12-
"""[X >: scala.Nothing <: scala.Any] => X match {
12+
"""[X >: scala.Nothing <: scala.Any] =>> X match {
1313
| case scala.Int => scala.Int
1414
|}""".stripMargin)
1515
assert(showStructure[[X] =>> X match { case Int => Int }] == """TypeLambda(List(X), List(TypeBounds(TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Nothing"), TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Any"))), MatchType(TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Any"), ParamRef(binder, 0), List(MatchCase(TypeRef(TermRef(ThisType(TypeRef(NoPrefix(), "<root>")), "scala"), "Int"), TypeRef(TermRef(ThisType(TypeRef(NoPrefix(), "<root>")), "scala"), "Int")))))""")

tests/run-staging/i5965.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ List(1, 2, 3)
1111
}
1212
Some(4)
1313
{
14-
val y: [V >: scala.Nothing <: scala.Any] => scala.collection.immutable.Map[scala.Int, V][scala.Int] = scala.Predef.Map.apply[scala.Int, scala.Int](scala.Predef.ArrowAssoc[scala.Int](4).->[scala.Int](1))
14+
val y: [V >: scala.Nothing <: scala.Any] =>> scala.collection.immutable.Map[scala.Int, V][scala.Int] = scala.Predef.Map.apply[scala.Int, scala.Int](scala.Predef.ArrowAssoc[scala.Int](4).->[scala.Int](1))
1515

1616
(y: scala.collection.immutable.Map[scala.Int, scala.Int])
1717
}

0 commit comments

Comments
 (0)