Skip to content

Fix #2772: Special case Devalify for java.lang.System.* #2781

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 7 commits into from
Jun 25, 2017
Merged
Changes from 1 commit
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
Diff view
33 changes: 18 additions & 15 deletions compiler/src/dotty/tools/dotc/transform/localopt/Devalify.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,17 @@ class Devalify extends Optimisation {
case _ => t
}

def readingOnlyVals(t: Tree)(implicit ctx: Context): Boolean = dropCasts(t) match {
def readingOnlyVals(t: Tree)(implicit ctx: Context): Boolean = {
def isGetterOfAImmutableField = t.symbol.isGetter && !t.symbol.is(Mutable)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of An ImmutableField

def isCaseClassWithVar = t.symbol.info.decls.exists(_.is(Mutable))
def isAccessingProductField = t.symbol.exists &&
t.symbol.owner.derivesFrom(defn.ProductClass) &&
t.symbol.owner.is(CaseClass) &&
t.symbol.name.isSelectorName &&
!isCaseClassWithVar // Conservatively covers case class A(var x: Int)
def isImmutableCaseAccessor = t.symbol.is(CaseAccessor) && !t.symbol.is(Mutable)

dropCasts(t) match {
case Typed(exp, _) => readingOnlyVals(exp)

case TypeApply(fun @ Select(rec, _), List(tp)) =>
Expand All @@ -173,29 +183,21 @@ class Devalify extends Optimisation {
else false

case Apply(Select(rec, _), Nil) =>
def isGetterOfAImmutableField = t.symbol.isGetter && !t.symbol.is(Mutable)
def isCaseClassWithVar = t.symbol.info.decls.exists(_.is(Mutable))
def isAccessingProductField = t.symbol.exists &&
t.symbol.owner.derivesFrom(defn.ProductClass) &&
t.symbol.owner.is(CaseClass) &&
t.symbol.name.isSelectorName &&
!isCaseClassWithVar // Conservative Covers case class A(var x: Int)
def isImmutableCaseAccessor = t.symbol.is(CaseAccessor) && !t.symbol.is(Mutable)
if (isGetterOfAImmutableField || isAccessingProductField || isImmutableCaseAccessor)
readingOnlyVals(rec)
else false

case Select(rec, _) if t.symbol.is(Method) =>
if (t.symbol.isGetter && !t.symbol.is(Mutable))
readingOnlyVals(rec) // getter of a immutable field
else if (t.symbol.owner.derivesFrom(defn.ProductClass) && t.symbol.owner.is(CaseClass) && t.symbol.name.isSelectorName) {
if (isGetterOfAImmutableField)
readingOnlyVals(rec) // Getter of an immutable field
else if (isAccessingProductField) {
def isImmutableField = {
val fieldId = t.symbol.name.toString.drop(1).toInt - 1
!t.symbol.owner.caseAccessors(ctx)(fieldId).is(Mutable)
}
if (isImmutableField) readingOnlyVals(rec) // accessing a field of a product
if (isImmutableField) readingOnlyVals(rec) // Accessing a field of a product
else false
} else if (t.symbol.is(CaseAccessor) && !t.symbol.is(Mutable))
} else if (isImmutableCaseAccessor)
readingOnlyVals(rec)
else false

Expand All @@ -208,7 +210,7 @@ class Devalify extends Optimisation {
} else
readingOnlyVals(qual)

case t: Ident if !t.symbol.is(Mutable) && !t.symbol.is(Method) && !t.symbol.info.dealias.isInstanceOf[ExprType] =>
case t: Ident if !t.symbol.is(Mutable | Method) && !t.symbol.info.dealias.isInstanceOf[ExprType] =>
desugarIdent(t) match {
case Some(t) => readingOnlyVals(t)
case None => true
Expand All @@ -228,5 +230,6 @@ class Devalify extends Optimisation {
case Literal(Constant(null)) => false
case t: Literal => true
case _ => false
}
}
}