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

Optimize captured type in splice hole #17070

Merged
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
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/Splicing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class Splicing extends MacroTransform:
if tree.symbol == defn.QuotedTypeModule_of && containsCapturedType(tpt.tpe) =>
val newContent = capturedPartTypes(tpt)
newContent match
case block: Block =>
case block: Block =>
inContext(ctx.withSource(tree.source)) {
Apply(TypeApply(typeof, List(newContent)), List(quotes)).withSpan(tree.span)
}
Expand Down Expand Up @@ -342,7 +342,7 @@ class Splicing extends MacroTransform:
val bindingSym = refBindingMap.getOrElseUpdate(tree.symbol, (tree, newBinding))._2
ref(bindingSym)

private def newQuotedTypeClassBinding(tpe: Type)(using Context) =
private def newQuotedTypeClassBinding(tpe: Type)(using Context) =
newSymbol(
spliceOwner,
UniqueName.fresh(nme.Type).toTermName,
Expand All @@ -361,7 +361,7 @@ class Splicing extends MacroTransform:
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) =>
case typeRef: TypeRef if containsCapturedType(typeRef) =>
val termRef = refBindingMap
.getOrElseUpdate(typeRef.symbol, (TypeTree(typeRef), newQuotedTypeClassBinding(typeRef)))._2.termRef
val tagRef = healedTypes.nn.getTagRef(termRef)
Expand All @@ -376,7 +376,7 @@ class Splicing extends MacroTransform:
tpt match
case block: Block =>
cpy.Block(block)(newHealedTypes ::: block.stats, TypeTree(captured))
case _ =>
case _ =>
if newHealedTypes.nonEmpty then
cpy.Block(tpt)(newHealedTypes, TypeTree(captured))
else
Expand Down
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/TreeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,11 @@ object TreeChecker {
override def typedHole(tree: untpd.Hole, pt: Type)(using Context): Tree = {
val tree1 @ Hole(isTermHole, _, args, content, tpt) = super.typedHole(tree, pt): @unchecked

// Check that we only add the captured type `T` instead of a more complex type like `List[T]`.
// If we have `F[T]` with captured `F` and `T`, we should list `F` and `T` separately in the args.
for arg <- args do
assert(arg.isTerm || arg.tpe.isInstanceOf[TypeRef], "Expected TypeRef in Hole type args but got: " + arg.tpe)

// Check result type of the hole
if isTermHole then assert(tpt.typeOpt <:< pt)
else assert(tpt.typeOpt =:= pt)
Expand Down
6 changes: 6 additions & 0 deletions tests/pos-macros/i12440.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ trait Mirror:

class Eq:

def test0(using Quotes): Unit = '{
type T
${ summonType[T]; ??? }
${ summonType[List[T]]; ??? }
}

def test1(using Quotes): Unit = '{
val m: Mirror = ???
${ summonType[m.ElemTypes]; ??? }
Expand Down