Skip to content

Commit 580b4a2

Browse files
committed
Re-resolve ==, != after expanding opaque types
Resolve overloading of `==` and `!=` after expanding opaque types. If we do not do that, all comparisons of opaque types will go to object equals, which involves boxing.
1 parent 64a239f commit 580b4a2

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package dotty.tools.dotc
1+
package dotty.tools
2+
package dotc
23
package transform
34

45
import core._
@@ -12,13 +13,16 @@ import Denotations.{SingleDenotation, NonSymSingleDenotation}
1213
import SymDenotations.SymDenotation
1314
import DenotTransformers._
1415
import TypeUtils._
16+
import Names._
17+
import ast.Trees._
1518

1619
object ElimOpaque {
1720
val name: String = "elimOpaque"
1821
}
1922

2023
/** Rewrites opaque type aliases to normal alias types */
2124
class ElimOpaque extends MiniPhase with DenotTransformer {
25+
import ast.tpd._
2226

2327
override def phaseName: String = ElimOpaque.name
2428

@@ -52,4 +56,18 @@ class ElimOpaque extends MiniPhase with DenotTransformer {
5256
ref
5357
}
5458
}
59+
60+
/** Resolve overloading of `==` and `!=` methods with the representation
61+
* types in order to avoid boxing. TODO: This should be in the SLS.
62+
*/
63+
override def transformApply(tree: Apply)(using Context): Tree =
64+
val sym = tree.symbol
65+
if sym == defn.Any_== || sym == defn.Any_!= then
66+
tree match
67+
case Apply(Select(receiver, name: TermName), args) =>
68+
applyOverloaded(receiver, name, args, Nil, defn.BooleanType)
69+
case _ =>
70+
tree
71+
else
72+
tree
5573
}

tests/pos/opaque.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ object usesites {
3535
// as a contextual implicit this takes precedence over the
3636
// implicit scope implicit LogarithmOps.
3737
// TODO: Remove any2stringadd
38+
assert(l == Logarithm(1.0))
39+
assert(l != l2)
3840
val d = l3.toDouble
3941
val l5: Logarithm = (1.0).asInstanceOf[Logarithm]
4042
}

0 commit comments

Comments
 (0)