8000 Merge pull request #9226 from lrytz/t12002b · scala/scala@8c86b7d · GitHub
[go: up one dir, main page]

Skip to content

Commit 8c86b7d

Browse files
authored
Merge pull request #9226 from lrytz/t12002b
Keep `private[this] var` class parameter fields that are only read
2 parents 4674f45 + 9a11180 commit 8c86b7d

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

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

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

@@ -192,28 +192,10 @@ abstract class Constructors extends Statics with Transform with TypingTransforme
192192
}
193193
}
194194
}
195-
class DetectAssigns(targets: mutable.Set[Symbol]) extends InternalTraverser {
196-
override def traverse(tree: Tree): Unit = if (targets.nonEmpty) {
197-
tree match {
198-
case Assign(lhs, _) if targets(lhs.symbol) =>
199-
targets -= lhs.symbol
200-
omittables -= lhs.symbol
201-
tree.traverse(this)
202-
case _ => tree.traverse(this)
203-
}
204-
}
205-
}
206195

207196
if (omittables.nonEmpty)
208197
(defs.iterator ++ auxConstructors.iterator) foreach detectUsages.traverse
209198

210-
if (omittables.nonEmpty) {
211-
val omittedVars = omittables.filter(_.isVariable)
212-
if (omittedVars.nonEmpty) {
213-
val detector = new DetectAssigns(omittedVars)
214-
constructor.foreach(detector.traverse)
215-
}
216-
}
217199
omittables.toSet
218200
}
219201
} // OmittablesHelper

test/files/run/t12002.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
11
good boy!
2+
List()
3+
List()
4+
List(private int C.x)
5+
List(private int D.x)
6+
List(private int E.x)
7+
List(private int F.x)
8+
List(private int G.x)
9+
List(private final int H.x)
10+
List(private int I.x)

test/files/run/t12002.scala

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
// cf t6880
2-
class C(private[this] var c: String) {
1+
class C0(private[this] var c: String) {
32
private[this] var x: String = _
43
c = "good"
54
x = c + " boy!"
65
override def toString = x
76
}
87

8+
class A(x: Int) { assert(x == 1) } // elided
9+
class B(private[this] val x: Int) { assert(x == 1) } // elided
10+
class C(private[this] var x: Int) { } // kept
11+
class D(private[this] var x: Int) { assert(x == 1) } // kept
12+
class E(private[this] var x: Int) { x = 2 } // kept
13+
class F(private[this] var x: Int) { x = 2; assert(x == 2) } // kept
14+
class G(private[this] var x: Int) { x = 2; assert(x == 2); def m() = assert(x == 2, "m" + x); m() } // kept
15+
class H(private val x: Int) { } // kept
16+
class I(private var x: Int) { } // kept
17+
918
object Test {
10-
def main(args: Array[String]) = println {
11-
new C("bad")
19+
def fs(o: AnyRef) = println(o.getClass.getDeclaredFields.toList)
20+
def main(args: Array[String]): Unit = {
21+
println(new C0("bad"))
22+
List(new A(1), new B(1), new C(1), new D(1), new E(1), new F(1), new G(1), new H(1), new I(1)).foreach(fs)
1223
}
1324
}
14-
15-
// was
16-
// java.lang.NoSuchFieldError: c
17-
// at C.<init>(t12002.scala:6)

0 commit comments

Comments
 (0)
0