Skip to content

Enum values are not properly reused due to underlyingArgument in macros #14510

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

Closed
Atry opened this issue Feb 18, 2022 · 3 comments · Fixed by #14619
Closed

Enum values are not properly reused due to underlyingArgument in macros #14510

Atry opened this issue Feb 18, 2022 · 3 comments · Fixed by #14619
Labels
area:documentation area:metaprogramming:quotes Issues related to quotes and splices
Milestone

Comments

@Atry
Copy link
Contributor

Atry commented Feb 18, 2022

Compiler version

3.1.1

Minimized code

import scala.quoted.*

given staging.Compiler = staging.Compiler.make(getClass.getClassLoader)

def underlyingArgumentImpl[A](expr: Expr[A])(using Quotes, Type[A]) =
  import scala.quoted.quotes.reflect.*
  expr.asTerm.underlyingArgument.asExprOf[A]

inline def underlyingArgument[A](inline a: A): A = ${
  underlyingArgumentImpl('a)
}

val b: Boolean = staging.run[Boolean] {
  '{
    def f() = {
      enum E:
        case V
      underlyingArgument(E.V == E.V)
    }
    f()
  }
}

println(b)

https://scastie.scala-lang.org/82665kkpQ3KIBESu0jMnTg

Output

false

Expectation

true

@Atry Atry added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Feb 18, 2022
@Atry Atry changed the title enum values did not properly reused due to underlyingArgument in macros Enum values were not properly reused due to underlyingArgument in macros Feb 18, 2022
@Atry
Copy link
Contributor Author

Atry commented Feb 18, 2022

If I replace expr.asTerm.underlyingArgument.asExprOf[A] with expr.asTerm.asExprOf[A], then the output would be true, as expected.

https://scastie.scala-lang.org/XuLYeLJ4TPmf8saom43maA

@Atry Atry changed the title Enum values were not properly reused due to underlyingArgument in macros Enum values are not properly reused due to underlyingArgument in macros Feb 18, 2022
@Atry
Copy link
Contributor Author

Atry commented Feb 18, 2022

Note that the staging compiler is used to bypass the error "Cannot call macro method underlyingArgumentImpl defined in the same source file". This bug can be reproduced without the staging compiler.

@romanowski romanowski 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 Feb 21, 2022
@nicolasstucki
Copy link
Contributor

It is unsafe to use the result of underlyingArgument in the generated code. The tree returned by this method is only meant to be analyzed.

The documentation should state this clearly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:documentation area:metaprogramming:quotes Issues related to quotes and splices
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants