Skip to content

Commit 1ed25ce

Browse files
Merge pull request #13547 from dotty-staging/fixx-#13546
Set the correct owner when quote making inlinable
2 parents a771c86 + 09cb925 commit 1ed25ce

File tree

4 files changed

+37
-12
lines changed

4 files changed

+37
-12
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,11 @@ object SymDenotations {
11101110
enclClass(symbol, false)
11111111
}
11121112

1113+
/** Skips symbol that are not owned by a class */
1114+
def skipLocalOwners(using Context): Symbol =
1115+
if symbol.owner.isClass then symbol
1116+
else symbol.owner.skipLocalOwners
1117+
11131118
/** A class that in source code would be lexically enclosing */
11141119
final def lexicallyEnclosingClass(using Context): Symbol =
11151120
if (!exists || isClass) symbol else owner.lexicallyEnclosingClass

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,23 @@ trait QuotesAndSplices {
5656
else if !qctx.tpe.isStable then
5757
report.error(em"Quotes require stable Quotes, but found non stable $qctx", qctx.srcPos)
5858

59-
val tree1 =
60-
if ctx.mode.is(Mode.Pattern) then
61-
typedQuotePattern(tree, pt, qctx)
62-
else if tree.quoted.isType then
63-
val msg = em"Consider using canonical type constructor scala.quoted.Type.of[${tree.quoted}] instead"
64-
if sourceVersion.isAtLeast(`future-migration`) then report.error(msg, tree.srcPos)
65-
else report.warning(msg, tree.srcPos)
66-
typedTypeApply(untpd.TypeApply(untpd.ref(defn.QuotedTypeModule_of.termRef), tree.quoted :: Nil), pt)(using quoteContext).select(nme.apply).appliedTo(qctx)
67-
else
68-
typedApply(untpd.Apply(untpd.ref(defn.QuotedRuntime_exprQuote.termRef), tree.quoted), pt)(using pushQuotes(qctx)).select(nme.apply).appliedTo(qctx)
69-
makeInlineable(tree1.withSpan(tree.span))
59+
if ctx.mode.is(Mode.Pattern) then
60+
typedQuotePattern(tree, pt, qctx).withSpan(tree.span)
61+
else if tree.quoted.isType then
62+
val msg = em"Consider using canonical type constructor scala.quoted.Type.of[${tree.quoted}] instead"
63+
if sourceVersion.isAtLeast(`future-migration`) then report.error(msg, tree.srcPos)
64+
else report.warning(msg, tree.srcPos)
65+
val typeOfTree = untpd.TypeApply(untpd.ref(defn.QuotedTypeModule_of.termRef), tree.quoted :: Nil).withSpan(tree.span)
66+
makeInlineable(typedTypeApply(typeOfTree, pt)(using quoteContext).select(nme.apply).appliedTo(qctx).withSpan(tree.span))
67+
else
68+
val exprQuoteTree = untpd.Apply(untpd.ref(defn.QuotedRuntime_exprQuote.termRef), tree.quoted)
69+
makeInlineable(typedApply(exprQuoteTree, pt)(using pushQuotes(qctx)).select(nme.apply).appliedTo(qctx).withSpan(tree.span))
7070
}
7171

7272
private def makeInlineable(tree: Tree)(using Context): Tree =
73-
PrepareInlineable.makeInlineable(tree)
73+
inContext(ctx.withOwner(ctx.owner.skipLocalOwners)) {
74+
PrepareInlineable.makeInlineable(tree)
75+
}
7476

7577
/** Translate `${ t: Expr[T] }` into expression `t.splice` while tracking the quotation level in the context */
7678
def typedSplice(tree: untpd.Splice, pt: Type)(using Context): Tree = {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package mylib
2+
import scala.quoted.*
3+
4+
object Main:
5+
protected def foo: Unit = {}
6+
inline def fooCaller: Unit =
7+
def f = foo
8+
foo
9+
inline def fooCallerM: Unit = ${ fooMacro }
10+
def fooMacro(using Quotes): Expr[Unit] =
11+
'{ foo }
12+
val fooExpr = '{ foo }
13+
'{ $fooExpr }

tests/pos-macros/i13546/Test_2.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import mylib.Main
2+
3+
object Test:
4+
Main.fooCaller
5+
Main.fooCallerM

0 commit comments

Comments
 (0)