Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-resolve ==, != after expanding opaque types #9583

Merged
merged 2 commits into from Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 21 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/ElimOpaque.scala
@@ -1,4 +1,5 @@
package dotty.tools.dotc
package dotty.tools
package dotc
package transform

import core._
Expand All @@ -12,13 +13,17 @@ import Denotations.{SingleDenotation, NonSymSingleDenotation}
import SymDenotations.SymDenotation
import DenotTransformers._
import TypeUtils._
import Names._
import ast.Trees._

object ElimOpaque {
val name: String = "elimOpaque"
}

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

override def phaseName: String = ElimOpaque.name

Expand Down Expand Up @@ -52,4 +57,19 @@ class ElimOpaque extends MiniPhase with DenotTransformer {
ref
}
}

/** Resolve overloading of `==` and `!=` methods with the representation
* types in order to avoid boxing.
*/
override def transformApply(tree: Apply)(using Context): Tree =
val sym = tree.symbol
if sym == defn.Any_== || sym == defn.Any_!= then
tree match
case Apply(Select(receiver, name: TermName), args)
if atPhase(thisPhase)(receiver.tpe.widen.typeSymbol.isOpaqueAlias) =>
applyOverloaded(receiver, name, args, Nil, defn.BooleanType)
case _ =>
tree
else
tree
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to restrict it to opaque types. Let's see how it impacts performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree.

}
15 changes: 15 additions & 0 deletions docs/docs/reference/other-new-features/opaques-details.md
Expand Up @@ -46,6 +46,21 @@ object o {
def id(x: o.T): o.T = x
```

### Translation of Equality

Comparing two values of opaque type with `==` or `!=` normally uses universal equality,
unless another overloaded `==` or `!=` operator is defined for the type. To avoid
boxing, the operation is mapped after type checking to the (in-)equality operator
defined on the underlying type. For instance,
```scala
opaque type T = Int

...
val x: T
val y: T
x == y // uses Int equality for the comparison.
```

### Toplevel Opaque Types

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
Expand Down
2 changes: 2 additions & 0 deletions tests/pos/opaque.scala
Expand Up @@ -35,6 +35,8 @@ object usesites {
// as a contextual implicit this takes precedence over the
// implicit scope implicit LogarithmOps.
// TODO: Remove any2stringadd
assert(l == Logarithm(1.0))
assert(l != l2)
val d = l3.toDouble
val l5: Logarithm = (1.0).asInstanceOf[Logarithm]
}