Skip to content
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
18 changes: 15 additions & 3 deletions compiler/src/scala/quoted/runtime/impl/printers/Extractors.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scala.quoted
package runtime.impl.printers

import scala.collection.mutable
import scala.quoted.*

object Extractors {
Expand Down Expand Up @@ -67,6 +68,12 @@ object Extractors {
import quotes.reflect.*

private val sb: StringBuilder = new StringBuilder
private var recTypeCounter = 0
private val recTypeIds = mutable.Map.empty[RecursiveType, Int]

private def nextRecTypeId(): Int =
recTypeCounter += 1
recTypeCounter

def result(): String = sb.result()

Expand Down Expand Up @@ -226,9 +233,14 @@ object Extractors {
case SuperType(thistpe, supertpe) =>
this += "SuperType(" += thistpe += ", " += supertpe += ")"
case RecursiveThis(binder) =>
this += "RecursiveThis(" += binder += ")"
case RecursiveType(underlying) =>
this += "RecursiveType(" += underlying += ")"
val id = recTypeIds.getOrElse(binder, -1)
if (id == -1)
this += "RecursiveThis(" += binder += ")"
else
this += "RecursiveThis(<rec" += id += ">)"
case rt @ RecursiveType(underlying) =>
val id = recTypeIds.getOrElseUpdate(rt, nextRecTypeId())
this += "RecursiveType(rec" += id += " => " += underlying += ")"
case MethodType(argNames, argTypes, resType) =>
this += "MethodType(" ++= argNames += ", " ++= argTypes += ", " += resType += ")"
case PolyType(argNames, argBounds, resType) =>
Expand Down
9 changes: 9 additions & 0 deletions tests/run/i22649/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.quoted.*

inline def structureOf[T]: String =
${ structureOfImpl[T] }

private def structureOfImpl[T](using Quotes, Type[T]): Expr[String] =
import quotes.reflect.*
val str = Printer.TypeReprStructure.show(TypeRepr.of[T])
Expr(str)
11 changes: 11 additions & 0 deletions tests/run/i22649/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@main def Test =
val str =
structureOf[{
type T
def make: T
}]

assert(
str == """RecursiveType(rec1 => Refinement(Refinement(TypeRef(ThisType(TypeRef(NoPrefix(), "lang")), "Object"), "T", TypeBounds(TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Nothing"), TypeRef(ThisType(TypeRef(NoPrefix(), "scala")), "Any"))), "make", ByNameType(TypeRef(RecursiveThis(<rec1>), "T"))))""",
s"Was $str"
)