Skip to content

Commit

Permalink
Remove NamedArg from inlined arguments
Browse files Browse the repository at this point in the history
Fixes #17227
  • Loading branch information
nicolasstucki committed Apr 11, 2023
1 parent bcaa1ca commit 0b346f0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
15 changes: 10 additions & 5 deletions compiler/src/dotty/tools/dotc/inlines/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,16 @@ class Inliner(val call: tpd.Tree)(using Context):
private[inlines] def paramBindingDef(name: Name, formal: Type, arg0: Tree,
buf: DefBuffer)(using Context): ValOrDefDef = {
val isByName = formal.dealias.isInstanceOf[ExprType]
val arg = arg0 match {
case Typed(arg1, tpt) if tpt.tpe.isRepeatedParam && arg1.tpe.derivesFrom(defn.ArrayClass) =>
wrapArray(arg1, arg0.tpe.elemType)
case _ => arg0
}
val arg =
def inlinedArg(arg: Tree): Tree = arg match
case Typed(seq @ SeqLiteral(elems, seqTpt), tpt) if tpt.tpe.isRepeatedParam =>
val elems1 = elems.mapConserve(inlinedArg)
val seq1 = cpy.SeqLiteral(seq)(elems1, seqTpt)
if seq.tpe.derivesFrom(defn.ArrayClass) then wrapArray(seq1, arg.tpe.elemType)
else cpy.Typed(arg)(seq1, tpt)
case NamedArg(_, arg1) => arg1
case _ => arg
inlinedArg(arg0)
val argtpe = arg.tpe.dealiasKeepAnnots.translateFromRepeated(toArray = false)
val argIsBottom = argtpe.isBottomTypeAfterErasure
val bindingType =
Expand Down
22 changes: 22 additions & 0 deletions tests/pos-macros/i17227/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import scala.quoted.*

inline def foo(f: Int => Int): Int => Int = ${impl('f)}
inline def bar(inline f: Int => Int): Int => Int = ${impl('f)}
inline def baz(inline f: (Int => Int)*): Int => Int = ${impl2('f)}

def impl(f: Expr[Int => Int])(using Quotes): Expr[Int => Int] =
assertNoNamedArgs(f)
'{identity}

def impl2(f: Expr[Seq[Int => Int]])(using Quotes): Expr[Int => Int] =
assertNoNamedArgs(f)
'{identity}

def assertNoNamedArgs(expr: Expr[Any])(using Quotes): Unit =
import quotes.reflect.*
new TreeTraverser {
override def traverseTree(tree: Tree)(owner: Symbol): Unit = tree match
case _: NamedArg =>
report.throwError(s"Unexpected NamedArg after inlining: ${tree}", tree.pos)
case _ => traverseTreeChildren(tree)(owner)
}.traverseTree(expr.asTerm)(Symbol.spliceOwner)
6 changes: 6 additions & 0 deletions tests/pos-macros/i17227/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def g(i: Int): Int = i

def test =
foo(f = g)
bar(f = g)
baz(f = g)
10 changes: 10 additions & 0 deletions tests/pos/i17227.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
inline def foo(f: Int => Int): Int => Int = f
inline def bar(inline f: Int => Int): Int => Int = f
inline def baz(f: (Int => Int)*): Int => Int = f.head

def g(i: Int): Int = i

def test =
foo(f = g)
bar(f = g)
baz(f = g)

0 comments on commit 0b346f0

Please sign in to comment.