Skip to content

Commit 3f42535

Browse files
nicolasstuckiWojciechMazur
authored andcommitted
Improve message when tree cannot be shown as source
Closes #19905 [Cherry-picked cb253f8]
1 parent ffd8f5a commit 3f42535

File tree

4 files changed

+49
-7
lines changed

4 files changed

+49
-7
lines changed

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ object SourceCode {
188188
case Select(newTree: New, _) =>
189189
printType(newTree.tpe)(using Some(cdef.symbol))
190190
case parent: Term =>
191-
throw new MatchError(parent.show(using Printer.TreeStructure))
191+
cannotBeShownAsSource(parent.show(using Printer.TreeStructure))
192192
}
193193

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

538538
case _ =>
539-
throw new MatchError(tree.show(using Printer.TreeStructure))
539+
cannotBeShownAsSource(tree.show(using Printer.TreeStructure))
540540

541541
}
542542

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

@@ -953,7 +953,7 @@ object SourceCode {
953953
printTree(v)
954954

955955
case _ =>
956-
throw new MatchError(pattern.show(using Printer.TreeStructure))
956+
cannotBeShownAsSource(pattern.show(using Printer.TreeStructure))
957957

958958
}
959959

@@ -1079,7 +1079,7 @@ object SourceCode {
10791079
printTypeTree(tpt)
10801080

10811081
case _ =>
1082-
throw new MatchError(tree.show(using Printer.TreeStructure))
1082+
cannotBeShownAsSource(tree.show(using Printer.TreeStructure))
10831083

10841084
}
10851085

@@ -1248,7 +1248,7 @@ object SourceCode {
12481248
printType(rhs)
12491249

12501250
case _ =>
1251-
throw new MatchError(tpe.show(using Printer.TypeReprStructure))
1251+
cannotBeShownAsSource(tpe.show(using Printer.TypeReprStructure))
12521252
}
12531253

12541254
private def printSelector(sel: Selector): this.type = sel match {
@@ -1287,7 +1287,7 @@ object SourceCode {
12871287
val sym = annot.tpe.typeSymbol
12881288
sym != Symbol.requiredClass("scala.forceInline") &&
12891289
sym.maybeOwner != Symbol.requiredPackage("scala.annotation.internal")
1290-
case x => throw new MatchError(x.show(using Printer.TreeStructure))
1290+
case x => cannotBeShownAsSource(x.show(using Printer.TreeStructure))
12911291
}
12921292
printAnnotations(annots)
12931293
if (annots.nonEmpty) this += " "
@@ -1462,6 +1462,9 @@ object SourceCode {
14621462
}
14631463
}
14641464

1465+
private def cannotBeShownAsSource(x: String): Nothing =
1466+
throw new Exception(s"$x does not have a source representation")
1467+
14651468
private object SpecialOp {
14661469
def unapply(arg: Tree): Option[(String, List[Term])] = arg match {
14671470
case arg @ Apply(fn, args) =>

tests/run-macros/i19905.check

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
java.lang.Exception: NoPrefix() does not have a source representation
2+
java.lang.Exception: NoPrefix() does not have a source representation
3+
NoPrefix()

tests/run-macros/i19905/Macro_1.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import scala.quoted.*
2+
3+
inline def noPrefixShortCode: String =
4+
${ noPrefixShortCodeImpl }
5+
6+
inline def noPrefixCode: String =
7+
${ noPrefixCodeImpl }
8+
9+
inline def noPrefixStructure: String =
10+
${ noPrefixStructure }
11+
12+
def noPrefix(using Quotes): quotes.reflect.TypeRepr =
13+
import quotes.reflect.*
14+
TypeRepr.of[Unit] match
15+
case TypeRef(ThisType(TypeRef(prefix, _)), _) => prefix
16+
17+
def noPrefixShortCodeImpl(using Quotes): Expr[String] =
18+
import quotes.reflect.*
19+
try Expr(Printer.TypeReprShortCode.show(noPrefix))
20+
catch case ex: Exception =>
21+
Expr(s"${ex.getClass.getName}: ${ex.getMessage}")
22+
23+
def noPrefixCodeImpl(using Quotes): Expr[String] =
24+
import quotes.reflect.*
25+
try Expr(Printer.TypeReprCode.show(noPrefix))
26+
catch case ex: Exception =>
27+
Expr(s"${ex.getClass.getName}: ${ex.getMessage}")
28+
29+
def noPrefixStructure(using Quotes): Expr[String] =
30+
import quotes.reflect.*
31+
Expr(Printer.TypeReprStructure.show(noPrefix))

tests/run-macros/i19905/Test_2.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@main
2+
def Test: Unit =
3+
println(noPrefixShortCode)
4+
println(noPrefixCode)
5+
println(noPrefixStructure)

0 commit comments

Comments
 (0)