Skip to content

Preserve implicits in Quotes context #23263

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 2 commits 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
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,13 @@ object SymDenotations {

def recurWithoutParamss(info: Type): List[List[Symbol]] = info match
case info: LambdaType =>
val params = info.paramNames.lazyZip(info.paramInfos).map((pname, ptype) =>
newSymbol(symbol, pname, SyntheticParam, ptype))
val commonFlags =
if info.isContextualMethod then Given | SyntheticParam
else if info.isImplicitMethod then Implicit | SyntheticParam
else SyntheticParam
val params = info.paramNames.lazyZip(info.paramInfos).map: (pname, ptype) =>
val flags = if ptype.hasAnnotation(defn.ErasedParamAnnot) then commonFlags | Erased else commonFlags
newSymbol(symbol, pname, flags, ptype)
val prefs = params.map(_.namedType)
for param <- params do
param.info = param.info.substParams(info, prefs)
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import scala.collection.mutable

import QuoteUtils.*
import dotty.tools.io.NoAbstractFile
import dotty.tools.dotc.ast.TreeMapWithImplicits

object PickledQuotes {
import tpd.*
Expand Down Expand Up @@ -99,7 +100,7 @@ object PickledQuotes {

/** Replace all term holes with the spliced terms */
private def spliceTerms(tree: Tree, typeHole: TypeHole, termHole: ExprHole)(using Context): Tree = {
def evaluateHoles = new TreeMapWithPreciseStatContexts {
def evaluateHoles = new TreeMapWithImplicits {
override def transform(tree: tpd.Tree)(using Context): tpd.Tree = tree match {
case Hole(isTerm, idx, args, _) =>
inContext(SpliceScope.contextWithNewSpliceScope(tree.sourcePos)) {
Expand Down
13 changes: 10 additions & 3 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Decorators.*
import dotty.tools.dotc.core.NameKinds
import dotty.tools.dotc.core.NameOps.*
import dotty.tools.dotc.core.Scopes.*
import dotty.tools.dotc.core.StdNames.*
import dotty.tools.dotc.core.Types
import dotty.tools.dotc.NoCompilationUnit
Expand Down Expand Up @@ -2823,7 +2824,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
modFlags | dotc.core.Flags.ModuleValCreationFlags,
clsFlags | dotc.core.Flags.ModuleClassCreationFlags,
parents,
dotc.core.Scopes.newScope,
newScope,
privateWithin,
NoCoord,
compUnitInfo = null
Expand All @@ -2839,7 +2840,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
xCheckMacroAssert(!privateWithin.exists || privateWithin.isType, "privateWithin must be a type symbol or `Symbol.noSymbol`")
val privateWithin1 = if privateWithin.isTerm then Symbol.noSymbol else privateWithin
checkValidFlags(flags.toTermFlags, Flags.validMethodFlags)
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | dotc.core.Flags.Method, tpe, privateWithin1)
val method = dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | dotc.core.Flags.Method, tpe, privateWithin1)
method.setParamss(method.paramSymss)
method
def newVal(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
xCheckMacroAssert(!privateWithin.exists || privateWithin.isType, "privateWithin must be a type symbol or `Symbol.noSymbol`")
val privateWithin1 = if privateWithin.isTerm then Symbol.noSymbol else privateWithin
Expand Down Expand Up @@ -3066,7 +3069,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler

def asQuotes: Nested =
assert(self.ownersIterator.contains(ctx.owner), s"$self is not owned by ${ctx.owner}")
new QuotesImpl(using ctx.withOwner(self))
val newCtx = if ctx.owner eq self then ctx else
val newCtx = ctx.fresh.setOwner(self)
if !self.flags.is(Flags.Method) then newCtx
else newCtx.setScope(newScopeWith(self.paramSymss.flatten*))
new QuotesImpl(using newCtx)

end extension

Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i19842-a.check
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
|
| at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
| at dotty.tools.dotc.transform.TreeChecker$.checkParents(TreeChecker.scala:210)
| at scala.quoted.runtime.impl.QuotesImpl$reflect$ClassDef$.module(QuotesImpl.scala:286)
| at scala.quoted.runtime.impl.QuotesImpl$reflect$ClassDef$.module(QuotesImpl.scala:285)
| at scala.quoted.runtime.impl.QuotesImpl$reflect$ClassDef$.module(QuotesImpl.scala:284)
| at Macros$.makeSerializer(Macro.scala:25)
|
|---------------------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i19842-b.check
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
|
| at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8)
| at dotty.tools.dotc.transform.TreeChecker$.checkParents(TreeChecker.scala:210)
| at scala.quoted.runtime.impl.QuotesImpl$reflect$ClassDef$.module(QuotesImpl.scala:286)
| at scala.quoted.runtime.impl.QuotesImpl$reflect$ClassDef$.module(QuotesImpl.scala:285)
| at scala.quoted.runtime.impl.QuotesImpl$reflect$ClassDef$.module(QuotesImpl.scala:284)
| at Macros$.makeSerializer(Macro.scala:27)
|
|---------------------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i23008.check
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
| Exception occurred while executing macro expansion.
| java.lang.IllegalArgumentException: requirement failed: value of StringConstant cannot be `null`
| at scala.Predef$.require(Predef.scala:337)
| at scala.quoted.runtime.impl.QuotesImpl$reflect$StringConstant$.apply(QuotesImpl.scala:2534)
| at scala.quoted.runtime.impl.QuotesImpl$reflect$StringConstant$.apply(QuotesImpl.scala:2533)
| at scala.quoted.runtime.impl.QuotesImpl$reflect$StringConstant$.apply(QuotesImpl.scala:2532)
| at scala.quoted.ToExpr$StringToExpr.apply(ToExpr.scala:80)
| at scala.quoted.ToExpr$StringToExpr.apply(ToExpr.scala:78)
| at scala.quoted.Expr$.apply(Expr.scala:70)
Expand Down
6 changes: 6 additions & 0 deletions tests/run-macros/i22260.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
42
42
42
42
42
42
57 changes: 57 additions & 0 deletions tests/run-macros/i22260/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import scala.quoted.*

object Macros:
inline def crMethod: Int = ${ createMethod }
inline def inMethod: Int = ${ insideMethod }
inline def usMethod: Int = ${ usingMethod }
inline def inObject: Int = ${ insideObject }
inline def inClass: Int = ${ insideClass }
inline def usClass: Int = ${ usingClass }

def summon(using Quotes) =
Expr.summon[Int].getOrElse('{0})

def createMethod(using Quotes): Expr[Int] =
import quotes.reflect.*
val tpe = MethodType(MethodTypeKind.Contextual)("x" :: Nil)(_ => TypeRepr.of[Int] :: Nil, _ => TypeRepr.of[Int])
val sym = Symbol.newMethod(Symbol.spliceOwner, "foo", tpe)
val method = DefDef(sym, _ => Some(summon(using sym.asQuotes).asTerm))
Block(method :: Nil, Apply(Ref(sym), '{42}.asTerm :: Nil)).asExprOf[Int]

def insideMethod(using Quotes): Expr[Int] = '{
def foo =
given Int = 42
${summon}

foo
}

def usingMethod(using Quotes): Expr[Int] = '{
def foo(using Int) =
${summon}

foo(using 42)
}

def insideObject(using Quotes): Expr[Int] = '{
object Foo:
given Int = 42
val x = ${summon}

Foo.x
}

def insideClass(using Quotes): Expr[Int] = '{
class Foo:
given Int = 42
val x = ${summon}

new Foo().x
}

def usingClass(using Quotes): Expr[Int] = '{
class Foo(using Int):
val x = ${summon}

new Foo(using 42).x
}
7 changes: 7 additions & 0 deletions tests/run-macros/i22260/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@main def Test =
println(Macros.crMethod)
println(Macros.inMethod)
println(Macros.usMethod)
println(Macros.inObject)
println(Macros.inClass)
println(Macros.usClass)
Loading