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 1 commit
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
20 changes: 19 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,16 @@ 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 {
import ast.tpd._

override def phaseName: String = ElimOpaque.name

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

/** Resolve overloading of `==` and `!=` methods with the representation
* types in order to avoid boxing. TODO: This should be in the SLS.
*/
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) =>
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.

}
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]
}