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
Show file tree
Hide file tree
Changes from 2 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
Diff view
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ class Definitions {
lazy val BoxedFloatModule = ctx.requiredModule("java.lang.Float")
lazy val BoxedDoubleModule = ctx.requiredModule("java.lang.Double")
lazy val BoxedUnitModule = ctx.requiredModule("java.lang.Void")
lazy val SystemModule = ctx.requiredModule("java.lang.System")

lazy val ByNameParamClass2x = enterSpecialPolyClass(tpnme.BYNAME_PARAM_CLASS, Covariant, Seq(AnyType))
lazy val EqualsPatternClass = enterSpecialPolyClass(tpnme.EQUALS_PATTERN, EmptyFlags, Seq(AnyType))
Expand Down
46 changes: 31 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,43 +164,58 @@ 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)) =>
if ((fun.symbol eq defn.Any_asInstanceOf) && rec.tpe.derivesFrom(tp.tpe.classSymbol))
readingOnlyVals(rec)
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

case Select(qual, _) if !t.symbol.is(Mutable) =>
readingOnlyVals(qual)
case t: Ident if !t.symbol.is(Mutable) && !t.symbol.is(Method) && !t.symbol.info.dealias.isInstanceOf[ExprType] =>
if (t.symbol == defn.SystemModule) {
// System.in is static final fields that, for legacy reasons, must be
// allowed to be changed by the methods System.setIn...
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.5.4
false
} else
readingOnlyVals(qual)

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
}

case t: This => true
// null => false, or the following fails devalify:
// trait I {
Expand All @@ -215,5 +230,6 @@ class Devalify extends Optimisation {
case Literal(Constant(null)) => false
case t: Literal => true
case _ => false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ class DropNoEffects(val simplifyPhase: Simplify) extends Optimisation {
keepOnlySideEffects(rec)

// !name.eq(nme.TYPE_) && // Keep the .TYPE added by ClassOf, would be needed for AfterErasure
case s @ Select(qual, name) if !s.symbol.is(Mutable | Lazy | Method) =>
// Without is(JavaStatic), { System.out } becomes { System }, but "Java class can't be used as value"
case s @ Select(qual, name) if !s.symbol.is(Mutable | Lazy | Method | JavaStatic) =>
keepOnlySideEffects(qual)

case Block(List(t: DefDef), s: Closure) =>
Expand Down
14 changes: 14 additions & 0 deletions tests/run/2772.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.io.OutputStream

object Test {
def main(args: Array[String]): Unit = {
val oldErr = System.err
System.setErr(null) // setOut(null) confuses the testing framework...
val a = () => foo(oldErr)
a()
}

def foo(err: OutputStream): Unit = {
err.write(0)
}
}