File tree 4 files changed +26
-8
lines changed
compiler/src/dotty/tools/dotc/tastyreflect
library/src/scala/tasty/reflect
tests/run-separate-compilation/gestalt-type-toolbox-reflect
4 files changed +26
-8
lines changed Original file line number Diff line number Diff line change @@ -154,6 +154,11 @@ trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with CoreImpl {
154
154
if (sym.exists) Some (sym.asClass) else None
155
155
}
156
156
157
+ def companionModule (implicit ctx : Context ): Option [ValSymbol ] = {
158
+ val sym = symbol.companionModule
159
+ if (sym.exists) Some (sym.asTerm) else None
160
+ }
161
+
157
162
private def isField (sym : Symbol )(implicit ctx : Context ): Boolean = sym.isTerm && ! sym.is(Flags .Method )
158
163
}
159
164
@@ -173,6 +178,11 @@ trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with CoreImpl {
173
178
174
179
def ValSymbolDeco (symbol : ValSymbol ): ValSymbolAPI = new ValSymbolAPI {
175
180
def tree (implicit ctx : Context ): ValDef = FromSymbol .valDefFromSym(symbol)
181
+
182
+ def companionClass (implicit ctx : Context ): Option [ClassSymbol ] = {
183
+ val sym = symbol.companionClass
184
+ if (sym.exists) Some (sym.asClass) else None
185
+ }
176
186
}
177
187
178
188
object IsBindSymbol extends IsBindSymbolExtractor {
Original file line number Diff line number Diff line change @@ -19,10 +19,7 @@ trait TypeOrBoundsTreesOpsImpl extends scala.tasty.reflect.TypeOrBoundsTreeOps w
19
19
def TypeTreeDeco (tpt : TypeTree ): TypeTreeAPI = new TypeTreeAPI {
20
20
def pos (implicit ctx : Context ): Position = tpt.pos
21
21
def tpe (implicit ctx : Context ): Type = tpt.tpe.stripTypeVar
22
- def symbol (implicit ctx : Context ): Symbol = {
23
- val sym = tpt.symbol
24
- if (sym.isType) sym else sym.companionClass
25
- }
22
+ def symbol (implicit ctx : Context ): Symbol = tpt.symbol
26
23
}
27
24
28
25
object IsTypeTree extends IsTypeTreeExtractor {
Original file line number Diff line number Diff line change @@ -85,7 +85,12 @@ trait SymbolOps extends Core {
85
85
/** Fields of a case class type -- only the ones declared in primary constructor */
86
86
def caseFields (implicit ctx : Context ): List [ValSymbol ]
87
87
88
+ /** The class symbol of the companion module class */
88
89
def companionClass (implicit ctx : Context ): Option [ClassSymbol ]
90
+
91
+ /** The symbol of the companion module */
92
+ def companionModule (implicit ctx : Context ): Option [ValSymbol ]
93
+
89
94
}
90
95
implicit def ClassSymbolDeco (symbol : ClassSymbol ): ClassSymbolAPI
91
96
@@ -122,6 +127,10 @@ trait SymbolOps extends Core {
122
127
123
128
trait ValSymbolAPI {
124
129
def tree (implicit ctx : Context ): ValDef
130
+
131
+ /** The class symbol of the companion module class */
132
+ def companionClass (implicit ctx : Context ): Option [ClassSymbol ]
133
+
125
134
}
126
135
implicit def ValSymbolDeco (symbol : ValSymbol ): ValSymbolAPI
127
136
Original file line number Diff line number Diff line change @@ -96,17 +96,19 @@ object TypeToolbox {
96
96
inline def companion [T1 , T2 ]: Boolean = ~ companionImpl(' [T1 ], ' [T2 ])
97
97
private def companionImpl (t1 : Type [_], t2 : Type [_])(implicit reflect : Reflection ): Expr [Boolean ] = {
98
98
import reflect ._
99
- val res = t1.reflect.symbol.asClass.companionClass .contains(t2.reflect.symbol)
99
+ val res = t1.reflect.symbol.asClass.companionModule .contains(t2.reflect.symbol)
100
100
res.toExpr
101
101
}
102
102
103
103
inline def companionName [T1 ]: String = ~ companionNameImpl(' [T1 ])
104
104
private def companionNameImpl (tp : Type [_])(implicit reflect : Reflection ): Expr [String ] = {
105
105
import reflect ._
106
- tp.reflect.symbol match {
107
- case IsClassSymbol (sym) => sym.companionClass.map(_.fullName).getOrElse(" " ).toExpr
108
- case _ => '("")
106
+ val companionClassOpt = tp.reflect.symbol match {
107
+ case IsClassSymbol (sym) => sym.companionClass
108
+ case IsValSymbol (sym) => sym.companionClass
109
+ case _ => None
109
110
}
111
+ companionClassOpt.map(_.fullName).getOrElse(" " ).toExpr
110
112
}
111
113
112
114
// TODO add to the std lib
You can’t perform that action at this time.
0 commit comments