Skip to content

Commit b2a4898

Browse files
Add getDeclaringClass implementation to enums
1 parent 0470a2c commit b2a4898

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

compiler/src/dotty/tools/dotc/transform/SyntheticMethods.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
110110
case nme.name => _ => Literal(Constant("Hello World"))
111111
case nme.ordinal => _ => Literal(Constant(0))
112112
case nme.compareTo => vrefss => compareToBody(vrefss.head.head)
113-
case nme.getDeclaringClass => _ => Literal(Constant(null))
113+
case nme.getDeclaringClass => _ => getDeclaringClassBody
114114
}
115115
ctx.log(s"adding $synthetic to $clazz at ${ctx.phase}")
116116
DefDef(synthetic, syntheticRHS(ctx.withOwner(synthetic))).withSpan(ctx.owner.span.focus)
@@ -138,6 +138,21 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
138138
Apply(thisOrdinalCompareTo, otherOrdinalBoxed :: Nil)
139139
}
140140

141+
/** An enum case `X` in
142+
*
143+
* ```
144+
* enum Foo { case X }
145+
* ```
146+
*
147+
* gets the `getDeclaringClass` method:
148+
*
149+
* ```
150+
* def getDeclaringClass: Class[Foo] = Predef.classOf[Foo]
151+
* ```
152+
*/
153+
def getDeclaringClassBody: Tree =
154+
Ident(defn.Predef_classOfR).appliedToType(clazz.info.parents.head)
155+
141156
/** The class
142157
*
143158
* ```

0 commit comments

Comments
 (0)