Skip to content

Commit 1dfafc9

Browse files
smarterWojciechMazur
authored andcommitted
Quotes type printing: take infix type modifier into account
This is similar to how the regular compiler `.show` handles `infix` but using explicit parens everywhere to not have to reimplement the precedence logic (maybe quote type printing should just use `.show` eventually). [Cherry-picked 936c009]
1 parent c65573b commit 1dfafc9

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

Diff for: compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala

+13-2
Original file line numberDiff line numberDiff line change
@@ -1150,8 +1150,19 @@ object SourceCode {
11501150
case tp: TypeRef if tp.typeSymbol == Symbol.requiredClass("scala.<repeated>") =>
11511151
this += "_*"
11521152
case _ =>
1153-
printType(tp)
1154-
inSquare(printTypesOrBounds(args, ", "))
1153+
if !fullNames && args.lengthCompare(2) == 0 && tp.typeSymbol.flags.is(Flags.Infix) then
1154+
val lhs = args(0)
1155+
val rhs = args(1)
1156+
this += "("
1157+
printType(lhs)
1158+
this += " "
1159+
printType(tp)
1160+
this += " "
1161+
printType(rhs)
1162+
this += ")"
1163+
else
1164+
printType(tp)
1165+
inSquare(printTypesOrBounds(args, ", "))
11551166
}
11561167

11571168
case AnnotatedType(tp, annot) =>

Diff for: tests/run-macros/type-print.check

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
List[Int]
2+
scala.collection.immutable.List[scala.Int]
3+
scala.collection.immutable.List[scala.Int]
4+
AppliedType(TypeRef(ThisType(TypeRef(NoPrefix(), "immutable")), "List"), List(TypeRef(TermRef(ThisType(TypeRef(NoPrefix(), "<root>")), "scala"), "Int")))
5+
(3 + (a * b))
6+
scala.compiletime.ops.int.+[3, scala.compiletime.ops.int.*[a, b]]
7+
scala.compiletime.ops.int.+[3, scala.compiletime.ops.int.*[a, b]]
8+
AppliedType(TypeRef(TermRef(TermRef(TermRef(TermRef(ThisType(TypeRef(NoPrefix(), "<root>")), "scala"), "compiletime"), "ops"), "int"), "+"), List(ConstantType(IntConstant(3)), AppliedType(TypeRef(TermRef(TermRef(TermRef(TermRef(ThisType(TypeRef(NoPrefix(), "<root>")), "scala"), "compiletime"), "ops"), "int"), "*"), List(TermRef(NoPrefix(), "a"), TermRef(NoPrefix(), "b")))))
9+
((3 + a) * b)
10+
scala.compiletime.ops.int.*[scala.compiletime.ops.int.+[3, a], b]
11+
scala.compiletime.ops.int.*[scala.compiletime.ops.int.+[3, a], b]
12+
AppliedType(TypeRef(TermRef(TermRef(TermRef(TermRef(ThisType(TypeRef(NoPrefix(), "<root>")), "scala"), "compiletime"), "ops"), "int"), "*"), List(AppliedType(TypeRef(TermRef(TermRef(TermRef(TermRef(ThisType(TypeRef(NoPrefix(), "<root>")), "scala"), "compiletime"), "ops"), "int"), "+"), List(ConstantType(IntConstant(3)), TermRef(NoPrefix(), "a"))), TermRef(NoPrefix(), "b")))

Diff for: tests/run-macros/type-print/Macro_1.scala

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import scala.quoted.*
2+
3+
inline def printTypeShort[T]: String =
4+
${ printTypeShortImpl[T] }
5+
6+
inline def printType[T]: String =
7+
${ printTypeImpl[T] }
8+
9+
inline def printTypeAnsi[T]: String =
10+
${ printTypeAnsiImpl[T] }
11+
12+
inline def printTypeStructure[T]: String =
13+
${ printTypeStructureImpl[T] }
14+
15+
def printTypeShortImpl[T: Type](using Quotes): Expr[String] =
16+
import quotes.reflect.*
17+
Expr(Printer.TypeReprShortCode.show(TypeRepr.of[T]))
18+
19+
def printTypeImpl[T: Type](using Quotes): Expr[String] =
20+
import quotes.reflect.*
21+
Expr(Printer.TypeReprCode.show(TypeRepr.of[T]))
22+
23+
def printTypeAnsiImpl[T: Type](using Quotes): Expr[String] =
24+
import quotes.reflect.*
25+
Expr(Printer.TypeReprAnsiCode.show(TypeRepr.of[T]))
26+
27+
def printTypeStructureImpl[T: Type](using Quotes): Expr[String] =
28+
import quotes.reflect.*
29+
Expr(Printer.TypeReprStructure.show(TypeRepr.of[T]))

Diff for: tests/run-macros/type-print/Test_2.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.compiletime.ops.int.*
2+
3+
inline def printAll[T]: Unit =
4+
println(printTypeShort[T])
5+
println(printType[T])
6+
println(printTypeAnsi[T])
7+
println(printTypeStructure[T])
8+
9+
@main
10+
def Test: Unit =
11+
printAll[List[Int]]
12+
val a = 1
13+
val b = 2
14+
printAll[3 + a.type * b.type]
15+
printAll[(3 + a.type) * b.type]

0 commit comments

Comments
 (0)