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

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

Closed
AugustNagro opened this issue Jun 26, 2023 · 2 comments · Fixed by #18075
Closed

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

AugustNagro opened this issue Jun 26, 2023 · 2 comments · Fixed by #18075
Assignees
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug

Comments

@AugustNagro
Copy link
Contributor

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

@AugustNagro AugustNagro added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 26, 2023
@odersky odersky added area:metaprogramming:quotes Issues related to quotes and splices and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 26, 2023
@nicolasstucki
Copy link
Contributor

nicolasstucki commented Jun 27, 2023

This is fixed on the main branch and 3.3.1-RC1.

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jun 27, 2023
nicolasstucki added a commit that referenced this issue Jun 27, 2023
Closes #18059

Probably fixed by #17342

[skip mima]
[skip docs]
[skip community_build]
@AugustNagro
Copy link
Contributor Author

AugustNagro commented Jun 27, 2023 via email

Kordyjan pushed a commit that referenced this issue Nov 23, 2023
Closes #18059

Probably fixed by #17342

[Cherry-picked 050f54b]
Kordyjan pushed a commit that referenced this issue Nov 29, 2023
Closes #18059

Probably fixed by #17342

[Cherry-picked 050f54b]
Kordyjan added a commit that referenced this issue Dec 8, 2023
Backports #18075 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:quotes Issues related to quotes and splices itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants