Skip to content

Commit 5d06f96

Browse files
committed
Address some review comments
1 parent 9db0a00 commit 5d06f96

File tree

5 files changed

+109
-12
lines changed

5 files changed

+109
-12
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,11 +2653,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
26532653
def newTypeAlias(owner: Symbol, name: String, flags: Flags, tpe: TypeRepr, privateWithin: Symbol): Symbol =
26542654
checkValidFlags(flags.toTypeFlags, Flags.validTypeAliasFlags)
26552655
assert(!tpe.isInstanceOf[Types.TypeBounds], "Passed `tpe` into newTypeAlias should not represent TypeBounds")
2656-
dotc.core.Symbols.newSymbol(owner, name.toTypeName, flags | dotc.core.Flags.Deferred, dotc.core.Types.TypeAlias(tpe), privateWithin)
2656+
dotc.core.Symbols.newSymbol(owner, name.toTypeName, flags, dotc.core.Types.TypeAlias(tpe), privateWithin)
26572657

26582658
def newBoundedType(owner: Symbol, name: String, flags: Flags, tpe: TypeBounds, privateWithin: Symbol): Symbol =
26592659
checkValidFlags(flags.toTypeFlags, Flags.validBoundedTypeFlags)
2660-
dotc.core.Symbols.newSymbol(owner, name.toTypeName, flags, tpe, privateWithin)
2660+
dotc.core.Symbols.newSymbol(owner, name.toTypeName, flags | dotc.core.Flags.Deferred, tpe, privateWithin)
26612661

26622662
def noSymbol: Symbol = dotc.core.Symbols.NoSymbol
26632663

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//> using options -experimental -Yno-experimental
2+
import scala.quoted.*
3+
4+
inline def testConflictingBounds = ${ testConflictingBoundsImpl }
5+
inline def testConflictingBoundsWithTypeLambda = ${ testConflictingBoundsWithTypeLambdaImpl }
6+
7+
transparent inline def transparentTestConflictingBounds = ${ testConflictingBoundsImpl }
8+
transparent inline def transparentTestConflictingBoundsWithTypeLambda = ${ testConflictingBoundsWithTypeLambdaImpl }
9+
10+
11+
def testConflictingBoundsImpl(using Quotes): Expr[Object] = {
12+
import quotes.reflect.*
13+
14+
def makeType(owner: Symbol): Symbol =
15+
// type Foo >: Int <: String
16+
Symbol.newBoundedType(
17+
owner,
18+
"Foo",
19+
Flags.EmptyFlags,
20+
TypeBounds(TypeRepr.of[Int], TypeRepr.of[String]),
21+
Symbol.noSymbol
22+
)
23+
makeClass(makeType)
24+
}
25+
26+
def testConflictingBoundsWithTypeLambdaImpl(using Quotes): Expr[Object] = {
27+
import quotes.reflect.*
28+
def makeType(owner: Symbol): Symbol =
29+
// type Foo >: [X] =>> Int <: Any
30+
Symbol.newBoundedType(
31+
owner,
32+
"Foo",
33+
Flags.EmptyFlags,
34+
TypeBounds(TypeLambda.apply(List("X"), _ => List(TypeBounds.empty), _ => TypeRepr.of[Int]), TypeRepr.of[Any]),
35+
Symbol.noSymbol
36+
)
37+
makeClass(makeType)
38+
}
39+
40+
def makeClass(using quotes: Quotes)(typeCons: quotes.reflect.Symbol => quotes.reflect.Symbol) = {
41+
import quotes.reflect.*
42+
val clsSymbol = Symbol.newClass(Symbol.spliceOwner, "CLS", List(TypeRepr.of[Object]), sym => List(typeCons(sym)), None)
43+
val classDef: ClassDef = ClassDef(clsSymbol, List(TypeTree.of[Object]), List(TypeDef(clsSymbol.typeMember("Foo"))))
44+
45+
Block(List(classDef), Apply(Select(New(TypeIdent(clsSymbol)), clsSymbol.primaryConstructor), List.empty)).asExprOf[Object]
46+
}
47+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//> using options -experimental -Yno-experimental
2+
def test =
3+
transparentTestConflictingBounds // error
4+
transparentTestConflictingBoundsWithTypeLambda // error
5+
// testConflictingBounds // should throw an error here also, to be implemented before stabilisation
6+
// testConflictingBoundsWithTypeLambda // should throw an error here also, to be implemented before stabilisation

