Skip to content

Compiler error if using Quotes parameter is not named (ie, using q: Quotes)  #18059

Closed
@AugustNagro

Description

@AugustNagro

Compiler version

3.3.0

Minimized code

Repo: https://github.com/AugustNagro/quotes-required-error-msg

case class AppUser(
    id: Long,
    firstName: Option[String],
    @SqlName("last_name") lastName: String
)

@main def hello: Unit =
  println(sqlFieldNamesFor[AppUser]) // Vector((lastName, last_name))

In different file:

import scala.annotation.StaticAnnotation

class SqlName(val sqlName: String) extends StaticAnnotation

import scala.compiletime.*
import scala.quoted.*

inline def sqlFieldNamesFor[T]: Vector[(String, String)] = ${
  sqlFieldNamesForImpl[T]
}

private def sqlFieldNamesForImpl[T: Type](using
    Quotes // must be named!! like `q: Quotes`
): Expr[Vector[(String, String)]] =
  import quotes.reflect.*
  val annot = TypeRepr.of[SqlName].typeSymbol
  val tuples: Seq[Expr[(String, String)]] = TypeRepr
    .of[T]
    .typeSymbol
    .primaryConstructor
    .paramSymss
    .head
    .collect:
      case sym if sym.hasAnnotation(annot) =>
        val fieldNameExpr = Expr(sym.name.asInstanceOf[String])
        val annotExpr = sym.getAnnotation(annot).get.asExprOf[SqlName]
        '{ ($fieldNameExpr, $annotExpr.sqlName) }
  val seq: Expr[Seq[(String, String)]] = Expr.ofSeq(tuples)
  '{ $seq.toVector }

Output

[error] -- [E007] Type Mismatch Error: /Users/august/prog/test/macro-test/src/main/scala/macros.scala:23:8 
[error] 23 |        '{ ($fieldNameExpr, $annotExpr.sqlName) }
[error]    |        ^
[error]    |Found:    (x$1 : x$1².reflect.Symbol)
[error]    |Required: quoted.Quotes
[error]    |
[error]    |where:    x$1  is a parameter in an anonymous function in method sqlFieldNamesForImpl
[error]    |          x$1² is a parameter in method sqlFieldNamesForImpl
[error]    |
[error]    | longer explanation available when compiling with `-explain`
[error] one error found
[error] (Compile / compileIncremental) Compilation failed
[error] Total time: 4 s, completed Jun 25, 2023, 5:02:56 PM
sbt:macro-test> 

Expectation

The current error message is difficult to understand. If you add a name to the given Quotes, the macro compiles and works fine. A better error message (or removing the error) would be helpful. It looks like others have run into the issue as well: https://stackoverflow.com/questions/68814663/scala-3-macro-found-x1-x1%C2%B2-reflect-symbol-required-quoted-quotes

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions