diff --git a/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala b/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala index b27016045051..9aec7fc17ed7 100644 --- a/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala +++ b/compiler/src/scala/quoted/runtime/impl/printers/SourceCode.scala @@ -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 { @@ -536,7 +536,7 @@ object SourceCode { printCaseDef(tree) case _ => - throw new MatchError(tree.show(using Printer.TreeStructure)) + cannotBeShownAsSource(tree.show(using Printer.TreeStructure)) } @@ -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, ", ")) @@ -953,7 +953,7 @@ object SourceCode { printTree(v) case _ => - throw new MatchError(pattern.show(using Printer.TreeStructure)) + cannotBeShownAsSource(pattern.show(using Printer.TreeStructure)) } @@ -1079,7 +1079,7 @@ object SourceCode { printTypeTree(tpt) case _ => - throw new MatchError(tree.show(using Printer.TreeStructure)) + cannotBeShownAsSource(tree.show(using Printer.TreeStructure)) } @@ -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 { @@ -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 += " " @@ -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) => diff --git a/tests/run-macros/i19905.check b/tests/run-macros/i19905.check new file mode 100644 index 000000000000..36ba7772bfdb --- /dev/null +++ b/tests/run-macros/i19905.check @@ -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() diff --git a/tests/run-macros/i19905/Macro_1.scala b/tests/run-macros/i19905/Macro_1.scala new file mode 100644 index 000000000000..dc2351f32864 --- /dev/null +++ b/tests/run-macros/i19905/Macro_1.scala @@ -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)) diff --git a/tests/run-macros/i19905/Test_2.scala b/tests/run-macros/i19905/Test_2.scala new file mode 100644 index 000000000000..9363beb64e6d --- /dev/null +++ b/tests/run-macros/i19905/Test_2.scala @@ -0,0 +1,5 @@ +@main +def Test: Unit = + println(noPrefixShortCode) + println(noPrefixCode) + println(noPrefixStructure)