Skip to content

Commit cd035b2

Browse files
Factor out isMutable logic, use it in DropGoodCasts
1 parent faa8864 commit cd035b2

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

compiler/src/dotty/tools/dotc/transform/localopt/Devalify.scala

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import core.Types._
1010
import ast.Trees._
1111
import scala.collection.mutable
1212
import config.Printers.simplify
13-
import Simplify.desugarIdent
13+
import Simplify.{desugarIdent, isMutable}
1414
import transform.SymUtils._
1515

1616
/** Inline vals and remove vals that are aliases to other vals
@@ -201,14 +201,8 @@ class Devalify extends Optimisation {
201201
readingOnlyVals(rec)
202202
else false
203203

204-
case Select(qual, _) if !t.symbol.is(Mutable) =>
205-
if (t.symbol == defn.SystemModule) {
206-
// System.in is static final fields that, for legacy reasons, must be
207-
// allowed to be changed by the methods System.setIn...
208-
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.5.4
209-
false
210-
} else
211-
readingOnlyVals(qual)
204+
case t @ Select(qual, _) if !isMutable(t) =>
205+
readingOnlyVals(qual)
212206

213207
case t: Ident if !t.symbol.is(Mutable | Method) && !t.symbol.info.dealias.isInstanceOf[ExprType] =>
214208
desugarIdent(t) match {

compiler/src/dotty/tools/dotc/transform/localopt/DropGoodCasts.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import core.Types._
1010
import core.Flags._
1111
import ast.Trees._
1212
import transform.SymUtils._
13+
import Simplify.isMutable
1314

1415
/** Eliminated casts and equality tests whose results can be locally
1516
* determined at compile time:
@@ -77,7 +78,8 @@ import transform.SymUtils._
7778

7879
case TypeApply(fun @ Select(x, _), List(tp))
7980
if fun.symbol.eq(defn.Any_isInstanceOf) &&
80-
!x.symbol.is(Method | Mutable) &&
81+
!isMutable(x) &&
82+
!x.symbol.is(Method) &&
8183
x.symbol.exists && !x.symbol.owner.isClass =>
8284
(x.symbol, tp.tpe) :: Nil
8385

@@ -89,14 +91,16 @@ import transform.SymUtils._
8991
def collectNullTests(t: Tree)(implicit ctx: Context): List[Symbol] = {
9092
def recur(t: Tree): List[Symbol] =
9193
t match {
92-
case Apply(x, _) if (x.symbol == defn.Boolean_! || x.symbol == defn.Boolean_||) => Nil
94+
case Apply(x, _) if (x.symbol == defn.Boolean_! || x.symbol == defn.Boolean_||) =>
95+
Nil
9396

9497
case Apply(fun @ Select(x, _), y) if (fun.symbol == defn.Boolean_&&) =>
9598
recur(x) ++ recur(y.head)
9699

97100
case Apply(fun @ Select(x, _), List(tp))
98101
if fun.symbol.eq(defn.Object_ne) &&
99-
!x.symbol.is(Method | Mutable) &&
102+
!isMutable(x) &&
103+
!x.symbol.is(Method) &&
100104
x.symbol.exists && !x.symbol.owner.isClass =>
101105
x.symbol :: Nil
102106

compiler/src/dotty/tools/dotc/transform/localopt/Simplify.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,15 @@ object Simplify {
146146
case _ => None
147147
}
148148
}
149+
150+
// System.in is static final fields that, for legacy reasons, must be
151+
// allowed to be changed by the methods System.setIn. `isMutable` is true
152+
// is the field is Mutable or if it's a field of java.lang.System.
153+
// https://docs.oracle.com/javase/specs/jls/se8/html/jls-17.html#jls-17.5.4
154+
def isMutable(t: Tree)(implicit ctx: Context): Boolean = t match {
155+
case _ if t.symbol.is(Mutable) => true
156+
case s: Symbol => (s.symbol == defn.SystemModule)
157+
case i: Ident => desugarIdent(i).exists(isMutable)
158+
case _ => false
159+
}
149160
}

0 commit comments

Comments
 (0)