tests/pos-macros/quote-sym-newboundedtype/Macro_1.scala

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,42 @@ transparent inline def transparentTestMacro = ${ testImpl }
88
def testImpl(using Quotes): Expr[Object] = {
99
import quotes.reflect.*
1010

11-
def makeType(owner: Symbol): Symbol =
12-
Symbol.newBoundedType(owner, "mytype", Flags.EmptyFlags, TypeBounds.lower(TypeRepr.of[String]), Symbol.noSymbol)
11+
def makeBasicType(owner: Symbol): Symbol =
12+
Symbol.newBoundedType(owner, "tpe", Flags.EmptyFlags, TypeBounds.lower(TypeRepr.of[String]), Symbol.noSymbol)
1313

14-
val typeDef = TypeDef(makeType(Symbol.spliceOwner))
14+
def makeTypesForClass(owner: Symbol): List[Symbol] =
15+
val typeLambda = TypeLambda.apply(List("X"), _ => List(TypeBounds.empty), _ => TypeRepr.of[Int])
16+
List(
17+
makeBasicType(owner),
18+
// type Bla >: Nothing <: [X] =>> Int
19+
Symbol.newBoundedType(
20+
owner,
21+
"tpe1",
22+
Flags.EmptyFlags,
23+
TypeBounds.upper(typeLambda),
24+
Symbol.noSymbol
25+
),
26+
// type Bar >: [X] =>> Int <: [X] =>> Int
27+
Symbol.newBoundedType(
28+
owner,
29+
"tpe2",
30+
Flags.EmptyFlags,
31+
TypeBounds(typeLambda, typeLambda),
32+
Symbol.noSymbol
33+
)
34+
)
35+
36+
val typeDef = TypeDef(makeBasicType(Symbol.spliceOwner))
1537
// Expr printer does not work here, see comment:
1638
// https://github.com/scala/scala3/pull/20347#issuecomment-2096824617
17-
assert(typeDef.toString == "TypeDef(mytype,TypeTree[TypeBounds(TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class java)),object lang),String),TypeRef(ThisType(TypeRef(NoPrefix,module class scala)),class Any))])")
39+
println(typeDef.toString)
40+
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))])")
1841

19-
val clsSymbol = Symbol.newClass(Symbol.spliceOwner, "CLS", List(TypeRepr.of[Object]), sym => List(makeType(sym)), None)
20-
val classDef: ClassDef = ClassDef(clsSymbol, List(TypeTree.of[Object]), List(TypeDef(clsSymbol.typeMember("mytype"))))
42+
val clsSymbol = Symbol.newClass(Symbol.spliceOwner, "CLS", List(TypeRepr.of[Object]), sym => makeTypesForClass(sym), None)
43+
val classDef: ClassDef = ClassDef(clsSymbol, List(TypeTree.of[Object]), List(
44+
TypeDef(clsSymbol.typeMember("tpe")),
45+
TypeDef(clsSymbol.typeMember("tpe1")),
46+
TypeDef(clsSymbol.typeMember("tpe2")),
47+
))
2148
Block(List(classDef), Apply(Select(New(TypeIdent(clsSymbol)), clsSymbol.primaryConstructor), List.empty)).asExprOf[Object]
2249
}

tests/pos-macros/quote-sym-newtype-in-trait/Macro_1.scala

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,28 @@ transparent inline def transparentTestMacro = ${ testImpl }
88
def testImpl(using Quotes): Expr[Object] = {
99
import quotes.reflect.*
1010

11-
def makeType(owner: Symbol): Symbol =
12-
Symbol.newTypeAlias(owner, "mytype", Flags.EmptyFlags, TypeRepr.of[String], Symbol.noSymbol)
11+
def makeBasicType(owner: Symbol): Symbol =
12+
Symbol.newTypeAlias(owner, "tpe", Flags.EmptyFlags, TypeRepr.of[String], Symbol.noSymbol)
1313

14-
val clsSymbol = Symbol.newClass(Symbol.spliceOwner, "CLS", List(TypeRepr.of[Object]), sym => List(makeType(sym)), None)
15-
val classDef: ClassDef = ClassDef(clsSymbol, List(TypeTree.of[Object]), List(TypeDef(clsSymbol.typeMember("mytype"))))
14+
def makeTypesForClass(owner: Symbol): List[Symbol] =
15+
val typeLambda = TypeLambda.apply(List("X"), _ => List(TypeBounds.empty), _ => TypeRepr.of[Int])
16+
List(
17+
makeBasicType(owner),
18+
// type Foo = [X] =>> Int
19+
Symbol.newTypeAlias(
20+
owner,
21+
"tpe1",
22+
Flags.EmptyFlags,
23+
typeLambda,
24+
Symbol.noSymbol
25+
),
26+
)
27+
28+
val clsSymbol = Symbol.newClass(Symbol.spliceOwner, "CLS", List(TypeRepr.of[Object]), sym => makeTypesForClass(sym), None)
29+
val classDef: ClassDef = ClassDef(clsSymbol, List(TypeTree.of[Object]), List(
30+
TypeDef(clsSymbol.typeMember("tpe")),
31+
TypeDef(clsSymbol.typeMember("tpe1")),
32+
))
1633

1734
Block(List(classDef), Apply(Select(New(TypeIdent(clsSymbol)), clsSymbol.primaryConstructor), List.empty)).asExprOf[Object]
1835
}

0 commit comments

Comments
 (0)