Skip to content

Set the correct owner when quote making inlinable #13547

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
merged 3 commits into from
Oct 11, 2021
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
5 changes: 5 additions & 0 deletions compiler/src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,11 @@ object SymDenotations {
enclClass(symbol, false)
}

/** Skips symbol that are not owned by a class */
def skipLocalOwners(using Context): Symbol =
if symbol.owner.isClass then symbol
else symbol.owner.skipLocalOwners

/** A class that in source code would be lexically enclosing */
final def lexicallyEnclosingClass(using Context): Symbol =
if (!exists || isClass) symbol else owner.lexicallyEnclosingClass
Expand Down
26 changes: 14 additions & 12 deletions compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,23 @@ trait QuotesAndSplices {
else if !qctx.tpe.isStable then
report.error(em"Quotes require stable Quotes, but found non stable $qctx", qctx.srcPos)

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

private def makeInlineable(tree: Tree)(using Context): Tree =
PrepareInlineable.makeInlineable(tree)
inContext(ctx.withOwner(ctx.owner.skipLocalOwners)) {
PrepareInlineable.makeInlineable(tree)
}

/** Translate `${ t: Expr[T] }` into expression `t.splice` while tracking the quotation level in the context */
def typedSplice(tree: untpd.Splice, pt: Type)(using Context): Tree = {
Expand Down
13 changes: 13 additions & 0 deletions tests/pos-macros/i13546/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mylib
import scala.quoted.*

object Main:
protected def foo: Unit = {}
inline def fooCaller: Unit =
def f = foo
foo
inline def fooCallerM: Unit = ${ fooMacro }
def fooMacro(using Quotes): Expr[Unit] =
'{ foo }
val fooExpr = '{ foo }
'{ $fooExpr }
5 changes: 5 additions & 0 deletions tests/pos-macros/i13546/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import mylib.Main

object Test:
Main.fooCaller
Main.fooCallerM