|
| 1 | +package dotty.tools.dotc.transform |
| 2 | + |
| 3 | +import dotty.tools.dotc.ast.tpd |
| 4 | +import dotty.tools.dotc.core.Contexts.Context |
| 5 | +import dotty.tools.dotc.core.Flags._ |
| 6 | +import dotty.tools.dotc.core.Symbols.Symbol |
| 7 | +import dotty.tools.dotc.core.Types.{Type, ExprType} |
| 8 | +import dotty.tools.dotc.transform.MegaPhase.MiniPhase |
| 9 | +import dotty.tools.dotc.transform.SymUtils._ |
| 10 | + |
| 11 | +import scala.collection.JavaConverters._ |
| 12 | +import scala.collection.mutable |
| 13 | + |
| 14 | +import java.util.IdentityHashMap |
| 15 | + |
| 16 | +object CollectNullableFields { |
| 17 | + val name = "collectNullableFields" |
| 18 | +} |
| 19 | + |
| 20 | +/** Collect fields that can be nulled out after use in lazy initialization. |
| 21 | + * |
| 22 | + * This information is used during lazy val transformation to assign null to private |
| 23 | + * fields that are only used within a lazy val initializer. This is not just an optimization, |
| 24 | + * but is needed for correctness to prevent memory leaks. E.g. |
| 25 | + * |
| 26 | + * ```scala |
| 27 | + * class TestByNameLazy(byNameMsg: => String) { |
| 28 | + * lazy val byLazyValMsg = byNameMsg |
| 29 | + * } |
| 30 | + * ``` |
| 31 | + * |
| 32 | + * Here `byNameMsg` should be null out once `byLazyValMsg` is |
| 33 | + * initialised. |
| 34 | + * |
| 35 | + * A field is nullable if all the conditions below hold: |
| 36 | + * - belongs to a non trait-class |
| 37 | + * - is private[this] |
| 38 | + * - is not lazy |
| 39 | + * - its type is nullable |
| 40 | + * - is only used in a lazy val initializer |
| 41 | + * - defined in the same class as the lazy val |
| 42 | + */ |
| 43 | +class CollectNullableFields extends MiniPhase { |
| 44 | + import tpd._ |
| 45 | + |
| 46 | + override def phaseName = CollectNullableFields.name |
| 47 | + |
| 48 | + /** Running after `ElimByName` to see by names as nullable types. */ |
| 49 | + override def runsAfter = Set(ElimByName.name) |
| 50 | + |
| 51 | + private[this] sealed trait FieldInfo |
| 52 | + private[this] case object NotNullable extends FieldInfo |
| 53 | + private[this] case class Nullable(by: Symbol) extends FieldInfo |
| 54 | + |
| 55 | + /** Whether or not a field is nullable */ |
| 56 | + private[this] var nullability: IdentityHashMap[Symbol, FieldInfo] = _ |
| 57 | + |
| 58 | + override def prepareForUnit(tree: Tree)(implicit ctx: Context) = { |
| 59 | + if (nullability == null) nullability = new IdentityHashMap |
| 60 | + ctx |
| 61 | + } |
| 62 | + |
| 63 | + private def recordUse(tree: Tree)(implicit ctx: Context): Tree = { |
| 64 | + val sym = tree.symbol |
| 65 | + val isNullablePrivateField = |
| 66 | + sym.isField && |
| 67 | + !sym.is(Lazy) && |
| 68 | + !sym.owner.is(Trait) && |
| 69 | + sym.initial.is(PrivateLocal) && |
| 70 | + sym.info.widenDealias.typeSymbol.isNullableClass |
| 71 | + |
| 72 | + if (isNullablePrivateField) |
| 73 | + nullability.get(sym) match { |
| 74 | + case Nullable(from) if from != ctx.owner => // used in multiple lazy val initializers |
| 75 | + nullability.put(sym, NotNullable) |
| 76 | + case null => // not in the map |
| 77 | + val from = ctx.owner |
| 78 | + val isNullable = |
| 79 | + from.is(Lazy, butNot = Module) && // is lazy val |
| 80 | + from.owner.isClass && // is field |
| 81 | + from.owner.eq(sym.owner) // is lazy val and field defined in the same class |
| 82 | + val info = if (isNullable) Nullable(from) else NotNullable |
| 83 | + nullability.put(sym, info) |
| 84 | + case _ => |
| 85 | + // Do nothing for: |
| 86 | + // - NotNullable |
| 87 | + // - Nullable(ctx.owner) |
| 88 | + } |
| 89 | + |
| 90 | + tree |
| 91 | + } |
| 92 | + |
| 93 | + override def transformIdent(tree: Ident)(implicit ctx: Context) = |
| 94 | + recordUse(tree) |
| 95 | + |
| 96 | + override def transformSelect(tree: Select)(implicit ctx: Context) = |
| 97 | + recordUse(tree) |
| 98 | + |
| 99 | + /** Map lazy values to the fields they should null after initialization. */ |
| 100 | + def lazyValNullables(implicit ctx: Context): IdentityHashMap[Symbol, mutable.ListBuffer[Symbol]] = { |
| 101 | + val result = new IdentityHashMap[Symbol, mutable.ListBuffer[Symbol]] |
| 102 | + |
| 103 | + nullability.forEach { |
| 104 | + case (sym, Nullable(from)) => |
| 105 | + val bldr = result.computeIfAbsent(from, _ => new mutable.ListBuffer) |
| 106 | + bldr += sym |
| 107 | + case _ => |
| 108 | + } |
| 109 | + |
| 110 | + result |
| 111 | + } |
| 112 | +} |
0 commit comments