Skip to content

Commit 55c70d6

Browse files
committed
Restrict re-resolution of equality to opaque types
Also, document what is done in the doc page for opaque types.
1 parent 580b4a2 commit 55c70d6

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ object ElimOpaque {
2222

2323
/** Rewrites opaque type aliases to normal alias types */
2424
class ElimOpaque extends MiniPhase with DenotTransformer {
25+
thisPhase =>
2526
import ast.tpd._
2627

2728
override def phaseName: String = ElimOpaque.name
@@ -58,13 +59,14 @@ class ElimOpaque extends MiniPhase with DenotTransformer {
5859
}
5960

6061
/** Resolve overloading of `==` and `!=` methods with the representation
61-
* types in order to avoid boxing. TODO: This should be in the SLS.
62+
* types in order to avoid boxing.
6263
*/
6364
override def transformApply(tree: Apply)(using Context): Tree =
6465
val sym = tree.symbol
6566
if sym == defn.Any_== || sym == defn.Any_!= then
6667
tree match
67-
case Apply(Select(receiver, name: TermName), args) =>
68+
case Apply(Select(receiver, name: TermName), args)
69+
if atPhase(thisPhase)(receiver.tpe.widen.typeSymbol.isOpaqueAlias) =>
6870
applyOverloaded(receiver, name, args, Nil, defn.BooleanType)
6971
case _ =>
7072
tree

docs/docs/reference/other-new-features/opaques-details.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ object o {
4646
def id(x: o.T): o.T = x
4747
```
4848

49+
### Translation of Equality
50+
51+
Comparing two values of opaque type with `==` or `!=` normally uses universal equality,
52+
unless another overloaded `==` or `!=` operator is defined for the type. To avoid
53+
boxing, the operation is mapped after type checking to the (in-)equality operator
54+
defined on the underlying type. For instance,
55+
```scala
56+
opaque type T = Int
57+
58+
...
59+
val x: T
60+
val y: T
61+
x == y // uses Int equality for the comparison.
62+
```
63+
4964
### Toplevel Opaque Types
5065

5166
An opaque type alias on the toplevel is transparent in all other toplevel definitions in the sourcefile where it appears, but is opaque in nested

0 commit comments

Comments
 (0)