forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for overriding a definition with MacroAnnotation without dum…
…my definition (scala#19742)
- Loading branch information
Showing
3 changed files
with
28 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello, I was added by a MacroAnnotation and without being defined in the class. |
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,16 @@ | ||
import scala.annotation.MacroAnnotation | ||
import scala.quoted.* | ||
|
||
class implementAFoo extends MacroAnnotation: | ||
|
||
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = | ||
import quotes.reflect.* | ||
tree match | ||
case ClassDef(name, cstr, parents, self, body) => | ||
val owner = tree.symbol | ||
val sym = Symbol.newMethod(tree.symbol, "foo", ByNameType.apply(TypeRepr.of[String])) | ||
val mtd = DefDef.apply(sym, _ => Some(Literal(StringConstant("Hello, I was added by a MacroAnnotation and without being defined in the class.")))) | ||
List(ClassDef.copy(tree)(name, cstr, parents, self, mtd :: body)) | ||
case _ => report.errorAndAbort(s"@implementAFoo can only be applied to classes that extend AFoo") | ||
|
||
end implementAFoo |
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,11 @@ | ||
|
||
trait AFoo: | ||
def foo: String | ||
|
||
@implementAFoo | ||
class Foo extends AFoo | ||
|
||
@main def Test = | ||
val foo = new Foo | ||
println(foo.foo) | ||
|