Skip to content

Commit 963414f

Browse files
committed
SI-8462: Integral shift Long result type corrected
Constant folding was incorrectly promoting to Long when the operand was Long, as with other binary ops, but the result type depends on the receiver. Per SLS 12.2.1. This fixes ((1 << 2L): Int) and the other shift ops and the other integral types.
1 parent f13fc65 commit 963414f

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/compiler/scala/tools/nsc/typechecker/ConstantFolder.scala

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ abstract class ConstantFolder {
7575
case nme.AND => Constant(x.booleanValue & y.booleanValue)
7676
case nme.EQ => Constant(x.booleanValue == y.booleanValue)
7777
case nme.NE => Constant(x.booleanValue != y.booleanValue)
78-
case _ => null
78+
case _ => null
7979
}
8080
private def foldSubrangeOp(op: Name, x: Constant, y: Constant): Constant = op match {
8181
case nme.OR => Constant(x.intValue | y.intValue)
@@ -95,14 +95,20 @@ abstract class ConstantFolder {
9595
case nme.MUL => Constant(x.intValue * y.intValue)
9696
case nme.DIV => Constant(x.intValue / y.intValue)
9797
case nme.MOD => Constant(x.intValue % y.intValue)
98-
case _ => null
98+
case _ => null
9999
}
100100
private def foldLongOp(op: Name, x: Constant, y: Constant): Constant = op match {
101101
case nme.OR => Constant(x.longValue | y.longValue)
102102
case nme.XOR => Constant(x.longValue ^ y.longValue)
103103
case nme.AND => Constant(x.longValue & y.longValue)
104-
case nme.LSL => Constant(x.longValue << y.longValue)
104+
case nme.LSL if x.tag <= IntTag
105+
=> Constant(x.intValue << y.longValue)
106+
case nme.LSL => Constant(x.longValue << y.longValue)
107+
case nme.LSR if x.tag <= IntTag
108+
=> Constant(x.intValue >>> y.longValue)
105109
case nme.LSR => Constant(x.longValue >>> y.longValue)
110+
case nme.ASR if x.tag <= IntTag
111+
=> Constant(x.intValue >> y.longValue)
106112
case nme.ASR => Constant(x.longValue >> y.longValue)
107113
case nme.EQ => Constant(x.longValue == y.longValue)
108114
case nme.NE => Constant(x.longValue != y.longValue)
@@ -115,7 +121,7 @@ abstract class ConstantFolder {
115121
case nme.MUL => Constant(x.longValue * y.longValue)
116122
case nme.DIV => Constant(x.longValue / y.longValue)
117123
case nme.MOD => Constant(x.longValue % y.longValue)
118-
case _ => null
124+
case _ => null
119125
}
120126
private def foldFloatOp(op: Name, x: Constant, y: Constant): Constant = op match {
121127
case nme.EQ => Constant(x.floatValue == y.floatValue)
@@ -129,7 +135,7 @@ abstract class ConstantFolder {
129135
case nme.MUL => Constant(x.floatValue * y.floatValue)
130136
case nme.DIV => Constant(x.floatValue / y.floatValue)
131137
case nme.MOD => Constant(x.floatValue % y.floatValue)
132-
case _ => null
138+
case _ => null
133139
}
134140
private def foldDoubleOp(op: Name, x: Constant, y: Constant): Constant = op match {
135141
case nme.EQ => Constant(x.doubleValue == y.doubleValue)
@@ -143,7 +149,7 @@ abstract class ConstantFolder {
143149
case nme.MUL => Constant(x.doubleValue * y.doubleValue)
144150
case nme.DIV => Constant(x.doubleValue / y.doubleValue)
145151
case nme.MOD => Constant(x.doubleValue % y.doubleValue)
146-
case _ => null
152+
case _ => null
147153
}
148154

149155
private def foldBinop(op: Name, x: Constant, y: Constant): Constant = {
@@ -162,7 +168,7 @@ abstract class ConstantFolder {
162168
case _ => null
163169
}
164170
catch {
165-
case ex: ArithmeticException => null
171+
case _: ArithmeticException => null
166172
}
167173
}
168174
}

test/files/pos/t8462.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
trait ConstantOps {
3+
def exprs = (
4+
1 << 2L : Int, // was: error: type mismatch; found : Long(4L)
5+
64 >> 2L : Int, // was: error: type mismatch; found : Long(4L)
6+
64 >>> 2L : Int, // was: error: type mismatch; found : Long(4L)
7+
'a' << 2L : Int,
8+
'a' >> 2L : Int,
9+
'a'>>> 2L : Int
10+
)
11+
}

0 commit comments

Comments
 (0)