Skip to content

Commit e79b6ef

Browse files
committed
Never elide fields for private[this] var class parameters
After the previous commit, the field for a `private[this] var` class parameter is only elided if the field is not used at all. This seems not to be worth the effort.
1 parent a93ebdd commit e79b6ef

File tree

3 files changed

+3
-25
lines changed

3 files changed

+3
-25
lines changed

src/compiler/scala/tools/nsc/transform/Constructors.scala

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ abstract class Constructors extends Statics with Transform with TypingTransforme
171171
// class Outer { def test = {class LocalParent; class LocalChild extends LocalParent } }
172172
//
173173
// See run/t9408.scala for related test cases.
174-
def omittableParamAcc(sym: Symbol) = sym.isParamAccessor && sym.isPrivateLocal
174+
def omittableParamAcc(sym: Symbol) = sym.isParamAccessor && sym.isPrivateLocal && !sym.isVariable
175175
def omittableOuterAcc(sym: Symbol) = isEffectivelyFinal && sym.isOuterAccessor && !sym.isOverridingSymbol
176176
val omittables = mutable.Set.empty[Symbol] ++ (decls filter (sym => omittableParamAcc(sym) || omittableOuterAcc(sym))) // the closure only captures isEffectivelyFinal
177177

@@ -192,31 +192,9 @@ abstract class Constructors extends Statics with Transform with TypingTransforme
192192
}
193193
}
194194

195-
class DetectVarUses(targets: mutable.Set[Symbol]) extends Traverser {
196-
override def traverse(tree: Tree): Unit = if (targets.nonEmpty) {
197-
def mark(sym: Symbol): Unit = {
198-
targets -= sym
199-
omittables -= sym
200-
}
201-
tree match {
202-
case _: Select if targets(tree.symbol) => mark(tree.symbol)
203-
case Assign(lhs, _) if targets(lhs.symbol) => mark(lhs.symbol)
204-
case _ =>
205-
}
206-
super.traverse(tree)
207-
}
208-
}
209-
210195
if (omittables.nonEmpty)
211196
(defs.iterator ++ auxConstructors.iterator) foreach detectUsages.traverse
212197

213-
if (omittables.nonEmpty) {
214-
val omittedVars = omittables.filter(_.isVariable)
215-
if (omittedVars.nonEmpty) {
216-
val detector = new DetectVarUses(omittedVars)
217-
constructor.foreach(detector.traverse)
218-
}
219-
}
220198
omittables.toSet
221199
}
222200
} // OmittablesHelper

test/files/run/t12002.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
good boy!
22
List()
33
List()
4-
List()
4+
List(private int C.x)
55
List(private int D.x)
66
List(private int E.x)
77
List(private int F.x)

test/files/run/t12002.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class C0(private[this] var c: String) {
77

88
class A(x: Int) { assert(x == 1) } // elided
99
class B(private[this] val x: Int) { assert(x == 1) } // elided
10-
class C(private[this] var x: Int) { } // elided
10+
class C(private[this] var x: Int) { } // kept
1111
class D(private[this] var x: Int) { assert(x == 1) } // kept
1212
class E(private[this] var x: Int) { x = 2 } // kept
1313
class F(private[this] var x: Int) { x = 2; assert(x == 2) } // kept

0 commit comments

Comments
 (0)