You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importscala.annotation.experimentalimportscala.annotation.MacroAnnotationimportscala.quoted.*@experimental
classtoStringextendsMacroAnnotation:deftransform(usingQuotes)(tree: quotes.reflect.Definition):List[quotes.reflect.Definition] =importquotes.reflect.*
tree matchcaseClassDef(name, ctr, parents, self, body) =>valcls= tree.symbol
valtoStringSym=Symbol.requiredMethod("java.lang.Object.toString")
valtoStringOverrideSym=Symbol.newMethod(cls, "toString", toStringSym.info, Flags.Override, Symbol.noSymbol)
valtoStringDef=DefDef(toStringOverrideSym, _ =>Some(Literal(StringConstant("Hello from macro"))))
valnewClassDef=ClassDef.copy(tree)(name, ctr, parents, self, toStringDef :: body)
List(newClassDef)
case _ =>
report.error("@toString can only be annotated on class definitions")
tree ::Nil
Compiler version
3.3.1
Minimized code
Macro_1.scala
Test_2.scala
Output
It compiles and it crashes when running the code
Expectation
It should not compile
The text was updated successfully, but these errors were encountered: