Skip to content

Commit a32c7a2

Browse files
committed
Also fix asQuotes implicit context for methods
1 parent 39e2f69 commit a32c7a2

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

compiler/src/dotty/tools/dotc/core/SymDenotations.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,12 @@ object SymDenotations {
339339

340340
def recurWithoutParamss(info: Type): List[List[Symbol]] = info match
341341
case info: LambdaType =>
342+
val flags =
343+
if info.isContextualMethod then Given | SyntheticParam
344+
else if info.isImplicitMethod then Implicit | SyntheticParam
345+
else SyntheticParam
342346
val params = info.paramNames.lazyZip(info.paramInfos).map((pname, ptype) =>
343-
newSymbol(symbol, pname, SyntheticParam, ptype))
347+
newSymbol(symbol, pname, flags, ptype))
344348
val prefs = params.map(_.namedType)
345349
for param <- params do
346350
param.info = param.info.substParams(info, prefs)

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dotty.tools.dotc.core.Contexts.*
1111
import dotty.tools.dotc.core.Decorators.*
1212
import dotty.tools.dotc.core.NameKinds
1313
import dotty.tools.dotc.core.NameOps.*
14+
import dotty.tools.dotc.core.Scopes.*
1415
import dotty.tools.dotc.core.StdNames.*
1516
import dotty.tools.dotc.core.Types
1617
import dotty.tools.dotc.NoCompilationUnit
@@ -2823,7 +2824,7 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
28232824
modFlags | dotc.core.Flags.ModuleValCreationFlags,
28242825
clsFlags | dotc.core.Flags.ModuleClassCreationFlags,
28252826
parents,
2826-
dotc.core.Scopes.newScope,
2827+
newScope,
28272828
privateWithin,
28282829
NoCoord,
28292830
compUnitInfo = null
@@ -2839,7 +2840,9 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
28392840
xCheckMacroAssert(!privateWithin.exists || privateWithin.isType, "privateWithin must be a type symbol or `Symbol.noSymbol`")
28402841
val privateWithin1 = if privateWithin.isTerm then Symbol.noSymbol else privateWithin
28412842
checkValidFlags(flags.toTermFlags, Flags.validMethodFlags)
2842-
dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | dotc.core.Flags.Method, tpe, privateWithin1)
2843+
val method = dotc.core.Symbols.newSymbol(owner, name.toTermName, flags | dotc.core.Flags.Method, tpe, privateWithin1)
2844+
method.setParamss(method.paramSymss)
2845+
method
28432846
def newVal(owner: Symbol, name: String, tpe: TypeRepr, flags: Flags, privateWithin: Symbol): Symbol =
28442847
xCheckMacroAssert(!privateWithin.exists || privateWithin.isType, "privateWithin must be a type symbol or `Symbol.noSymbol`")
28452848
val privateWithin1 = if privateWithin.isTerm then Symbol.noSymbol else privateWithin
@@ -3066,7 +3069,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
30663069

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

30713078
end extension
30723079

tests/run-macros/i22260.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
42
33
42
44
42
5+
42
56
42

tests/run-macros/i22260/Macros_1.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import scala.quoted.*
22

33
object Macros:
4+
inline def crMethod: Int = ${ createMethod }
45
inline def inMethod: Int = ${ insideMethod }
56
inline def usMethod: Int = ${ usingMethod }
67
inline def inObject: Int = ${ insideObject }
78
inline def inClass: Int = ${ insideClass }
89
inline def usClass: Int = ${ usingClass }
910

1011
def summon(using Quotes) =
11-
Expr.summon[Int].getOrElse('{ 0 })
12+
Expr.summon[Int].getOrElse('{0})
13+
14+
def createMethod(using Quotes): Expr[Int] =
15+
import quotes.reflect.*
16+
val tpe = MethodType(MethodTypeKind.Contextual)("x" :: Nil)(_ => TypeRepr.of[Int] :: Nil, _ => TypeRepr.of[Int])
17+
val sym = Symbol.newMethod(Symbol.spliceOwner, "foo", tpe)
18+
val method = DefDef(sym, _ => Some(summon(using sym.asQuotes).asTerm))
19+
Block(method :: Nil, Apply(Ref(sym), '{42}.asTerm :: Nil)).asExprOf[Int]
1220

1321
def insideMethod(using Quotes): Expr[Int] = '{
1422
def foo =

tests/run-macros/i22260/Test_2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@main def Test =
2+
println(Macros.crMethod)
23
println(Macros.inMethod)
34
println(Macros.usMethod)
45
println(Macros.inObject)

0 commit comments

Comments
 (0)