Skip to content

Splice hole with singleton captures #18357

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

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
7 changes: 2 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/Splicing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class Splicing extends MacroTransform:
// Dealias references to captured types
TypeTree(tree.tpe.dealias)
else super.transform(tree)
case tree: TypeTree =>
case _: TypeTree | _: SingletonTypeTree =>
if containsCapturedType(tree.tpe) && level >= 1 then getTagRefFor(tree)
else tree
case tree @ Assign(lhs: RefTree, rhs) =>
Expand Down Expand Up @@ -314,10 +314,7 @@ class Splicing extends MacroTransform:
)

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
refBindingMap.getOrElseUpdate(tree.symbol, (TypeTree(tree.tpe), newQuotedTypeClassBinding(tree.tpe)))._2

private def capturedPartTypes(quote: Quote)(using Context): Tree =
val (tags, body1) = inContextWithQuoteTypeTags {
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/TreeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ object TreeChecker {
// 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)
assert(arg.isTerm || arg.tpe.isInstanceOf[TypeRef | TermRef | ThisType], "Unexpected type arg in Hole: " + arg.tpe)

// Check result type of the hole
if isTerm then assert(tree1.typeOpt <:< pt)
Expand All @@ -743,7 +743,7 @@ object TreeChecker {
defn.AnyType
case tpe => tpe
defn.QuotedExprClass.typeRef.appliedTo(tpe)
else defn.QuotedTypeClass.typeRef.appliedTo(arg.typeOpt.widenTermRefExpr)
else defn.QuotedTypeClass.typeRef.appliedTo(arg.typeOpt)
}
val expectedResultType =
if isTerm then defn.QuotedExprClass.typeRef.appliedTo(tree1.typeOpt)
Expand Down
16 changes: 16 additions & 0 deletions tests/pos-macros/i17103c/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import scala.quoted.*

inline def test = ${ testExpr }

def testExpr(using Quotes): Expr[Unit] =
'{
trait C
val c: C = ???
${
val expr = '{
val cRef: c.type = ???
()
}
expr
}
}
1 change: 1 addition & 0 deletions tests/pos-macros/i17103c/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def Test = test