Skip to content

Fix MatchError when printing match types from macros #13278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ object Extractors {
this += "TypeBounds(" += lo += ", " += hi += ")"
case NoPrefix() =>
this += "NoPrefix()"
case MatchCase(pat, rhs) =>
this += "MatchCase(" += pat += ", " += rhs += ")"
}

def visitSignature(sig: Signature): this.type = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,12 @@ object SourceCode {
this += " <: "
printType(hi)

case MatchCase(pat, rhs) =>
this += "case "
printType(pat)
this += " => "
printType(rhs)

case _ =>
throw new MatchError(tpe.show(using Printer.TypeReprStructure))
}
Expand Down
10 changes: 8 additions & 2 deletions tests/run-macros/type-show/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import scala.quoted.*

object TypeToolbox {
inline def show[A]: String = ${ showImpl[A] }
private def showImpl[A: Type](using Quotes) : Expr[String] =
inline def show[A <: AnyKind]: String = ${ showImpl[A] }
private def showImpl[A <: AnyKind: Type](using Quotes) : Expr[String] =
Expr(Type.show[A])

inline def showStructure[A <: AnyKind]: String = ${ showStructureImpl[A] }
private def showStructureImpl[A <: AnyKind](using q: Quotes, a: Type[A]) : Expr[String] = {
import q.reflect._
Expr(TypeRepr.of[A].show(using Printer.TypeReprStructure))
}
}
5 changes: 5 additions & 0 deletions tests/run-macros/type-show/Test_2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ object Test {
assert(show[Int] == "scala.Int")
assert(show[Int => Int] == "scala.Function1[scala.Int, scala.Int]")
assert(show[(Int, String)] == "scala.Tuple2[scala.Int, scala.Predef.String]")
assert(show[[X] =>> X match { case Int => Int }] ==
"""[X >: scala.Nothing <: scala.Any] => X match {
| case scala.Int => scala.Int
|}""".stripMargin)
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")))))""")

// TODO: more complex types:
// - implicit function types
Expand Down