Skip to content

Commit 9c68552

Browse files
committed
Simplify: run devalify before erasure.
1 parent 8220305 commit 9c68552

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,10 +1135,20 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
11351135
/** Inline vals */
11361136
val devalify: Optimization = { implicit ctx: Context =>
11371137
val timesUsed = mutable.HashMap[Symbol, Int]()
1138+
val timesUsedAsType = mutable.HashMap[Symbol, Int]()
1139+
11381140
val defined = mutable.HashSet[Symbol]()
11391141
val usedInInnerClass = mutable.HashMap[Symbol, Int]()
11401142
// Either a duplicate or a read through series of immutable fields
11411143
val copies = mutable.HashMap[Symbol, Tree]()
1144+
def visitType(tp: Type): Unit = {
1145+
tp.foreachPart(x => x match {
1146+
case TermRef(NoPrefix, _) =>
1147+
val b4 = timesUsedAsType.getOrElseUpdate(x.termSymbol, 0)
1148+
timesUsedAsType.put(x.termSymbol, b4 + 1)
1149+
case _ =>
1150+
})
1151+
}
11421152
def doVisit(tree: Tree, used: mutable.HashMap[Symbol, Int]): Unit = tree match {
11431153
case valdef: ValDef if !valdef.symbol.is(Param | Mutable | Module | Lazy) &&
11441154
valdef.symbol.exists && !valdef.symbol.owner.isClass =>
@@ -1149,6 +1159,7 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
11491159
copies.put(valdef.symbol, valdef.rhs)
11501160
case _ =>
11511161
}
1162+
visitType(valdef.symbol.info)
11521163
case t: New =>
11531164
val symIfExists = t.tpt.tpe.normalizedPrefix.termSymbol
11541165
val b4 = used.getOrElseUpdate(symIfExists, 0)
@@ -1159,6 +1170,11 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
11591170
// TODO: handle params after constructors. Start changing public signatures by eliminating unused arguments.
11601171
defined += valdef.symbol
11611172

1173+
case valdef: ValDef => visitType(valdef.symbol.info)
1174+
case t: DefDef => visitType(t.symbol.info)
1175+
case t: Typed =>
1176+
visitType(t.tpt.tpe)
1177+
case t: TypeApply => t.args.foreach(x => visitType(x.tpe))
11621178
case t: RefTree =>
11631179
val b4 = used.getOrElseUpdate(t.symbol, 0)
11641180
used.put(t.symbol, b4 + 1)
@@ -1191,19 +1207,19 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
11911207
}
11921208

11931209
val transformer: Transformer = () => localCtx => {
1194-
val valsToDrop = defined -- timesUsed.keySet
1210+
val valsToDrop = defined -- timesUsed.keySet -- timesUsedAsType.keySet
11951211
val copiesToReplaceAsDuplicates = copies.filter { x =>
11961212
val rhs = dropCasts(x._2)
11971213
rhs.isInstanceOf[Literal] || (!rhs.symbol.owner.isClass && !rhs.symbol.is(Method | Mutable))
1198-
}
1214+
} -- timesUsedAsType.keySet
11991215
// TODO: if a non-synthetic val is duplicate of a synthetic one, rename a synthetic one and drop synthetic flag?
12001216

12011217
val copiesToReplaceAsUsedOnce =
12021218
timesUsed.filter(x => x._2 == 1).
12031219
flatMap(x => copies.get(x._1) match {
12041220
case Some(tr) => List((x._1, tr))
12051221
case None => Nil
1206-
})
1222+
}) -- timesUsedAsType.keySet
12071223

12081224
val replacements = copiesToReplaceAsDuplicates ++ copiesToReplaceAsUsedOnce -- usedInInnerClass.keySet
12091225

@@ -1249,7 +1265,7 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
12491265
transformation
12501266
}
12511267
// See tests/pos/devalify.scala for examples of why this needs to be after Erasure.
1252-
("devalify", AfterErasure, visitor, transformer)
1268+
("devalify", BeforeAndAfterErasure, visitor, transformer)
12531269
}
12541270

12551271
/** Inline val with exactly one assignment to a var. For example:

0 commit comments

Comments
 (0)