11package dotty .tools .dotc
22package typer
33
4+ import scala .language .{unsafeNulls => _ }
5+
46import java .lang .ArithmeticException
57
68import ast ._
@@ -74,7 +76,7 @@ object ConstFold:
7476 private def withFoldedType (c : Constant | Null ): T =
7577 if c == null then tree else tree.withType(ConstantType (c)).asInstanceOf [T ]
7678
77- private def foldUnop (op : Name , x : Constant ): Constant = (op, x.tag) match {
79+ private def foldUnop (op : Name , x : Constant ): Constant | Null = (op, x.tag) match {
7880 case (nme.UNARY_! , BooleanTag ) => Constant (! x.booleanValue)
7981
8082 case (nme.UNARY_~ , IntTag ) => Constant (~ x.intValue)
@@ -96,7 +98,7 @@ object ConstFold:
9698 /** These are local helpers to keep foldBinop from overly taxing the
9799 * optimizer.
98100 */
99- private def foldBooleanOp (op : Name , x : Constant , y : Constant ): Constant = op match {
101+ private def foldBooleanOp (op : Name , x : Constant , y : Constant ): Constant | Null = op match {
100102 case nme.ZOR => Constant (x.booleanValue | y.booleanValue)
101103 case nme.OR => Constant (x.booleanValue | y.booleanValue)
102104 case nme.XOR => Constant (x.booleanValue ^ y.booleanValue)
@@ -106,7 +108,7 @@ object ConstFold:
106108 case nme.NE => Constant (x.booleanValue != y.booleanValue)
107109 case _ => null
108110 }
109- private def foldSubrangeOp (op : Name , x : Constant , y : Constant ): Constant = op match {
111+ private def foldSubrangeOp (op : Name , x : Constant , y : Constant ): Constant | Null = op match {
110112 case nme.OR => Constant (x.intValue | y.intValue)
111113 case nme.XOR => Constant (x.intValue ^ y.intValue)
112114 case nme.AND => Constant (x.intValue & y.intValue)
@@ -126,7 +128,7 @@ object ConstFold:
126128 case nme.MOD => Constant (x.intValue % y.intValue)
127129 case _ => null
128130 }
129- private def foldLongOp (op : Name , x : Constant , y : Constant ): Constant = op match {
131+ private def foldLongOp (op : Name , x : Constant , y : Constant ): Constant | Null = op match {
130132 case nme.OR => Constant (x.longValue | y.longValue)
131133 case nme.XOR => Constant (x.longValue ^ y.longValue)
132134 case nme.AND => Constant (x.longValue & y.longValue)
@@ -146,7 +148,7 @@ object ConstFold:
146148 case nme.MOD => Constant (x.longValue % y.longValue)
147149 case _ => null
148150 }
149- private def foldFloatOp (op : Name , x : Constant , y : Constant ): Constant = op match {
151+ private def foldFloatOp (op : Name , x : Constant , y : Constant ): Constant | Null = op match {
150152 case nme.EQ => Constant (x.floatValue == y.floatValue)
151153 case nme.NE => Constant (x.floatValue != y.floatValue)
152154 case nme.LT => Constant (x.floatValue < y.floatValue)
@@ -160,7 +162,7 @@ object ConstFold:
160162 case nme.MOD => Constant (x.floatValue % y.floatValue)
161163 case _ => null
162164 }
163- private def foldDoubleOp (op : Name , x : Constant , y : Constant ): Constant = op match {
165+ private def foldDoubleOp (op : Name , x : Constant , y : Constant ): Constant | Null = op match {
164166 case nme.EQ => Constant (x.doubleValue == y.doubleValue)
165167 case nme.NE => Constant (x.doubleValue != y.doubleValue)
166168 case nme.LT => Constant (x.doubleValue < y.doubleValue)
@@ -174,21 +176,21 @@ object ConstFold:
174176 case nme.MOD => Constant (x.doubleValue % y.doubleValue)
175177 case _ => null
176178 }
177- private def foldStringOp (op : Name , x : Constant , y : Constant ): Constant = op match {
179+ private def foldStringOp (op : Name , x : Constant , y : Constant ): Constant | Null = op match {
178180 case nme.ADD => Constant (x.stringValue + y.stringValue)
179181 case nme.EQ => Constant (x.stringValue == y.stringValue)
180182 case nme.NE => Constant (x.stringValue != y.stringValue)
181183 case _ => null
182184 }
183185
184- private def foldNullOp (op : Name , x : Constant , y : Constant ): Constant =
186+ private def foldNullOp (op : Name , x : Constant , y : Constant ): Constant | Null =
185187 assert(x.tag == NullTag || y.tag == NullTag )
186188 op match
187189 case nme.EQ => Constant (x.tag == y.tag)
188190 case nme.NE => Constant (x.tag != y.tag)
189191 case _ => null
190192
191- private def foldBinop (op : Name , x : Constant , y : Constant ): Constant =
193+ private def foldBinop (op : Name , x : Constant , y : Constant ): Constant | Null =
192194 val optag =
193195 if (x.tag == y.tag) x.tag
194196 else if (x.isNumeric && y.isNumeric) math.max(x.tag, y.tag)
0 commit comments