Skip to content

Commit

Permalink
Merge pull request #13547 from dotty-staging/fixx-#13546
Browse files Browse the repository at this point in the history
Set the correct owner when quote making inlinable
  • Loading branch information
nicolasstucki authored Oct 11, 2021
2 parents a771c86 + 09cb925 commit 1ed25ce
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
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

0 comments on commit 1ed25ce

Please sign in to comment.