Skip to content

Commit

Permalink
Address some review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jchyb committed Oct 8, 2024
1 parent 9db0a00 commit 5d06f96
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 12 deletions.
4 changes: 2 additions & 2 deletions compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2653,11 +2653,11 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
def newTypeAlias(owner: Symbol, name: String, flags: Flags, tpe: TypeRepr, privateWithin: Symbol): Symbol =
checkValidFlags(flags.toTypeFlags, Flags.validTypeAliasFlags)
assert(!tpe.isInstanceOf[Types.TypeBounds], "Passed `tpe` into newTypeAlias should not represent TypeBounds")
dotc.core.Symbols.newSymbol(owner, name.toTypeName, flags | dotc.core.Flags.Deferred, dotc.core.Types.TypeAlias(tpe), privateWithin)
dotc.core.Symbols.newSymbol(owner, name.toTypeName, flags, dotc.core.Types.TypeAlias(tpe), privateWithin)

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

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

Expand Down
47 changes: 47 additions & 0 deletions tests/neg-macros/quote-sym-newtype/Macro_1.scala
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]
}

6 changes: 6 additions & 0 deletions tests/neg-macros/quote-sym-newtype/Test_2.scala
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
39 changes: 33 additions & 6 deletions tests/pos-macros/quote-sym-newboundedtype/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,42 @@ transparent inline def transparentTestMacro = ${ testImpl }
def testImpl(using Quotes): Expr[Object] = {
import quotes.reflect.*

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

val typeDef = TypeDef(makeType(Symbol.spliceOwner))
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
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))])")
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 => List(makeType(sym)), None)
val classDef: ClassDef = ClassDef(clsSymbol, List(TypeTree.of[Object]), List(TypeDef(clsSymbol.typeMember("mytype"))))
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]
}
25 changes: 21 additions & 4 deletions tests/pos-macros/quote-sym-newtype-in-trait/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,28 @@ transparent inline def transparentTestMacro = ${ testImpl }
def testImpl(using Quotes): Expr[Object] = {
import quotes.reflect.*

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

val clsSymbol = Symbol.newClass(Symbol.spliceOwner, "CLS", List(TypeRepr.of[Object]), sym => List(makeType(sym)), None)
val classDef: ClassDef = ClassDef(clsSymbol, List(TypeTree.of[Object]), List(TypeDef(clsSymbol.typeMember("mytype"))))
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]
}

0 comments on commit 5d06f96

Please sign in to comment.