-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enter missing symbols in MacroAnnotations
- Loading branch information
1 parent
e2b38f9
commit 03a41a6
Showing
6 changed files
with
66 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
error overriding method toString in class Foo of type (): String; | ||
method toString of type (): String cannot override final member method toString in class Foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// nopos-error | ||
|
||
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) |