8000 [backport] Constructors handles private[this] var by som-snytt · Pull Request #9006 · scala/scala · GitHub
[go: up one dir, main page]

Skip to content

[backport] Constructors handles private[this] var #9006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
[backport] Constructors handles private[this] var
Backports PRs 9005, 7688.
  • Loading branch information
som-snytt committed May 23, 2020
commit 84360b57e306cdccd3a5a14a8e016ad9a6587f4c
25 changes: 22 additions & 3 deletions src/compiler/scala/tools/nsc/transform/Constructors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ abstract class Constructors extends Statics with Transform with TypingTransforme
*
*/
private trait OmittablesHelper {
def computeOmittableAccessors(clazz: Symbol, defs: List[Tree], auxConstructors: List[Tree]): Set[Symbol] = {
def computeOmittableAccessors(clazz: Symbol, defs: List[Tree], auxConstructors: List[Tree], constructor: List[Tree]): Set[Symbol] = {
val decls = clazz.info.decls.toSet
val isEffectivelyFinal = clazz.isEffectivelyFinal

Expand Down Expand Up @@ -191,10 +191,28 @@ abstract class Constructors extends Statics with Transform with TypingTransforme
}
}
}
class DetectAssigns(targets: mutable.Set[Symbol]) extends Traverser {
override def traverse(tree: Tree): Unit = if (targets.nonEmpty) {
tree match {
case Assign(lhs, _) if targets(lhs.symbol) =>
targets -= lhs.symbol
omittables -= lhs.symbol
super.traverse(tree)
case _ => super.traverse(tree)
}
}
}

if (omittables.nonEmpty)
< 8000 /td> (defs.iterator ++ auxConstructors.iterator) foreach detectUsages.traverse

if (omittables.nonEmpty) {
val omittedVars = omittables.filter(_.isVariable)
if (omittedVars.nonEmpty) {
val detector = new DetectAssigns(omittedVars)
constructor.foreach(detector.traverse)
}
}
omittables.toSet
}
} // OmittablesHelper
Expand Down Expand Up @@ -511,7 +529,8 @@ abstract class Constructors extends Statics with Transform with TypingTransforme
private def isStationaryParamRef(sym: Symbol) = (
isParamRef(sym) &&
!(sym.isGetter && sym.accessed.isVariable) &&
!sym.isSetter
!sym.isSetter &&
!sym.isVariable
)

/*
Expand Down Expand Up @@ -703,7 +722,7 @@ abstract class Constructors extends Statics with Transform with TypingTransforme
// omit unused outers
val omittableAccessor: Set[Symbol] =
if (isDelayedInitSubclass) Set.empty
else computeOmittableAccessors(clazz, defs, auxConstructors)
else computeOmittableAccessors(clazz, defs, auxConstructors, constructorStats)

// TODO: this should omit fields for non-memoized (constant-typed, unit-typed vals need no storage --
// all the action is in the getter)
Expand Down
1 change: 1 addition & 0 deletions test/files/run/t12002.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
good boy!
12 changes: 12 additions & 0 deletions test/files/run/t12002.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class C(private[this] var c: String) {
private[this] var x: String = _
c = "good"
x = c + " boy!"
override def toString = x
}

object Test {
def main(args: Array[String]) = println {
new C("bad")
}
}
0