Skip to content
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

Improve message when tree cannot be shown as source #19906

Merged
merged 1 commit into from
Mar 11, 2024
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
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)
Loading