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

Fix #16615 - crashes of path dependent types in spliced Type.of #16773

Merged
merged 2 commits into from
Feb 9, 2023
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
48 changes: 42 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/Splicing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,16 @@ class Splicing extends MacroTransform:
else args.mapConserve(arg => transformLevel0QuoteContent(arg)(using quoteContext))
}
cpy.Apply(tree)(cpy.Select(sel)(cpy.Apply(app)(fn, newArgs), nme.apply), quotesArgs)
case Apply(TypeApply(_, List(tpt)), List(quotes))
case Apply(TypeApply(typeof, List(tpt)), List(quotes))
if tree.symbol == defn.QuotedTypeModule_of && containsCapturedType(tpt.tpe) =>
ref(capturedType(tpt))(using ctx.withSource(tree.source)).withSpan(tree.span)
val newContent = capturedPartTypes(tpt)
newContent match
case block: Block =>
inContext(ctx.withSource(tree.source)) {
Apply(TypeApply(typeof, List(newContent)), List(quotes)).withSpan(tree.span)
}
case _ =>
ref(capturedType(newContent))(using ctx.withSource(tree.source)).withSpan(tree.span)
case CapturedApplication(fn, argss) =>
transformCapturedApplication(tree, fn, argss)
case _ =>
Expand Down Expand Up @@ -335,17 +342,46 @@ class Splicing extends MacroTransform:
val bindingSym = refBindingMap.getOrElseUpdate(tree.symbol, (tree, newBinding))._2
ref(bindingSym)

private def capturedType(tree: Tree)(using Context): Symbol =
val tpe = tree.tpe.widenTermRefExpr
def newBinding = newSymbol(
private def newQuotedTypeClassBinding(tpe: Type)(using Context) =
newSymbol(
spliceOwner,
UniqueName.fresh(nme.Type).toTermName,
Param,
defn.QuotedTypeClass.typeRef.appliedTo(tpe),
)
val bindingSym = refBindingMap.getOrElseUpdate(tree.symbol, (TypeTree(tree.tpe), newBinding))._2

private def capturedType(tree: Tree)(using Context): Symbol =
val tpe = tree.tpe.widenTermRefExpr
val bindingSym = refBindingMap
.getOrElseUpdate(tree.symbol, (TypeTree(tree.tpe), newQuotedTypeClassBinding(tpe)))._2
bindingSym

private def capturedPartTypes(tpt: Tree)(using Context): Tree =
val old = healedTypes
healedTypes = PCPCheckAndHeal.QuoteTypeTags(tpt.span)
val capturePartTypes = new TypeMap {
def apply(tp: Type) = tp match {
case typeRef @ TypeRef(prefix, _) if isCaptured(prefix.typeSymbol) || isCaptured(prefix.termSymbol) =>
val termRef = refBindingMap
.getOrElseUpdate(typeRef.symbol, (TypeTree(typeRef), newQuotedTypeClassBinding(typeRef)))._2.termRef
val tagRef = healedTypes.nn.getTagRef(termRef)
tagRef
case _ =>
mapOver(tp)
}
}
val captured = capturePartTypes(tpt.tpe.widenTermRefExpr)
val newHealedTypes = healedTypes.nn.getTypeTags
healedTypes = old
tpt match
case block: Block =>
cpy.Block(block)(newHealedTypes ::: block.stats, TypeTree(captured))
case _ =>
if newHealedTypes.nonEmpty then
cpy.Block(tpt)(newHealedTypes, TypeTree(captured))
else
tpt

private def getTagRefFor(tree: Tree)(using Context): Tree =
val capturedTypeSym = capturedType(tree)
TypeTree(healedTypes.nn.getTagRef(capturedTypeSym.termRef))
Expand Down
19 changes: 19 additions & 0 deletions tests/pos-macros/i16615.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import scala.quoted.*

trait Api:
type Reader[E]

def bugImpl[T: Type, Q[_]: Type](using Quotes) =
'{
val p: Api = ???
${
Type.of[p.Reader[T]]
Type.of[Q[p.Reader[T]]]
Type.of[p.Reader[Q[p.Reader[T]]]]
nicolasstucki marked this conversation as resolved.
Show resolved Hide resolved
Type.of[List[p.Reader[T]]]
Type.of[p.Reader[List[p.Reader[T]]]]
Type.of[p.Reader[List[T]]]
Type.of[p.Reader[Q[T]]]
Expr(1)
}
}