-
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.
TASTy macro annotations class modifications (part 2)
Enable modification of classes with `TastyAnnotation`: * Can annotate `class` to transform it * Can annotate `object` to transform the companion class Supported class modifications: * Modify the implementations of `def`, `val`, `var`, `lazy val`, `class`, `object` in the class * Add new `def`, `val`, `var`, `lazy val`, `class`, `object` members to the class * Add a new override for a `def`, `val`, `var`, `lazy val` members in the class Restrictions: * An annotation on a top-level class cannot return a top-level `def`, `val`, `var`, `lazy val`
- Loading branch information
1 parent
6853a75
commit 1dc36c6
Showing
33 changed files
with
556 additions
and
135 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,9 @@ | ||
|
||
-- Error: tests/neg-macros/annot-mod-class-add-top-method/Test_2.scala:1:0 --------------------------------------------- | ||
1 |@addTopLevelMethod // error | ||
|^^^^^^^^^^^^^^^^^^ | ||
|TASTy annotation can not add top-level method. @addTopLevelMethod tried to add method toLevelMethod$1. | ||
-- Error: tests/neg-macros/annot-mod-class-add-top-method/Test_2.scala:4:0 --------------------------------------------- | ||
4 |@addTopLevelMethod // error | ||
|^^^^^^^^^^^^^^^^^^ | ||
|TASTy annotation can not add top-level method. @addTopLevelMethod tried to add method toLevelMethod$2. |
18 changes: 18 additions & 0 deletions
18
tests/neg-macros/annot-mod-class-add-top-method/Macro_1.scala
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,18 @@ | ||
import scala.annotation.{experimental, TastyAnnotation} | ||
import scala.quoted._ | ||
import scala.collection.mutable | ||
|
||
@experimental | ||
class addTopLevelMethod extends TastyAnnotation: | ||
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 methType = MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Int]) | ||
val methSym = Symbol.newUniqueMethod(cls.owner, "toLevelMethod", methType, Flags.EmptyFlags, Symbol.noSymbol) | ||
val methDef = DefDef(methSym, _ => Some(Literal(IntConstant(1)))) | ||
List(methDef, tree) | ||
case _ => | ||
report.error("Annotation only supports `class`") | ||
List(tree) |
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,5 @@ | ||
@addTopLevelMethod // error | ||
class Foo | ||
|
||
@addTopLevelMethod // error | ||
object 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,9 @@ | ||
|
||
-- Error: tests/neg-macros/annot-mod-class-add-top-val/Test_2.scala:1:0 ------------------------------------------------ | ||
1 |@addTopLevelVal // error | ||
|^^^^^^^^^^^^^^^ | ||
|TASTy annotation can not add top-level value. @addTopLevelVal tried to add value toLevelvalod$1. | ||
-- Error: tests/neg-macros/annot-mod-class-add-top-val/Test_2.scala:4:0 ------------------------------------------------ | ||
4 |@addTopLevelVal // error | ||
|^^^^^^^^^^^^^^^ | ||
|TASTy annotation can not add top-level value. @addTopLevelVal tried to add value toLevelvalod$2. |
17 changes: 17 additions & 0 deletions
17
tests/neg-macros/annot-mod-class-add-top-val/Macro_1.scala
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,17 @@ | ||
import scala.annotation.{experimental, TastyAnnotation} | ||
import scala.quoted._ | ||
import scala.collection.mutable | ||
|
||
@experimental | ||
class addTopLevelVal extends TastyAnnotation: | ||
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 valSym = Symbol.newUniqueVal(cls.owner, "toLevelvalod", TypeRepr.of[Int], Flags.EmptyFlags, Symbol.noSymbol) | ||
val valDef = DefDef(valSym, _ => Some(Literal(IntConstant(1)))) | ||
List(valDef, tree) | ||
case _ => | ||
report.error("Annotation only supports `class`") | ||
List(tree) |
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,5 @@ | ||
@addTopLevelVal // error | ||
class Foo | ||
|
||
@addTopLevelVal // error | ||
object Foo |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.