Skip to content

Avoid recursion when transforming types used in Holes #23595

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
39 changes: 38 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/PickleQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,47 @@ class PickleQuotes extends MacroTransform {
ctx.typeAssigner.assignType(untpd.TypeDef(local.name, hole), local).withSpan(typeArg.span)
}

/** Avoid all non-static types except those defined in the quote. */
/** Avoid all non-static types except those defined in the quote.
* Also avoid recursive LazyRef references that can be generated by TypeBounds.
*/
private def getPicklableHoleType(tpe: Type, isStagedClasses: Symbol => Boolean)(using Context) =
new TypeOps.AvoidMap {
val processedTypeRefs = util.HashSet[Type]()
def toAvoid(tp: NamedType) = !isStagedClasses(tp.typeSymbol) && !isStaticPrefix(tp)
override def apply(tp: Type) = tp match
case typeRef: TypeRef if toAvoid(typeRef) =>
if processedTypeRefs.contains(typeRef) then typeRef
else
typeRef.info match
case TypeBounds(lo, hi) =>
val typeRefRemapper = (recType: RecType) => new TypeMap {
def apply(t: Type) = t match
case lr: LazyRef if lr.ref == typeRef => recType.recThis
case tr: TypeRef if tr == typeRef => recType.recThis
case lr: LazyRef => lr
case _ => mapOver(t)
}
processedTypeRefs.add(typeRef)
val newLo =
RecType.closeOver(typeRefRemapper(_).apply(atVariance(-variance)(apply(lo))))
val newHi =
apply(hi) match
case Range(l, h) =>
Range(
RecType.closeOver(typeRefRemapper(_).apply(l)),
RecType.closeOver(typeRefRemapper(_).apply(h))
)
case other =>
RecType.closeOver(typeRefRemapper(_).apply(other))
processedTypeRefs.remove(typeRef)
range(newLo, newHi)
case _ =>
super.apply(tp)
case lazyRef: LazyRef =>
if processedTypeRefs.contains(lazyRef.ref) then lazyRef
else super.apply(lazyRef)
case _ =>
super.apply(tp)
}.apply(tpe)
}

Expand Down
14 changes: 14 additions & 0 deletions tests/pos/i22996.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import scala.quoted.*

trait Appendable[T]
trait Format[T] { def empty: T }

object FormatMacros {

def test[T <: Appendable[T]: Type, F <: Format[T]: Type](f: Expr[F])(using Quotes): Expr[T] =
'{ $f.empty }

def test2[T <: Appendable[T2]: Type, T2 <: Appendable[T], F <: Format[T]: Type](f: Expr[F])(using Quotes): Expr[T] =
'{ $f.empty }

}
Loading