Skip to content

Commit 6b7dc95

Browse files
committed
Use NoSymbol instead of Options
1 parent a63758c commit 6b7dc95

File tree

6 files changed

+30
-38
lines changed

6 files changed

+30
-38
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,9 +1532,9 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
15321532
def Symbol_fields(self: Symbol)(given Context): List[Symbol] =
15331533
self.unforcedDecls.filter(isField)
15341534

1535-
def Symbol_field(self: Symbol)(name: String)(given Context): Option[Symbol] = {
1535+
def Symbol_field(self: Symbol)(name: String)(given Context): Symbol = {
15361536
val sym = self.unforcedDecls.find(sym => sym.name == name.toTermName)
1537-
if (sym.exists && isField(sym)) Some(sym) else None
1537+
if (isField(sym)) sym else core.Symbols.NoSymbol
15381538
}
15391539

15401540
def Symbol_classMethod(self: Symbol)(name: String)(given Context): List[DefDefSymbol] =
@@ -1568,11 +1568,6 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
15681568
case sym if sym.is(Flags.CaseAccessor) => sym.asTerm
15691569
}
15701570

1571-
def Symbol_companionModule(self: Symbol)(given Context): Option[ValDefSymbol] = {
1572-
val sym = self.companionModule
1573-
if (sym.exists) Some(sym.asTerm) else None
1574-
}
1575-
15761571
private def isField(sym: Symbol)(given Context): Boolean = sym.isTerm && !sym.is(Flags.Method)
15771572

15781573
def Symbol_of(fullName: String)(given ctx: Context): ClassDefSymbol = ctx.requiredClass(fullName)
@@ -1607,15 +1602,11 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
16071602
def isValDefSymbol(symbol: Symbol)(given Context): Boolean =
16081603
symbol.isTerm && !symbol.is(core.Flags.Method) && !symbol.is(core.Flags.Case)
16091604

1610-
def Symbol_moduleClass(self: Symbol)(given Context): Option[ClassDefSymbol] = {
1611-
val sym = self.moduleClass
1612-
if (sym.exists) Some(sym.asClass) else None
1613-
}
1605+
def Symbol_moduleClass(self: Symbol)(given Context): Symbol = self.moduleClass
16141606

1615-
def Symbol_companionClass(self: Symbol)(given Context): Option[ClassDefSymbol] = {
1616-
val sym = self.companionClass
1617-
if (sym.exists) Some(sym.asClass) else None
1618-
}
1607+
def Symbol_companionClass(self: Symbol)(given Context): Symbol = self.companionClass
1608+
1609+
def Symbol_companionModule(self: Symbol)(given Context): Symbol = self.companionModule
16191610

16201611
type BindSymbol = core.Symbols.TermSymbol
16211612

