Skip to content

Commit 31292be

Browse files
authored
Merge pull request #13278 from jeremyrsmith/fix-match-type-printing
Fix MatchError when printing match types from macros
2 parents 7ccdfbc + 64c7ca5 commit 31292be

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ object Extractors {
232232
this += "TypeBounds(" += lo += ", " += hi += ")"
233233
case NoPrefix() =>
234234
this += "NoPrefix()"
235+
case MatchCase(pat, rhs) =>
236+
this += "MatchCase(" += pat += ", " += rhs += ")"
235237
}
236238

237239
def visitSignature(sig: Signature): this.type = {

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

+6
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,12 @@ object SourceCode {
12241224
this += " <: "
12251225
printType(hi)
12261226

1227+
case MatchCase(pat, rhs) =>
1228+
this += "case "
1229+
printType(pat)
1230+
this += " => "
1231+
printType(rhs)
1232+
12271233
case _ =>
12281234
throw new MatchError(tpe.show(using Printer.TypeReprStructure))
12291235
}
+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import scala.quoted.*
22

33
object TypeToolbox {
4-
inline def show[A]: String = ${ showImpl[A] }
5-
private def showImpl[A: Type](using Quotes) : Expr[String] =
4+
inline def show[A <: AnyKind]: String = ${ showImpl[A] }
5+
private def showImpl[A <: AnyKind: Type](using Quotes) : Expr[String] =
66
Expr(Type.show[A])
7+
8+
inline def showStructure[A <: AnyKind]: String = ${ showStructureImpl[A] }
9+
private def showStructureImpl[A <: AnyKind](using q: Quotes, a: Type[A]) : Expr[String] = {
10+
import q.reflect._
11+
Expr(TypeRepr.of[A].show(using Printer.TypeReprStructure))
12+
}
713
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ object Test {
88
assert(show[Int] == "scala.Int")
99
assert(show[Int => Int] == "scala.Function1[scala.Int, scala.Int]")
1010
assert(show[(Int, String)] == "scala.Tuple2[scala.Int, scala.Predef.String]")
11+
assert(show[[X] =>> X match { case Int => Int }] ==
12+
"""[X >: scala.Nothing <: scala.Any] => X match {
13+
| case scala.Int => scala.Int
14+
|}""".stripMargin)
15+
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")))))""")
1116

1217
// TODO: more complex types:
1318
// - implicit function types

0 commit comments

Comments
 (0)