Skip to content

Optimise away erased phantom statements #3150

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

Merged
merged 1 commit into from
Sep 27, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/core/PhantomErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package dotty.tools.dotc.core

import dotty.tools.dotc.ast.tpd._
import dotty.tools.dotc.core.Contexts.Context
import dotty.tools.dotc.core.Symbols.defn
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.Types.Type

/** Phantom erasure erases:
Expand All @@ -27,4 +27,7 @@ object PhantomErasure {
/** Returns the default erased tree for a phantom parameter ref */
def erasedParameterRef(implicit ctx: Context): Tree = ref(defn.ErasedPhantom_UNIT)

/** Is it a pure term inserted by the phantom erasure? */
def isErasedPhantom(sym: Symbol)(implicit ctx: Context): Boolean = sym eq defn.ErasedPhantom_UNIT

}
20 changes: 12 additions & 8 deletions compiler/src/dotty/tools/dotc/transform/localopt/Simplify.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import core.NameOps._
import transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo}
import config.Printers.simplify
import ast.tpd
import dotty.tools.dotc.core.PhantomErasure

import scala.annotation.tailrec

Expand Down Expand Up @@ -175,13 +176,16 @@ object Simplify {
}

def isImmutableAccessor(t: Tree)(implicit ctx: Context): Boolean = {
val isImmutableGetter = t.symbol.isGetter && !t.symbol.is(Mutable | Lazy)
val isCaseAccessor = t.symbol.is(CaseAccessor) && !t.symbol.is(Mutable | Lazy)
val isProductAccessor = t.symbol.exists &&
t.symbol.owner.derivesFrom(defn.ProductClass) &&
t.symbol.owner.is(CaseClass) &&
t.symbol.name.isSelectorName &&
!t.symbol.info.decls.exists(_.is(Mutable | Lazy)) // Conservatively covers case class A(var x: Int)
isImmutableGetter || isCaseAccessor || isProductAccessor
val sym = t.symbol
val isImmutableGetter = sym.isGetter && !sym.is(Mutable | Lazy)
val isCaseAccessor = sym.is(CaseAccessor) && !sym.is(Mutable | Lazy)
val isProductAccessor = sym.exists &&
sym.owner.derivesFrom(defn.ProductClass) &&
sym.owner.is(CaseClass) &&
sym.name.isSelectorName &&
!sym.info.decls.exists(_.is(Mutable | Lazy)) // Conservatively covers case class A(var x: Int)
val isErasedPhantom = PhantomErasure.isErasedPhantom(sym)

isImmutableGetter || isCaseAccessor || isProductAccessor || isErasedPhantom
}
}