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 2 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
28 changes: 16 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,25 @@ 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)
def quoteOwner(sym: Symbol): Symbol =
if sym.owner.isClass then sym else quoteOwner(sym.owner)
inContext(ctx.withOwner(quoteOwner(ctx.owner))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need quoteOwner.

ctx.owner.enclosingClass

will work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That will not work. We need the symbol of the method in that class. We are skipping local owners. Do we have something for that? I could not find it. I could add it to SymDenotation.

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