@@ -1135,10 +1135,20 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
1135
1135
/** Inline vals */
1136
1136
val devalify : Optimization = { implicit ctx : Context =>
1137
1137
val timesUsed = mutable.HashMap [Symbol , Int ]()
1138
+ val timesUsedAsType = mutable.HashMap [Symbol , Int ]()
1139
+
1138
1140
val defined = mutable.HashSet [Symbol ]()
1139
1141
val usedInInnerClass = mutable.HashMap [Symbol , Int ]()
1140
1142
// Either a duplicate or a read through series of immutable fields
1141
1143
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
+ }
1142
1152
def doVisit (tree : Tree , used : mutable.HashMap [Symbol , Int ]): Unit = tree match {
1143
1153
case valdef : ValDef if ! valdef.symbol.is(Param | Mutable | Module | Lazy ) &&
1144
1154
valdef.symbol.exists && ! valdef.symbol.owner.isClass =>
@@ -1149,6 +1159,7 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
1149
1159
copies.put(valdef.symbol, valdef.rhs)
1150
1160
case _ =>
1151
1161
}
1162
+ visitType(valdef.symbol.info)
1152
1163
case t : New =>
1153
1164
val symIfExists = t.tpt.tpe.normalizedPrefix.termSymbol
1154
1165
val b4 = used.getOrElseUpdate(symIfExists, 0 )
@@ -1159,6 +1170,11 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
1159
1170
// TODO: handle params after constructors. Start changing public signatures by eliminating unused arguments.
1160
1171
defined += valdef.symbol
1161
1172
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))
1162
1178
case t : RefTree =>
1163
1179
val b4 = used.getOrElseUpdate(t.symbol, 0 )
1164
1180
used.put(t.symbol, b4 + 1 )
@@ -1191,19 +1207,19 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
1191
1207
}
1192
1208
1193
1209
val transformer : Transformer = () => localCtx => {
1194
- val valsToDrop = defined -- timesUsed.keySet
1210
+ val valsToDrop = defined -- timesUsed.keySet -- timesUsedAsType.keySet
1195
1211
val copiesToReplaceAsDuplicates = copies.filter { x =>
1196
1212
val rhs = dropCasts(x._2)
1197
1213
rhs.isInstanceOf [Literal ] || (! rhs.symbol.owner.isClass && ! rhs.symbol.is(Method | Mutable ))
1198
- }
1214
+ } -- timesUsedAsType.keySet
1199
1215
// TODO: if a non-synthetic val is duplicate of a synthetic one, rename a synthetic one and drop synthetic flag?
1200
1216
1201
1217
val copiesToReplaceAsUsedOnce =
1202
1218
timesUsed.filter(x => x._2 == 1 ).
1203
1219
flatMap(x => copies.get(x._1) match {
1204
1220
case Some (tr) => List ((x._1, tr))
1205
1221
case None => Nil
1206
- })
1222
+ }) -- timesUsedAsType.keySet
1207
1223
1208
1224
val replacements = copiesToReplaceAsDuplicates ++ copiesToReplaceAsUsedOnce -- usedInInnerClass.keySet
1209
1225
@@ -1249,7 +1265,7 @@ class Simplify extends MiniPhaseTransform with IdentityDenotTransformer {
1249
1265
transformation
1250
1266
}
1251
1267
// 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)
1253
1269
}
1254
1270
1255
1271
/** Inline val with exactly one assignment to a var. For example:
0 commit comments