-
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 the possibility to create a typeSymbol in the Quotes API (#20347)
Closes #19448
- Loading branch information
Showing
11 changed files
with
216 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
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,47 @@ | ||
//> using options -experimental -Yno-experimental | ||
import scala.quoted.* | ||
|
||
inline def testConflictingBounds = ${ testConflictingBoundsImpl } | ||
inline def testConflictingBoundsWithTypeLambda = ${ testConflictingBoundsWithTypeLambdaImpl } | ||
|
||
transparent inline def transparentTestConflictingBounds = ${ testConflictingBoundsImpl } | ||
transparent inline def transparentTestConflictingBoundsWithTypeLambda = ${ testConflictingBoundsWithTypeLambdaImpl } | ||
|
||
|
||
def testConflictingBoundsImpl(using Quotes): Expr[Object] = { | ||
import quotes.reflect.* | ||
|
||
def makeType(owner: Symbol): Symbol = | ||
// type Foo >: Int <: String | ||
Symbol.newBoundedType( | ||
owner, | ||
"Foo", | ||
Flags.EmptyFlags, | ||
TypeBounds(TypeRepr.of[Int], TypeRepr.of[String]), | ||
Symbol.noSymbol | ||
) | ||
makeClass(makeType) | ||
} | ||
|
||
def testConflictingBoundsWithTypeLambdaImpl(using Quotes): Expr[Object] = { | ||
import quotes.reflect.* | ||
def makeType(owner: Symbol): Symbol = | ||
// type Foo >: [X] =>> Int <: Any | ||
Symbol.newBoundedType( | ||
owner, | ||
"Foo", | ||
Flags.EmptyFlags, | ||
TypeBounds(TypeLambda.apply(List("X"), _ => List(TypeBounds.empty), _ => TypeRepr.of[Int]), TypeRepr.of[Any]), | ||
Symbol.noSymbol | ||
) | ||
makeClass(makeType) | ||
} | ||
|
||
def makeClass(using quotes: Quotes)(typeCons: quotes.reflect.Symbol => quotes.reflect.Symbol) = { | ||
import quotes.reflect.* | ||
val clsSymbol = Symbol.newClass(Symbol.spliceOwner, "CLS", List(TypeRepr.of[Object]), sym => List(typeCons(sym)), None) | ||
val classDef: ClassDef = ClassDef(clsSymbol, List(TypeTree.of[Object]), List(TypeDef(clsSymbol.typeMember("Foo")))) | ||
|
||
Block(List(classDef), Apply(Select(New(TypeIdent(clsSymbol)), clsSymbol.primaryConstructor), List.empty)).asExprOf[Object] | ||
} | ||
|
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 -Yno-experimental | ||
def test = | ||
transparentTestConflictingBounds // error | ||
transparentTestConflictingBoundsWithTypeLambda // error | ||
// testConflictingBounds // should throw an error here also, to be implemented before stabilisation | ||
// testConflictingBoundsWithTypeLambda // should throw an error here also, to be implemented before stabilisation |
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,49 @@ | ||
//> using options -experimental -Yno-experimental | ||
import scala.quoted.* | ||
|
||
inline def testMacro = ${ testImpl } | ||
|
||
transparent inline def transparentTestMacro = ${ testImpl } | ||
|
||
def testImpl(using Quotes): Expr[Object] = { | ||
import quotes.reflect.* | ||
|
||
def makeBasicType(owner: Symbol): Symbol = | ||
Symbol.newBoundedType(owner, "tpe", Flags.EmptyFlags, TypeBounds.lower(TypeRepr.of[String]), Symbol.noSymbol) | ||
|
||
def makeTypesForClass(owner: Symbol): List[Symbol] = | ||
val typeLambda = TypeLambda.apply(List("X"), _ => List(TypeBounds.empty), _ => TypeRepr.of[Int]) | ||
List( | ||
makeBasicType(owner), | ||
// type Bla >: Nothing <: [X] =>> Int | ||
Symbol.newBoundedType( | ||
owner, | ||
"tpe1", | ||
Flags.EmptyFlags, | ||
TypeBounds.upper(typeLambda), | ||
Symbol.noSymbol | ||
), | ||
// type Bar >: [X] =>> Int <: [X] =>> Int | ||
Symbol.newBoundedType( | ||
owner, | ||
"tpe2", | ||
Flags.EmptyFlags, | ||
TypeBounds(typeLambda, typeLambda), | ||
Symbol.noSymbol | ||
) | ||
) | ||
|
||
val typeDef = TypeDef(makeBasicType(Symbol.spliceOwner)) | ||
// Expr printer does not work here, see comment: | ||
// https://github.com/scala/scala3/pull/20347#issuecomment-2096824617 | ||
println(typeDef.toString) | ||
assert(typeDef.toString == "TypeDef(tpe,TypeTree[TypeBounds(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class java)),object lang),String),TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Any))])") | ||
|
||
val clsSymbol = Symbol.newClass(Symbol.spliceOwner, "CLS", List(TypeRepr.of[Object]), sym => makeTypesForClass(sym), None) | ||
val classDef: ClassDef = ClassDef(clsSymbol, List(TypeTree.of[Object]), List( | ||
TypeDef(clsSymbol.typeMember("tpe")), | ||
TypeDef(clsSymbol.typeMember("tpe1")), | ||
TypeDef(clsSymbol.typeMember("tpe2")), | ||
)) | ||
Block(List(classDef), Apply(Select(New(TypeIdent(clsSymbol)), clsSymbol.primaryConstructor), List.empty)).asExprOf[Object] | ||
} |
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 @@ | ||
//> using options -experimental -Yno-experimental | ||
def test = | ||
testMacro | ||
transparentTestMacro |
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,35 @@ | ||
//> using options -experimental -Yno-experimental | ||
import scala.quoted.* | ||
|
||
inline def testMacro = ${ testImpl } | ||
|
||
transparent inline def transparentTestMacro = ${ testImpl } | ||
|
||
def testImpl(using Quotes): Expr[Object] = { | ||
import quotes.reflect.* | ||
|
||
def makeBasicType(owner: Symbol): Symbol = | ||
Symbol.newTypeAlias(owner, "tpe", Flags.EmptyFlags, TypeRepr.of[String], Symbol.noSymbol) | ||
|
||
def makeTypesForClass(owner: Symbol): List[Symbol] = | ||
val typeLambda = TypeLambda.apply(List("X"), _ => List(TypeBounds.empty), _ => TypeRepr.of[Int]) | ||
List( | ||
makeBasicType(owner), | ||
// type Foo = [X] =>> Int | ||
Symbol.newTypeAlias( | ||
owner, | ||
"tpe1", | ||
Flags.EmptyFlags, | ||
typeLambda, | ||
Symbol.noSymbol | ||
), | ||
) | ||
|
||
val clsSymbol = Symbol.newClass(Symbol.spliceOwner, "CLS", List(TypeRepr.of[Object]), sym => makeTypesForClass(sym), None) | ||
val classDef: ClassDef = ClassDef(clsSymbol, List(TypeTree.of[Object]), List( | ||
TypeDef(clsSymbol.typeMember("tpe")), | ||
TypeDef(clsSymbol.typeMember("tpe1")), | ||
)) | ||
|
||
Block(List(classDef), Apply(Select(New(TypeIdent(clsSymbol)), clsSymbol.primaryConstructor), List.empty)).asExprOf[Object] | ||
} |
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 @@ | ||
//> using options -experimental -Yno-experimental | ||
def test = | ||
testMacro | ||
transparentTestMacro |
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,13 @@ | ||
//> using options -experimental -Yno-experimental | ||
import scala.quoted.* | ||
|
||
inline def testMacro = ${ testImpl } | ||
|
||
def testImpl(using Quotes): Expr[Unit] = { | ||
import quotes.reflect.* | ||
val sym = Symbol.newTypeAlias(Symbol.spliceOwner, "mytype", Flags.EmptyFlags, TypeRepr.of[String], Symbol.noSymbol) | ||
val typeDef = TypeDef(sym) | ||
assert(typeDef.show == "type mytype = java.lang.String") | ||
|
||
Block(List(typeDef), '{()}.asTerm).asExprOf[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,2 @@ | ||
//> using options -experimental -Yno-experimental | ||
def test = testMacro |
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