Skip to content

Macro can invoke constructor of a case object #17545

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
arainko opened this issue May 20, 2023 · 0 comments · Fixed by #17553
Closed

Macro can invoke constructor of a case object #17545

arainko opened this issue May 20, 2023 · 0 comments · Fixed by #17553
Labels
area:metaprogramming:reflection Issues related to the quotes reflection API itype:bug
Milestone

Comments

@arainko
Copy link
Contributor

arainko commented May 20, 2023

Compiler version

3.2.2, 3.3.0-RC6

Minimized code

// InvokeConstructor.scala
//> using scala "3.3.0-RC6"

import scala.quoted.* 

object InvokeConstructor {
  inline def apply[A] = ${ constructorMacro[A] }

  def constructorMacro[A: Type](using Quotes) = {
    import quotes.reflect.*
    val tpe = TypeRepr.of[A]
    New(Inferred(tpe)).select(tpe.typeSymbol.primaryConstructor).appliedToArgs(Nil).asExprOf[A]
  }
}

Variant 1 - a local case object, which doesn't result in a runtime failure but we actually get a new instance of a singleton

// usage.scala
@main def main = {
  case object Whatever

  val whatever: Whatever.type = InvokeConstructor[Whatever.type]
  println(whatever eq Whatever) // evaluates to false
  println(whatever == Whatever) // evaluates to false
}

Variant 2 - a toplevel case object, this results in a runtime error

case object Whatever

/*
fails at runtime:
Exception in thread "main" java.lang.IllegalAccessError: class Usage$package$ tried to access private method 'void Whatever$.<init>()' (Usage$package$ and Whatever$ are in unnamed module of loader 'app')
        at Usage$package$.main(Usage.scala:5)
        at main.main(Usage.scala:3)
*/
@main def main = {

  val whatever: Whatever.type = InvokeConstructor[Whatever.type]
}

Expectation

Compilation failure.

@arainko arainko added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 20, 2023
@nicolasstucki nicolasstucki added area:metaprogramming:reflection Issues related to the quotes reflection API and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels May 22, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue May 22, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue May 22, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue May 23, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue May 23, 2023
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue May 25, 2023
@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metaprogramming:reflection Issues related to the quotes reflection API itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants