Skip to content

Commit ab9e75d

Browse files
Remove NamedArg from inlined arguments (#17228)
Fixes #17227
2 parents a9a16eb + 781a398 commit ab9e75d

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

compiler/src/dotty/tools/dotc/inlines/Inliner.scala

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,18 @@ class Inliner(val call: tpd.Tree)(using Context):
207207
private[inlines] def paramBindingDef(name: Name, formal: Type, arg0: Tree,
208208
buf: DefBuffer)(using Context): ValOrDefDef = {
209209
val isByName = formal.dealias.isInstanceOf[ExprType]
210-
val arg = arg0 match {
211-
case Typed(arg1, tpt) if tpt.tpe.isRepeatedParam && arg1.tpe.derivesFrom(defn.ArrayClass) =>
212-
wrapArray(arg1, arg0.tpe.elemType)
213-
case _ => arg0
214-
}
210+
val arg =
211+
def dropNameArg(arg: Tree): Tree = arg match
212+
case NamedArg(_, arg1) => arg1
213+
case SeqLiteral(elems, tpt) =>
214+
cpy.SeqLiteral(arg)(elems.mapConserve(dropNameArg), tpt)
215+
case _ => arg
216+
arg0 match
217+
case Typed(seq, tpt) if tpt.tpe.isRepeatedParam =>
218+
if seq.tpe.derivesFrom(defn.ArrayClass) then wrapArray(dropNameArg(seq), arg0.tpe.elemType)
219+
else cpy.Typed(arg0)(dropNameArg(seq), tpt)
220+
case arg0 =>
221+
dropNameArg(arg0)
215222
val argtpe = arg.tpe.dealiasKeepAnnots.translateFromRepeated(toArray = false)
216223
val argIsBottom = argtpe.isBottomTypeAfterErasure
217224
val bindingType =

tests/pos-macros/i17227/Macro_1.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import scala.quoted.*
2+
3+
inline def foo(f: Int => Int): Int => Int = ${impl('f)}
4+
inline def bar(inline f: Int => Int): Int => Int = ${impl('f)}
5+
inline def baz(inline f: (Int => Int)*): Int => Int = ${impl2('f)}
6+
7+
def impl(f: Expr[Int => Int])(using Quotes): Expr[Int => Int] =
8+
assertNoNamedArgs(f)
9+
'{identity}
10+
11+
def impl2(f: Expr[Seq[Int => Int]])(using Quotes): Expr[Int => Int] =
12+
assertNoNamedArgs(f)
13+
'{identity}
14+
15+
def assertNoNamedArgs(expr: Expr[Any])(using Quotes): Unit =
16+
import quotes.reflect.*
17+
new TreeTraverser {
18+
override def traverseTree(tree: Tree)(owner: Symbol): Unit = tree match
19+
case _: NamedArg =>
20+
report.throwError(s"Unexpected NamedArg after inlining: ${tree}", tree.pos)
21+
case _ => traverseTreeChildren(tree)(owner)
22+
}.traverseTree(expr.asTerm)(Symbol.spliceOwner)

tests/pos-macros/i17227/Test_2.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def g(i: Int): Int = i
2+
3+
def test =
4+
foo(f = g)
5+
bar(f = g)
6+
baz(f = g)

tests/pos/i17227.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
inline def foo(f: Int => Int): Int => Int = f
2+
inline def bar(inline f: Int => Int): Int => Int = f
3+
inline def baz(f: (Int => Int)*): Int => Int = f.head
4+
5+
def g(i: Int): Int = i
6+
7+
def test =
8+
foo(f = g)
9+
bar(f = g)
10+
baz(f = g)

0 commit comments

Comments
 (0)