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

Overriding final method using MacroAnnotations #18825

Closed
hamzaremmal opened this issue Nov 2, 2023 · 0 comments · Fixed by #18826
Closed

Overriding final method using MacroAnnotations #18825

hamzaremmal opened this issue Nov 2, 2023 · 0 comments · Fixed by #18826

Comments

@hamzaremmal
Copy link
Member

Compiler version

3.3.1

Minimized code

Macro_1.scala

import scala.annotation.experimental
import scala.annotation.MacroAnnotation
import scala.quoted.*

@experimental
class toString extends MacroAnnotation :
    def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
        import quotes.reflect.*
        tree match
            case ClassDef(name, ctr, parents, self, body) =>
                val cls = tree.symbol
                val toStringSym = Symbol.requiredMethod("java.lang.Object.toString")
                val toStringOverrideSym = Symbol.newMethod(cls, "toString", toStringSym.info, Flags.Override, Symbol.noSymbol)
                val toStringDef = DefDef(toStringOverrideSym, _ => Some(Literal(StringConstant("Hello from macro"))))
                val newClassDef = ClassDef.copy(tree)(name, ctr, parents, self, toStringDef :: body)
                List(newClassDef)
            case _ =>
                report.error("@toString can only be annotated on class definitions")
                tree :: Nil

Test_2.scala

import annotation.experimental

class Foo :
    final override def toString(): String = "Hello"

@experimental
@toString
class AFoo extends Foo //:
    //override def toString(): String = "Hello from macro"

@experimental
@main def run =
    println(new AFoo().toString)

Output

It compiles and it crashes when running the code

Expectation

It should not compile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant