Skip to content

Internal "Wrong number of arguments" error when reading a Scala 2 annotation in a macro #22200

Open
@jchyb

Description

@jchyb

Compiler version

Scala 2: 2.13.14
Scala 3: 3.3.4

Minimized code

Compiled with Scala 2:

class annot(str: List[String]) extends scala.annotation.StaticAnnotation
@annot(List("test", "test2")) class TestClass

Compiled with Scala 3, with results of the above on the classpath:
Macro.scala:

import scala.quoted._

object Macro {
  inline def call: Unit = ${ callImpl }
  def callImpl(using Quotes): Expr[Unit] = {
    import quotes.reflect._
    val sym = TypeRepr.of[TestClass].typeSymbol
    val annot = TypeRepr.of[annot].typeSymbol

    println(sym.getAnnotation(annot)) // causes error

    '{()}
  }
}

Main.scala:

@main def main() = Macro.call

Output

[error] -- Error: /Users/jchyb/workspace/mix-macros-scala-2-and-3/app3/src/main/scala/Main.scala:4:25 
[error] 4 |@main def main() = Macro.call
[error]   |                   ^^^^^^^^^^
[error]   |wrong number of arguments at <no phase> for (elems: String*): CC[String]: (elems: String*): CC[String], expected: 1, found: 2
Some(Apply(Select(New(TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class <empty>)),class annot)]),<init>),List(Apply(TypeApply(Select(Select(Select(Ident(scala),package),List),apply),List(TypeTree[TypeRef(ThisType(TypeRef(NoPrefix,module class lang)),class String)])),List(Literal(Constant(test)), Literal(Constant(test2)))))))

Expectation

No error.
An interesting observation: this bug does not happen when the annotation argument is not a list. Also we do not return from the macro instantly as we see the error, instead still printing out the tree and ending compilation when we leave the macro.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions