Skip to content

Commit fbb7865

Browse files
committed
Eliminating duplication in zero creation.
1 parent 7a5966f commit fbb7865

File tree

4 files changed

+26
-31
lines changed

4 files changed

+26
-31
lines changed

src/compiler/scala/reflect/internal/TreeGen.scala

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,19 @@ abstract class TreeGen {
250250
* var x: T = _
251251
* which is appropriate to the given Type.
252252
*/
253-
def mkZero(tp: Type): Tree = {
254-
val tree = tp.typeSymbol match {
255-
case UnitClass => Literal(Constant())
256-
case BooleanClass => Literal(Constant(false))
257-
case FloatClass => Literal(Constant(0.0f))
258-
case DoubleClass => Literal(Constant(0.0d))
259-
case ByteClass => Literal(Constant(0.toByte))
260-
case ShortClass => Literal(Constant(0.toShort))
261-
case IntClass => Literal(Constant(0))
262-
case LongClass => Literal(Constant(0L))
263-
case CharClass => Literal(Constant(0.toChar))
264-
case _ => Literal(Constant(null))
265-
}
266-
tree setType tp
253+
def mkZero(tp: Type): Tree = Literal(mkConstantZero(tp)) setType tp
254+
255+
def mkConstantZero(tp: Type): Constant = tp.typeSymbol match {
256+
case UnitClass => Constant(())
257+
case BooleanClass => Constant(false)
258+
case FloatClass => Constant(0.0f)
259+
case DoubleClass => Constant(0.0d)
260+
case ByteClass => Constant(0.toByte)
261+
case ShortClass => Constant(0.toShort)
262+
case IntClass => Constant(0)
263+
case LongClass => Constant(0L)
264+
case CharClass => Constant(0.toChar)
265+
case _ => Constant(null)
267266
}
268267

269268
def mkZeroContravariantAfterTyper(tp: Type): Tree = {

src/compiler/scala/tools/nsc/backend/icode/GenICode.scala

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -879,23 +879,19 @@ abstract class GenICode extends SubComponent {
879879
} else if (sym.elisionLevel.exists (_ < settings.elidebelow.value || settings.noassertions.value)) {
880880
// XXX settings.noassertions.value temporarily retained to avoid
881881
// breakage until a reasonable interface is settled upon.
882-
debuglog("Eliding call from " + tree.symbol.owner + " to " + sym + " based on its elision threshold of " + sym.elisionLevel.get)
883-
val value = expectedType match {
884-
case UNIT => ()
885-
case BOOL => false
886-
case BYTE => 0:Byte
887-
case SHORT => 0:Short
888-
case CHAR => '?'
889-
case INT => 0
890-
case LONG => 0L
891-
case FLOAT => 0.0f
892-
case DOUBLE => 0.0
893-
case _ => null
882+
debuglog("Eliding call from " + tree.symbol.owner + " to " + sym +
883+
" based on its elision threshold of " + sym.elisionLevel.get)
884+
if (expectedType.isValueType) {
885+
ctx.bb.emit(CONSTANT(global.gen.mkConstantZero(expectedType.toType)), tree.pos)
886+
generatedType = expectedType
887+
}
888+
else {
889+
ctx.bb.emit(CONSTANT(Constant(null)), tree.pos)
890+
generatedType = NullReference
894891
}
895-
ctx.bb.emit(CONSTANT(Constant(value)), tree.pos)
896-
generatedType = if (expectedType.isInstanceOf[ValueTypeKind]) expectedType else NullReference
897892
ctx
898-
} else { // normal method call
893+
}
894+
else { // normal method call
899895
debuglog("Gen CALL_METHOD with sym: " + sym + " isStaticSymbol: " + sym.isStaticMember);
900896
val invokeStyle =
901897
if (sym.isStaticMember)

test/files/run/elidable.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Good for me, I was not elided. C.f2
66
false
77
0
88
0
9-
?
9+
0
1010
0
1111
0
1212
0.0

test/files/run/elidable.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ object Test {
5959
println(f6())
6060
println(f7())
6161
println(f8())
62-
println(f9())
62+
println(f9().toInt)
6363
println(fa())
6464
println(fb())
6565
println(fc())

0 commit comments

Comments
 (0)