library/src/scala/tasty/reflect/CompilerInterface.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ trait CompilerInterface {
12671267
def Symbol_fields(self: Symbol)(given ctx: Context): List[Symbol]
12681268

12691269
/** Field with the given name directly declared in the class */
1270-
def Symbol_field(self: Symbol)(name: String)(given ctx: Context): Option[Symbol]
1270+
def Symbol_field(self: Symbol)(name: String)(given ctx: Context): Symbol
12711271

12721272
/** Get non-private named methods defined directly inside the class */
12731273
def Symbol_classMethod(self: Symbol)(name: String)(given ctx: Context): List[DefDefSymbol]
@@ -1284,9 +1284,6 @@ trait CompilerInterface {
12841284
/** Fields of a case class type -- only the ones declared in primary constructor */
12851285
def Symbol_caseFields(self: Symbol)(given ctx: Context): List[ValDefSymbol]
12861286

1287-
/** The symbol of the companion module */
1288-
def Symbol_companionModule(self: Symbol)(given ctx: Context): Option[ValDefSymbol]
1289-
12901287
def Symbol_of(fullName: String)(given ctx: Context): ClassDefSymbol
12911288

12921289
/** Symbol of a type (parameter or member) definition. */
@@ -1319,9 +1316,13 @@ trait CompilerInterface {
13191316
def isValDefSymbol(symbol: Symbol)(given ctx: Context): Boolean
13201317

13211318
/** The class symbol of the companion module class */
1322-
def Symbol_moduleClass(self: Symbol)(given ctx: Context): Option[ClassDefSymbol]
1319+
def Symbol_moduleClass(self: Symbol)(given ctx: Context): Symbol
13231320

1324-
def Symbol_companionClass(self: Symbol)(given ctx: Context): Option[ClassDefSymbol]
1321+
/** The symbol of the companion class */
1322+
def Symbol_companionClass(self: Symbol)(given ctx: Context): Symbol
1323+
1324+
/** The symbol of the companion module */
1325+
def Symbol_companionModule(self: Symbol)(given ctx: Context): Symbol
13251326

13261327
/** Symbol representing a bind definition. */
13271328
type BindSymbol <: TermSymbol

library/src/scala/tasty/reflect/SymbolOps.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ trait SymbolOps extends Core {
2929
/** This symbol is protected within the resulting type */
3030
def protectedWithin(given ctx: Context): Option[Type] = internal.Symbol_protectedWithin(self)
3131

32-
3332
/** The name of this symbol */
3433
def name(given ctx: Context): String = internal.Symbol_name(self)
3534

@@ -110,7 +109,7 @@ trait SymbolOps extends Core {
110109
internal.Symbol_fields(self)
111110

112111
/** Field with the given name directly declared in the class */
113-
def field(name: String)(given ctx: Context): Option[Symbol] =
112+
def field(name: String)(given ctx: Context): Symbol =
114113
internal.Symbol_field(self)(name)
115114

116115
/** Get non-private named methods defined directly inside the class */
@@ -133,10 +132,6 @@ trait SymbolOps extends Core {
133132
def caseFields(given ctx: Context): List[ValDefSymbol] =
134133
internal.Symbol_caseFields(self)
135134

136-
/** The symbol of the companion module */
137-
def companionModule(given ctx: Context): Option[ValDefSymbol] =
138-
internal.Symbol_companionModule(self)
139-
140135
def isTypeParam(given ctx: Context): Boolean =
141136
internal.Symbol_isTypeParam(self)
142137

@@ -145,11 +140,16 @@ trait SymbolOps extends Core {
145140
internal.Symbol_signature(self)
146141

147142
/** The class symbol of the companion module class */
148-
def moduleClass(given ctx: Context): Option[ClassDefSymbol] =
143+
def moduleClass(given ctx: Context): Symbol =
149144
internal.Symbol_moduleClass(self)
150145

151-
def companionClass(given ctx: Context): Option[ClassDefSymbol] =
146+
/** The symbol of the companion class */
147+
def companionClass(given ctx: Context): Symbol =
152148
internal.Symbol_companionClass(self)
149+
150+
/** The symbol of the companion module */
151+
def companionModule(given ctx: Context): Symbol =
152+
internal.Symbol_companionModule(self)
153153
}
154154

155155
}

tests/run-macros/gestalt-type-toolbox-reflect/Macro_1.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ object TypeToolbox {
4545
private def fieldInImpl(t: Type[_], mem: String)(given qctx: QuoteContext): Expr[String] = {
4646
import qctx.tasty._
4747
val field = t.unseal.symbol.asClassDef.field(mem)
48-
Expr(field.map(_.name).getOrElse(""))
48+
Expr(if field.isNoSymbol then "" else field.name)
4949
}
5050

5151
inline def fieldsIn[T]: Seq[String] = ${fieldsInImpl('[T])}
@@ -89,19 +89,19 @@ object TypeToolbox {
8989
inline def companion[T1, T2]: Boolean = ${companionImpl('[T1], '[T2])}
9090
private def companionImpl(t1: Type[_], t2: Type[_])(given qctx: QuoteContext): Expr[Boolean] = {
9191
import qctx.tasty._
92-
val res = t1.unseal.symbol.asClassDef.companionModule.contains(t2.unseal.symbol)
92+
val res = t1.unseal.symbol.asClassDef.companionModule == t2.unseal.symbol
9393
Expr(res)
9494
}
9595

9696
inline def companionName[T1]: String = ${companionNameImpl('[T1])}
9797
private def companionNameImpl(tp: Type[_])(given qctx: QuoteContext): Expr[String] = {
9898
import qctx.tasty._
9999
val sym = tp.unseal.symbol
100-
val companionClassOpt =
101-
if sym.isClassDef then sym.asClassDef.companionModule.getOrElse(Symbol.noSymbol).companionClass
102-
else if sym.isValDef then sym.asValDef.companionClass
103-
else None
104-
Expr(companionClassOpt.map(_.fullName).getOrElse(""))
100+
val companionClass =
101+
if sym.isClassDef then sym.companionModule.companionClass
102+
else if sym.isValDef then sym.companionClass
103+
else Symbol.noSymbol
104+
Expr(if companionClass.isNoSymbol then "" else companionClass.fullName)
105105
}
106106

107107
}

tests/run-with-compiler-custom-args/tasty-interpreter/interpreter/jvm/Interpreter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Interpreter[R <: Reflection & Singleton](reflect0: R) extends TreeInterpre
7373
jvmReflection.interpretStaticVal(fn.symbol.owner, fn.symbol)
7474
case _ =>
7575
if (fn.symbol.flags.is(Flags.Object))
76-
jvmReflection.loadModule(fn.symbol.asValDef.moduleClass.get)
76+
jvmReflection.loadModule(fn.symbol.asValDef.moduleClass)
7777
else
7878
jvmReflection.interpretStaticVal(fn.symbol.owner, fn.symbol)
7979
}

0 commit comments

Comments
 (0)