-
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.
Add reflect
ClassDef.apply
and Symbol.newClass
- Loading branch information
1 parent
fae7c09
commit 4845353
Showing
22 changed files
with
408 additions
and
10 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
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 @@ | ||
|
||
-- Error: tests/neg-macros/newClassExtendsNoParents/Test_2.scala:1:25 -------------------------------------------------- | ||
1 |def test: Any = makeClass("foo") // error | ||
| ^^^^^^^^^^^^^^^^ | ||
| Exception occurred while executing macro expansion. | ||
| java.lang.AssertionError: assertion failed: First parent must be a class | ||
| at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) | ||
| at scala.quoted.runtime.impl.QuotesImpl$reflect$Symbol$.newClass(QuotesImpl.scala:2464) | ||
| at scala.quoted.runtime.impl.QuotesImpl$reflect$Symbol$.newClass(QuotesImpl.scala:2463) | ||
| at Macro_1$package$.makeClassExpr(Macro_1.scala:11) | ||
| at Macro_1$package$.inline$makeClassExpr(Macro_1.scala:4) | ||
| | ||
|--------------------------------------------------------------------------------------------------------------------- | ||
|Inline stack trace | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
|This location contains code that was inlined from Macro_1.scala:3 | ||
3 |inline def makeClass(inline name: String): Any = ${ makeClassExpr('name) } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
--------------------------------------------------------------------------------------------------------------------- |
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,23 @@ | ||
import scala.quoted.* | ||
|
||
inline def makeClass(inline name: String): Any = ${ makeClassExpr('name) } | ||
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Any] = { | ||
import quotes.reflect.* | ||
|
||
val name = nameExpr.valueOrAbort | ||
val parents = List.empty[Tree] // BUG: first parent is not a class | ||
def decls(cls: Symbol): List[Symbol] = Nil | ||
|
||
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = Nil, decls, selfInfo = None) | ||
val clsDef = ClassDef(cls, parents, body = List()) | ||
val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Object]) | ||
|
||
Block(List(clsDef), newCls).asExpr | ||
|
||
// '{ | ||
// class `name`() { | ||
// def foo(): Unit = println("Calling `name`.foo") | ||
// } | ||
// new `name`() | ||
// } | ||
} |
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 @@ | ||
def test: Any = makeClass("foo") // error |
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 @@ | ||
|
||
-- Error: tests/neg-macros/newClassExtendsOnlyTrait/Test_2.scala:1:25 -------------------------------------------------- | ||
1 |def test: Foo = makeClass("foo") // error | ||
| ^^^^^^^^^^^^^^^^ | ||
| Exception occurred while executing macro expansion. | ||
| java.lang.AssertionError: assertion failed: First parent must be a class | ||
| at scala.runtime.Scala3RunTime$.assertFailed(Scala3RunTime.scala:8) | ||
| at scala.quoted.runtime.impl.QuotesImpl$reflect$Symbol$.newClass(QuotesImpl.scala:2464) | ||
| at scala.quoted.runtime.impl.QuotesImpl$reflect$Symbol$.newClass(QuotesImpl.scala:2463) | ||
| at Macro_1$package$.makeClassExpr(Macro_1.scala:12) | ||
| at Macro_1$package$.inline$makeClassExpr(Macro_1.scala:4) | ||
| | ||
|--------------------------------------------------------------------------------------------------------------------- | ||
|Inline stack trace | ||
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
|This location contains code that was inlined from Macro_1.scala:3 | ||
3 |inline def makeClass(inline name: String): Foo = ${ makeClassExpr('name) } | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
--------------------------------------------------------------------------------------------------------------------- |
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,31 @@ | ||
import scala.quoted.* | ||
|
||
inline def makeClass(inline name: String): Foo = ${ makeClassExpr('name) } | ||
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Foo] = { | ||
import quotes.reflect.* | ||
|
||
val name = nameExpr.valueOrAbort | ||
val parents = List(TypeTree.of[Foo]) // BUG: first parent is not a class | ||
def decls(cls: Symbol): List[Symbol] = | ||
List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit]))) | ||
|
||
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None) | ||
val fooSym = cls.declaredMethod("foo").head | ||
|
||
val fooDef = DefDef(fooSym, argss => Some('{println(s"Calling ${$nameExpr}.foo")}.asTerm)) | ||
val clsDef = ClassDef(cls, parents, body = List(fooDef)) | ||
val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Foo]) | ||
|
||
Block(List(clsDef), newCls).asExprOf[Foo] | ||
|
||
// '{ | ||
// class `name`() extends Foo { | ||
// def foo(): Unit = println("Calling `name`.foo") | ||
// } | ||
// new `name`() | ||
// } | ||
} | ||
|
||
trait Foo { | ||
def foo(): Unit | ||
} |
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 @@ | ||
def test: Foo = makeClass("foo") // error |
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,4 @@ | ||
Constructing foo | ||
class Test_2$package$foo$1 | ||
Constructing bar | ||
class Test_2$package$bar$1 |
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,21 @@ | ||
import scala.quoted.* | ||
|
||
inline def makeClass(inline name: String): Any = ${ makeClassExpr('name) } | ||
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Any] = { | ||
import quotes.reflect.* | ||
|
||
val name = nameExpr.valueOrAbort | ||
val parents = List(TypeTree.of[Object]) | ||
def decls(cls: Symbol): List[Symbol] = Nil | ||
|
||
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None) | ||
|
||
val clsDef = ClassDef(cls, parents, body = List('{println(s"Constructing ${$nameExpr}")}.asTerm)) | ||
val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Object]) | ||
|
||
Block(List(clsDef), newCls).asExpr | ||
// '{ | ||
// class `name`() { println("Constructing `name`") } | ||
// new `name`() | ||
// } | ||
} |
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,6 @@ | ||
@main def Test: Unit = { | ||
val foo = makeClass("foo") | ||
println(foo.getClass) | ||
val bar = makeClass("bar") | ||
println(bar.getClass) | ||
} |
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,4 @@ | ||
Calling foo.foo | ||
class Test_2$package$foo$1 | ||
Calling bar.foo | ||
class Test_2$package$bar$1 |
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,31 @@ | ||
import scala.quoted.* | ||
|
||
inline def makeClass(inline name: String): Foo = ${ makeClassExpr('name) } | ||
private def makeClassExpr(nameExpr: Expr[String])(using Quotes): Expr[Foo] = { | ||
import quotes.reflect.* | ||
|
||
val name = nameExpr.valueOrAbort | ||
val parents = List(TypeTree.of[Object], TypeTree.of[Foo]) | ||
def decls(cls: Symbol): List[Symbol] = | ||
List(Symbol.newMethod(cls, "foo", MethodType(Nil)(_ => Nil, _ => TypeRepr.of[Unit]))) | ||
|
||
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents = parents.map(_.tpe), decls, selfInfo = None) | ||
val fooSym = cls.declaredMethod("foo").head | ||
|
||
val fooDef = DefDef(fooSym, argss => Some('{println(s"Calling ${$nameExpr}.foo")}.asTerm)) | ||
val clsDef = ClassDef(cls, parents, body = List(fooDef)) | ||
val newCls = Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Foo]) | ||
|
||
Block(List(clsDef), newCls).asExprOf[Foo] | ||
|
||
// '{ | ||
// class `name`() extends Object, Foo { | ||
// def foo(): Unit = println("Calling `name`.foo") | ||
// } | ||
// new `name`() | ||
// } | ||
} | ||
|
||
trait Foo { | ||
def foo(): Unit | ||
} |
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,8 @@ | ||
@main def Test: Unit = { | ||
val foo: Foo = makeClass("foo") | ||
foo.foo() | ||
println(foo.getClass) | ||
val bar: Foo = makeClass("bar") | ||
bar.foo() | ||
println(bar.getClass) | ||
} |
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,4 @@ | ||
Calling Foo.foo with i = 1 | ||
class Test_2$package$foo$1 | ||
Calling Foo.foo with i = 1 | ||
class Test_2$package$bar$1 |
Oops, something went wrong.