@@ -82,9 +82,6 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
82
82
83
83
def removeColors (msg : String ): String = msg.replaceAll(" \u001b\\ [.*?m" , " " )
84
84
def syntheticDef (sym : Symbol ): Tree = {
85
- // if (removeColors(sym.show) == "method getDeclaringClass")
86
- // println(s"Sym ${sym.show} info ${sym.denot.info.show} asf ${clazz.info.parents.head.show}: " +
87
- // s"${sym.denot.asSeenFrom(clazz.info.parents.head).info.show}")
88
85
val synthetic = sym
89
86
.copy(
90
87
owner = clazz,
@@ -112,13 +109,35 @@ class SyntheticMethods(thisPhase: DenotTransformer) {
112
109
// Enum Methods
113
110
case nme.name => _ => Literal (Constant (" Hello World" ))
114
111
case nme.ordinal => _ => Literal (Constant (0 ))
115
- case nme.compareTo => _ => Literal ( Constant ( 0 ) )
112
+ case nme.compareTo => vrefss => compareToBody(vrefss.head.head )
116
113
case nme.getDeclaringClass => _ => Literal (Constant (null ))
117
114
}
118
115
ctx.log(s " adding $synthetic to $clazz at ${ctx.phase}" )
119
116
DefDef (synthetic, syntheticRHS(ctx.withOwner(synthetic))).withSpan(ctx.owner.span.focus)
120
117
}
121
118
119
+ /** An enum case `X` in
120
+ *
121
+ * ```
122
+ * enum Foo { case X }
123
+ * ```
124
+ *
125
+ * gets the `compareTo` method:
126
+ *
127
+ * ```
128
+ * def compareTo(other: Foo): Int =
129
+ * new Integer(this).ordinal.compareTo(new Integer(other.ordinal))
130
+ * ```
131
+ */
132
+ def compareToBody (other : Tree ): Tree = {
133
+ val thisOrdinal = This (clazz).select(defn.Enum_ordinal )
134
+ val thisOrdinalBoxed = New (defn.BoxedIntType , thisOrdinal :: Nil )
135
+ val otherOrdinal = other.select(defn.Enum_ordinal )
136
+ val otherOrdinalBoxed = New (defn.BoxedIntType , otherOrdinal :: Nil )
137
+ val thisOrdinalCompareTo = thisOrdinalBoxed.select(defn.Comparable_compareTo )
138
+ Apply (thisOrdinalCompareTo, otherOrdinalBoxed :: Nil )
139
+ }
140
+
122
141
/** The class
123
142
*
124
143
* ```
0 commit comments