Skip to content

Fix #7343: Detect inlined quotes #8642

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 2 commits into from
Apr 6, 2020
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
9 changes: 1 addition & 8 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,7 @@ class ReifyQuotes extends MacroTransform {

val tpe = MethodType(defn.SeqType.appliedTo(defn.AnyType) :: Nil, tree.tpe.widen)
val meth = ctx.newSymbol(lambdaOwner, UniqueName.fresh(nme.ANON_FUN), Synthetic | Method, tpe)
val closure = Closure(meth, tss => body(tss.head.head)(ctx.withOwner(meth)).changeOwner(ctx.owner, meth)).withSpan(tree.span)

enclosingInlineds match {
case enclosingInline :: _ =>
// In case a tree was inlined inside of the quote and we this closure corresponds to code within it we need to keep the inlined node.
Inlined(enclosingInline, Nil, closure)(ctx.withSource(lambdaOwner.topLevelClass.source))
case Nil => closure
}
Closure(meth, tss => body(tss.head.head)(ctx.withOwner(meth)).changeOwner(ctx.owner, meth)).withSpan(tree.span)
}

private def transformWithCapturer(tree: Tree)(capturer: mutable.Map[Symbol, Tree] => Tree => Tree)(implicit ctx: Context): Tree = {
Expand Down
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1138,16 +1138,22 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
}

override def typedIdent(tree: untpd.Ident, pt: Type)(using Context): Tree =
tryInline(tree.asInstanceOf[tpd.Tree]) `orElse` super.typedIdent(tree, pt)
checkStaging(tryInline(tree.asInstanceOf[tpd.Tree]) `orElse` super.typedIdent(tree, pt))

override def typedSelect(tree: untpd.Select, pt: Type)(using Context): Tree = {
assert(tree.hasType, tree)
val qual1 = typed(tree.qualifier, selectionProto(tree.name, pt, this))
val res = constToLiteral(untpd.cpy.Select(tree)(qual1, tree.name).withType(tree.typeOpt))
ensureAccessible(res.tpe, tree.qualifier.isInstanceOf[untpd.Super], tree.sourcePos)
res
checkStaging(res)
}

private def checkStaging(tree: Tree): tree.type =
val sym = tree.symbol
if sym == defn.InternalQuoted_exprQuote || sym == defn.InternalQuoted_typeQuote then
ctx.compilationUnit.needsStaging = true
tree

override def typedIf(tree: untpd.If, pt: Type)(using Context): Tree =
typed(tree.cond, defn.BooleanType) match {
case cond1 @ ConstantValue(b: Boolean) =>
Expand Down
7 changes: 7 additions & 0 deletions tests/pos-macros/i7322/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted.{ QuoteContext, Expr, Type }

trait M[T] {
def f: Any
}

inline def g[T: Type](em: Expr[M[T]])(using QuoteContext) = '{$em.f}
3 changes: 3 additions & 0 deletions tests/pos-macros/i7322/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import scala.quoted.{ QuoteContext, Expr }

def h(m: Expr[M[String]])(using QuoteContext): Expr[Any] = g(m)
7 changes: 7 additions & 0 deletions tests/pos-macros/i7343/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted.{ QuoteContext, Expr }

trait M {
def f: Any
}

inline def g(em: Expr[M])(using QuoteContext) = '{$em.f}
3 changes: 3 additions & 0 deletions tests/pos-macros/i7343/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import scala.quoted.{ QuoteContext, Expr }

def h(m: Expr[M])(using QuoteContext): Expr[Any] = g(m)
1 change: 1 addition & 0 deletions tests/pos-macros/i7343b/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inline def g(using scala.quoted.QuoteContext) = '{1}
1 change: 1 addition & 0 deletions tests/pos-macros/i7343b/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def h(using scala.quoted.QuoteContext) = g