-
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 ability to use the class symbol in classdef parents
- Loading branch information
Showing
8 changed files
with
116 additions
and
15 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
40 changes: 40 additions & 0 deletions
40
tests/pos-macros/newClassExtendsWithSymbolInParent/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,40 @@ | ||
//> using options -experimental | ||
|
||
import scala.quoted.* | ||
|
||
transparent 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 | ||
|
||
// using asType on the passed Symbol leads to cyclic reference errors | ||
def parents(cls: Symbol) = | ||
List(AppliedType(TypeRepr.typeConstructorOf(Class.forName("Foo")), List(TypeIdent(cls).tpe))) | ||
def decls(cls: Symbol): List[Symbol] = Nil | ||
|
||
val cls = Symbol.newClass(Symbol.spliceOwner, name, parents, decls, selfType = None, paramNames = Nil, paramTypes = Nil, Flags.EmptyFlags, Symbol.noSymbol) | ||
|
||
val parentsWithSym = | ||
cls.typeRef.asType match | ||
case '[t] => | ||
List(Apply(TypeApply(Select(New(TypeTree.of[Foo[t]]), TypeRepr.of[Foo[t]].typeSymbol.primaryConstructor), List(TypeTree.of[t])), List())) | ||
val clsDef = ClassDef(cls, parentsWithSym, body = Nil) | ||
|
||
val newCls = cls.typeRef.asType match | ||
case '[t] => | ||
Typed(Apply(Select(New(TypeIdent(cls)), cls.primaryConstructor), Nil), TypeTree.of[Foo[t]]) | ||
|
||
cls.typeRef.asType match | ||
case '[t] => | ||
Block(List(clsDef), newCls).asExprOf[Foo[t]] | ||
|
||
// '{ | ||
// class Name() extends Foo[Name.type]() | ||
// new Name() | ||
// } | ||
} | ||
|
||
class Foo[X]() { self: X => | ||
def getSelf: X = self | ||
} |
6 changes: 6 additions & 0 deletions
6
tests/pos-macros/newClassExtendsWithSymbolInParent/Test_2.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,6 @@ | ||
//> using options -experimental | ||
|
||
@main def Test: Unit = { | ||
val foo = makeClass("Bar") | ||
foo.getSelf | ||
} |
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