Skip to content

Commit 906ad2a

Browse files
committed
Fix module class logic (now works as in compiler)
1 parent c5b986e commit 906ad2a

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with CoreImpl {
154154
if (sym.exists) Some(sym.asClass) else None
155155
}
156156

157+
def companionModule(implicit ctx: Context): Option[ValSymbol] = {
158+
val sym = symbol.companionModule
159+
if (sym.exists) Some(sym.asTerm) else None
160+
}
161+
157162
private def isField(sym: Symbol)(implicit ctx: Context): Boolean = sym.isTerm && !sym.is(Flags.Method)
158163
}
159164

@@ -173,6 +178,11 @@ trait SymbolOpsImpl extends scala.tasty.reflect.SymbolOps with CoreImpl {
173178

174179
def ValSymbolDeco(symbol: ValSymbol): ValSymbolAPI = new ValSymbolAPI {
175180
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+
}
176186
}
177187

178188
object IsBindSymbol extends IsBindSymbolExtractor {

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ trait TypeOrBoundsTreesOpsImpl extends scala.tasty.reflect.TypeOrBoundsTreeOps w
1919
def TypeTreeDeco(tpt: TypeTree): TypeTreeAPI = new TypeTreeAPI {
2020
def pos(implicit ctx: Context): Position = tpt.pos
2121
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
2623
}
2724

2825
object IsTypeTree extends IsTypeTreeExtractor {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ trait SymbolOps extends Core {
8585
/** Fields of a case class type -- only the ones declared in primary constructor */
8686
def caseFields(implicit ctx: Context): List[ValSymbol]
8787

88+
/** The class symbol of the companion module class */
8889
def companionClass(implicit ctx: Context): Option[ClassSymbol]
90+
91+
/** The symbol of the companion module */
92+
def companionModule(implicit ctx: Context): Option[ValSymbol]
93+
8994
}
9095
implicit def ClassSymbolDeco(symbol: ClassSymbol): ClassSymbolAPI
9196

@@ -122,6 +127,10 @@ trait SymbolOps extends Core {
122127

123128
trait ValSymbolAPI {
124129
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+
125134
}
126135
implicit def ValSymbolDeco(symbol: ValSymbol): ValSymbolAPI
127136

tests/run-separate-compilation/gestalt-type-toolbox-reflect/Macro_1.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,19 @@ object TypeToolbox {
9696
inline def companion[T1, T2]: Boolean = ~companionImpl('[T1], '[T2])
9797
private def companionImpl(t1: Type[_], t2: Type[_])(implicit reflect: Reflection): Expr[Boolean] = {
9898
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)
100100
res.toExpr
101101
}
102102

103103
inline def companionName[T1]: String = ~companionNameImpl('[T1])
104104
private def companionNameImpl(tp: Type[_])(implicit reflect: Reflection): Expr[String] = {
105105
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
109110
}
111+
companionClassOpt.map(_.fullName).getOrElse("").toExpr
110112
}
111113

112114
// TODO add to the std lib

0 commit comments

Comments
 (0)