Skip to content
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

Remove NamedArg from inlined arguments #17228

Merged
merged 1 commit into from
Apr 20, 2023
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
17 changes: 12 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,18 @@ 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 dropNameArg(arg: Tree): Tree = arg match
case NamedArg(_, arg1) => arg1
case SeqLiteral(elems, tpt) =>
cpy.SeqLiteral(arg)(elems.mapConserve(dropNameArg), tpt)
case _ => arg
arg0 match
case Typed(seq, tpt) if tpt.tpe.isRepeatedParam =>
if seq.tpe.derivesFrom(defn.ArrayClass) then wrapArray(dropNameArg(seq), arg0.tpe.elemType)
else cpy.Typed(arg0)(dropNameArg(seq), tpt)
case arg0 =>
dropNameArg(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)