Skip to content

Unable to invoke a varargs method with non-one number of arguments in reflect api #18784

Closed
@rayrobdod

Description

@rayrobdod

Compiler version

3.3.1, 3.4.0-RC1-bin-20231027-c0eae68-NIGHTLY

Minimized code

import scala.quoted.*

object Macro {
  def template(args:List[Expr[Int]])(using Quotes):Expr[List[Int]] = {
    import quotes.reflect.*
    val listObjectTerm = '{ List }.asTerm
    Apply(
      TypeApply(
        Select.unique(listObjectTerm, "apply"),
        List(TypeTree.of[Int])
      ),
      args.map(_.asTerm)
    ).asExprOf[List[Int]]
  }

  inline def apply_0 = ${Macro.impl_0}
  def impl_0(using quotes:Quotes):Expr[List[Int]] =
    Macro.template(List.empty)

  inline def apply_1 = ${Macro.impl_1}
  def impl_1(using quotes:Quotes):Expr[List[Int]] =
    Macro.template(List(Expr(1)))

  inline def apply_2 = ${Macro.impl_2}
  def impl_2(using quotes:Quotes):Expr[List[Int]] =
    Macro.template(List(Expr(1), Expr(2)))

  inline def repeated = ${Macro.repeatedImpl}
  def repeatedImpl(using Quotes):Expr[List[Int]] = {
    import quotes.reflect.*
    val args = List(Expr(1), Expr(2))
    val listObjectTerm = '{ List }.asTerm
    Apply(
      TypeApply(
        Select.unique(listObjectTerm, "apply"),
        List(TypeTree.of[Int])
      ),
      List(Repeated(args.map(_.asTerm), TypeTree.of[Int]))
    ).asExprOf[List[Int]]
  }
}
Macro.apply_0
Macro.apply_1
Macro.apply_2
Macro.repeated

Output

scala> Macro.apply_0
-- Error: ----------------------------------------------------------------------
1 |Macro.apply_0
  |^^^^^^^^^^^^^
  |wrong number of arguments at pickler for (elems: Int*): List[Int]: (elems: Int*): List[Int], expected: 1, found: 0
1 error found

scala> Macro.apply_1
val res0: List[Int] = List(1)

scala> Macro.apply_2
-- Error: ----------------------------------------------------------------------
1 |Macro.apply_2
  |^^^^^^^^^^^^^
  |wrong number of arguments at pickler for (elems: Int*): List[Int]: (elems: Int*): List[Int], expected: 1, found: 2
1 error found

scala> Macro.repeated
-- [E007] Type Mismatch Error: -------------------------------------------------
1 |Macro.repeated
  |^^^^^^^^^^^^^^
  |Found:    Seq[Int]
  |Required: Int
  |
  | longer explanation available when compiling with `-explain`
1 error found

Expectation

The three apply_x calls should all succeed, producing List(), List(1) and List(1, 2) respectively

Alternatively, the three apply_x call should all be compile errors, and the repeated call should succeed, producing List(1, 2)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions