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 captured references to singleton types #17138

Closed
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
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/PickleQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ class PickleQuotes extends MacroTransform {
case info: ClassInfo => info.parents.reduce(_ & _)
case info => info.hiBound
apply(hiBound)
case tp @ TermRef(pre, _) if isLocalPath(pre) =>
apply(tp.widenTermRefExpr)
case ThisType(ref @ TypeRef(pre, _)) if isLocalPath(pre) =>
apply(ref)
case tp =>
mapOver(tp)

Expand Down
5 changes: 2 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/Splicing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,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 @@ -360,9 +360,8 @@ 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
.getOrElseUpdate(tree.symbol, (TypeTree(tree.tpe), newQuotedTypeClassBinding(tree.tpe)))._2
bindingSym

private def capturedPartTypes(tpt: Tree)(using Context): Tree =
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/TreeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,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] || arg.tpe.isInstanceOf[TermRef] || arg.tpe.isInstanceOf[ThisType], "Expected TypeRef or TermRef in Hole type args but got: " + arg.tpe)

// Check result type of the hole
if isTermHole then assert(tpt.typeOpt <:< pt)
Expand Down
29 changes: 29 additions & 0 deletions tests/pos-macros/this-type-capture/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import scala.quoted.*

object Macro:
inline def generateCode: Unit = ${ testThisPaths }

def testThisPaths(using Quotes): Expr[Unit] =
'{
trait E extends G:
type V
val f: F
${
val expr = '{
// val _: Any = this // FIXME: this should work
// val _: Any = f // FIXME: this should work
val _: this.type = ???
val _: V = ???
val _: this.V = ???
val _: this.f.V = ???
val _: this.type = ???
val _: this.f.type = ???
}
expr
}
trait F:
type V
}

trait G:
val f: Any
1 change: 1 addition & 0 deletions tests/pos-macros/this-type-capture/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@main def test = Macro.generateCode