diff --git a/compiler/src/dotty/tools/dotc/inlines/Inliner.scala b/compiler/src/dotty/tools/dotc/inlines/Inliner.scala index 69d3047b3bbb..c71648984664 100644 --- a/compiler/src/dotty/tools/dotc/inlines/Inliner.scala +++ b/compiler/src/dotty/tools/dotc/inlines/Inliner.scala @@ -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") @@ -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) @@ -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 @@ -809,6 +810,7 @@ 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 @@ -816,12 +818,13 @@ class Inliner(val call: tpd.Tree)(using Context): 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 @@ -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 = diff --git a/tests/pos/i16331/Macro.scala b/tests/pos/i16331/Macro.scala new file mode 100644 index 000000000000..b8a43c275ea9 --- /dev/null +++ b/tests/pos/i16331/Macro.scala @@ -0,0 +1,4 @@ +import scala.quoted._ +object Macro: + transparent inline def macroDef[A](): Int = ${ macroDefImpl() } + def macroDefImpl()(using q: Quotes): Expr[Int] = '{0} diff --git a/tests/pos/i16331/Main.scala b/tests/pos/i16331/Main.scala new file mode 100644 index 000000000000..a31a0f600a6b --- /dev/null +++ b/tests/pos/i16331/Main.scala @@ -0,0 +1,3 @@ +object Main: + inline def inlineDef[A](): Any = Macro.macroDef() + def main(args: Array[String]) = inlineDef()