Skip to content

Interpolate any type vars from comparing against SelectionProto #16348

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 1 commit into from
Dec 13, 2022
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
19 changes: 11 additions & 8 deletions compiler/src/dotty/tools/dotc/inlines/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -749,9 +749,9 @@ class Inliner(val call: tpd.Tree)(using Context):
ctx

override def typedIdent(tree: untpd.Ident, pt: Type)(using Context): Tree =
val tree1 = inlineIfNeeded(
tryInlineArg(tree.asInstanceOf[tpd.Tree]) `orElse` super.typedIdent(tree, pt)
)
val locked = ctx.typerState.ownedVars
val tree0 = tryInlineArg(tree.asInstanceOf[tpd.Tree]) `orElse` super.typedIdent(tree, pt)
val tree1 = inlineIfNeeded(tree0, pt, locked)
tree1 match
case id: Ident if tpd.needsSelect(id.tpe) =>
inlining.println(i"expanding $id to selection")
Expand All @@ -760,6 +760,7 @@ class Inliner(val call: tpd.Tree)(using Context):
tree1

override def typedSelect(tree: untpd.Select, pt: Type)(using Context): Tree = {
val locked = ctx.typerState.ownedVars
val qual1 = typed(tree.qualifier, shallowSelectionProto(tree.name, pt, this))
val resNoReduce = untpd.cpy.Select(tree)(qual1, tree.name).withType(tree.typeOpt)
val reducedProjection = reducer.reduceProjection(resNoReduce)
Expand All @@ -771,7 +772,7 @@ class Inliner(val call: tpd.Tree)(using Context):
if resNoReduce ne res then
typed(res, pt) // redo typecheck if reduction changed something
else if res.symbol.isInlineMethod then
inlineIfNeeded(res)
inlineIfNeeded(res, pt, locked)
else
ensureAccessible(res.tpe, tree.qualifier.isInstanceOf[untpd.Super], tree.srcPos)
res
Expand Down Expand Up @@ -809,19 +810,21 @@ class Inliner(val call: tpd.Tree)(using Context):
tree match
case Quoted(Spliced(inner)) => inner
case _ => tree
val locked = ctx.typerState.ownedVars
val res = cancelQuotes(constToLiteral(betaReduce(super.typedApply(tree, pt)))) match {
case res: Apply if res.symbol == defn.QuotedRuntime_exprSplice
&& StagingContext.level == 0
&& !hasInliningErrors =>
val expanded = expandMacro(res.args.head, tree.srcPos)
typedExpr(expanded) // Inline calls and constant fold code generated by the macro
case res =>
specializeEq(inlineIfNeeded(res))
specializeEq(inlineIfNeeded(res, pt, locked))
}
res

override def typedTypeApply(tree: untpd.TypeApply, pt: Type)(using Context): Tree =
val tree1 = inlineIfNeeded(constToLiteral(betaReduce(super.typedTypeApply(tree, pt))))
val locked = ctx.typerState.ownedVars
val tree1 = inlineIfNeeded(constToLiteral(betaReduce(super.typedTypeApply(tree, pt))), pt, locked)
if tree1.symbol.isQuote then
ctx.compilationUnit.needsStaging = true
tree1
Expand Down Expand Up @@ -889,11 +892,11 @@ class Inliner(val call: tpd.Tree)(using Context):
/** True if this inline typer has already issued errors */
override def hasInliningErrors(using Context) = ctx.reporter.errorCount > initialErrorCount

private def inlineIfNeeded(tree: Tree)(using Context): Tree =
private def inlineIfNeeded(tree: Tree, pt: Type, locked: TypeVars)(using Context): Tree =
val meth = tree.symbol
if meth.isAllOf(DeferredInline) then
errorTree(tree, em"Deferred inline ${meth.showLocated} cannot be invoked")
else if Inlines.needsInlining(tree) then Inlines.inlineCall(tree)
else if Inlines.needsInlining(tree) then Inlines.inlineCall(simplify(tree, pt, locked))
else tree

override def typedUnadapted(tree: untpd.Tree, pt: Type, locked: TypeVars)(using Context): Tree =
Expand Down
4 changes: 4 additions & 0 deletions tests/pos/i16331/Macro.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import scala.quoted._
object Macro:
transparent inline def macroDef[A](): Int = ${ macroDefImpl() }
def macroDefImpl()(using q: Quotes): Expr[Int] = '{0}
3 changes: 3 additions & 0 deletions tests/pos/i16331/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Main:
inline def inlineDef[A](): Any = Macro.macroDef()
def main(args: Array[String]) = inlineDef()