Skip to content

Commit de1d12e

Browse files
Merge pull request #3150 from dotty-staging/optimise-boxed-units
Optimise away erased phantom statements
2 parents 2b9d346 + 4751d92 commit de1d12e

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

compiler/src/dotty/tools/dotc/core/PhantomErasure.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools.dotc.core
22

33
import dotty.tools.dotc.ast.tpd._
44
import dotty.tools.dotc.core.Contexts.Context
5-
import dotty.tools.dotc.core.Symbols.defn
5+
import dotty.tools.dotc.core.Symbols._
66
import dotty.tools.dotc.core.Types.Type
77

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

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

compiler/src/dotty/tools/dotc/transform/localopt/Simplify.scala

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import core.NameOps._
1111
import transform.TreeTransforms.{MiniPhaseTransform, TransformerInfo}
1212
import config.Printers.simplify
1313
import ast.tpd
14+
import dotty.tools.dotc.core.PhantomErasure
1415

1516
import scala.annotation.tailrec
1617

@@ -175,13 +176,16 @@ object Simplify {
175176
}
176177

177178
def isImmutableAccessor(t: Tree)(implicit ctx: Context): Boolean = {
178-
val isImmutableGetter = t.symbol.isGetter && !t.symbol.is(Mutable | Lazy)
179-
val isCaseAccessor = t.symbol.is(CaseAccessor) && !t.symbol.is(Mutable | Lazy)
180-
val isProductAccessor = t.symbol.exists &&
181-
t.symbol.owner.derivesFrom(defn.ProductClass) &&
182-
t.symbol.owner.is(CaseClass) &&
183-
t.symbol.name.isSelectorName &&
184-
!t.symbol.info.decls.exists(_.is(Mutable | Lazy)) // Conservatively covers case class A(var x: Int)
185-
isImmutableGetter || isCaseAccessor || isProductAccessor
179+
val sym = t.symbol
180+
val isImmutableGetter = sym.isGetter && !sym.is(Mutable | Lazy)
181+
val isCaseAccessor = sym.is(CaseAccessor) && !sym.is(Mutable | Lazy)
182+
val isProductAccessor = sym.exists &&
183+
sym.owner.derivesFrom(defn.ProductClass) &&
184+
sym.owner.is(CaseClass) &&
185+
sym.name.isSelectorName &&
186+
!sym.info.decls.exists(_.is(Mutable | Lazy)) // Conservatively covers case class A(var x: Int)
187+
val isErasedPhantom = PhantomErasure.isErasedPhantom(sym)
188+
189+
isImmutableGetter || isCaseAccessor || isProductAccessor || isErasedPhantom
186190
}
187191
}

0 commit comments

Comments
 (0)