Skip to content

Commit

Permalink
Improve message when tree cannot be shown as source (#19906)
Browse files Browse the repository at this point in the history
Closes #19905
  • Loading branch information
nicolasstucki authored Mar 11, 2024
2 parents 9e608ce + cb253f8 commit abe7b0f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
17 changes: 10 additions & 7 deletions compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ object SourceCode {
case Select(newTree: New, _) =>
printType(newTree.tpe)(using Some(cdef.symbol))
case parent: Term =>
throw new MatchError(parent.show(using Printer.TreeStructure))
cannotBeShownAsSource(parent.show(using Printer.TreeStructure))
}

def printSeparated(list: List[Tree /* Term | TypeTree */]): Unit = list match {
Expand Down Expand Up @@ -536,7 +536,7 @@ object SourceCode {
printCaseDef(tree)

case _ =>
throw new MatchError(tree.show(using Printer.TreeStructure))
cannotBeShownAsSource(tree.show(using Printer.TreeStructure))

}

Expand Down Expand Up @@ -934,7 +934,7 @@ object SourceCode {
case Ident("unapply" | "unapplySeq") =>
this += fun.symbol.owner.fullName.stripSuffix("$")
case _ =>
throw new MatchError(fun.show(using Printer.TreeStructure))
cannotBeShownAsSource(fun.show(using Printer.TreeStructure))
}
inParens(printPatterns(patterns, ", "))

Expand All @@ -953,7 +953,7 @@ object SourceCode {
printTree(v)

case _ =>
throw new MatchError(pattern.show(using Printer.TreeStructure))
cannotBeShownAsSource(pattern.show(using Printer.TreeStructure))

}

Expand Down Expand Up @@ -1079,7 +1079,7 @@ object SourceCode {
printTypeTree(tpt)

case _ =>
throw new MatchError(tree.show(using Printer.TreeStructure))
cannotBeShownAsSource(tree.show(using Printer.TreeStructure))

}

Expand Down Expand Up @@ -1248,7 +1248,7 @@ object SourceCode {
printType(rhs)

case _ =>
throw new MatchError(tpe.show(using Printer.TypeReprStructure))
cannotBeShownAsSource(tpe.show(using Printer.TypeReprStructure))
}

private def printSelector(sel: Selector): this.type = sel match {
Expand Down Expand Up @@ -1287,7 +1287,7 @@ object SourceCode {
val sym = annot.tpe.typeSymbol
sym != Symbol.requiredClass("scala.forceInline") &&
sym.maybeOwner != Symbol.requiredPackage("scala.annotation.internal")
case x => throw new MatchError(x.show(using Printer.TreeStructure))
case x => cannotBeShownAsSource(x.show(using Printer.TreeStructure))
}
printAnnotations(annots)
if (annots.nonEmpty) this += " "
Expand Down Expand Up @@ -1458,6 +1458,9 @@ object SourceCode {
}
}

private def cannotBeShownAsSource(x: String): Nothing =
throw new Exception(s"$x does not have a source representation")

private object SpecialOp {
def unapply(arg: Tree): Option[(String, List[Term])] = arg match {
case arg @ Apply(fn, args) =>
Expand Down
3 changes: 3 additions & 0 deletions tests/run-macros/i19905.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
java.lang.Exception: NoPrefix() does not have a source representation
java.lang.Exception: NoPrefix() does not have a source representation
NoPrefix()
31 changes: 31 additions & 0 deletions tests/run-macros/i19905/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import scala.quoted.*

inline def noPrefixShortCode: String =
${ noPrefixShortCodeImpl }

inline def noPrefixCode: String =
${ noPrefixCodeImpl }

inline def noPrefixStructure: String =
${ noPrefixStructure }

def noPrefix(using Quotes): quotes.reflect.TypeRepr =
import quotes.reflect.*
TypeRepr.of[Unit] match
case TypeRef(ThisType(TypeRef(prefix, _)), _) => prefix

def noPrefixShortCodeImpl(using Quotes): Expr[String] =
import quotes.reflect.*
try Expr(Printer.TypeReprShortCode.show(noPrefix))
catch case ex: Exception =>
Expr(s"${ex.getClass.getName}: ${ex.getMessage}")

def noPrefixCodeImpl(using Quotes): Expr[String] =
import quotes.reflect.*
try Expr(Printer.TypeReprCode.show(noPrefix))
catch case ex: Exception =>
Expr(s"${ex.getClass.getName}: ${ex.getMessage}")

def noPrefixStructure(using Quotes): Expr[String] =
import quotes.reflect.*
Expr(Printer.TypeReprStructure.show(noPrefix))
5 changes: 5 additions & 0 deletions tests/run-macros/i19905/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@main
def Test: Unit =
println(noPrefixShortCode)
println(noPrefixCode)
println(noPrefixStructure)

0 comments on commit abe7b0f

Please sign in to comment.