KT-27948: Use proper KotlinType when comparing with literal 0

#KT-27948
This commit is contained in:
Dmitry Petrov
2018-11-07 12:34:45 +03:00
parent e14f74bc18
commit 1523185734
8 changed files with 76 additions and 1 deletions
@@ -3680,8 +3680,17 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}
private StackValue genCmpWithZero(KtExpression exp, IElementType opToken, @Nullable StackValue pregeneratedExpr) {
StackValue argument;
if (pregeneratedExpr == null) {
KotlinType kotlinType = kotlinType(exp);
assert kotlinType != null : "No KotlinType for expression " + exp.getText();
argument = genLazy(exp, asmType(kotlinType), kotlinType);
}
else {
argument = pregeneratedExpr;
}
return StackValue.compareIntWithZero(
pregeneratedExpr != null ? pregeneratedExpr : gen(exp),
argument,
(KtTokens.EQEQ == opToken || KtTokens.EQEQEQ == opToken) ? IFNE : IFEQ
);
}
@@ -0,0 +1,36 @@
// WITH_UNSIGNED
// IGNORE_BACKEND: JVM_IR
fun isZeroUInt(n: UInt?) = n!! == 0U
fun isZeroUInt2(n: UInt?) = 0U == n!!
fun isZeroULong(n: ULong?) = n!! == 0UL
fun isZeroULong2(n: ULong?) = 0UL == n!!
fun isNullUInt(n: UInt?) = n == null
fun isNullUInt2(n: UInt?) = null == n
fun isNullULong(n: ULong?) = n == null
fun isNullULong2(n: ULong?) = null == n
fun box(): String {
if (isZeroUInt(1U)) throw AssertionError()
if (isZeroUInt2(1U)) throw AssertionError()
if (!isZeroUInt(0U)) throw AssertionError()
if (!isZeroUInt2(0U)) throw AssertionError()
if (isZeroULong(1UL)) throw AssertionError()
if (isZeroULong2(1UL)) throw AssertionError()
if (!isZeroULong(0UL)) throw AssertionError()
if (!isZeroULong2(0UL)) throw AssertionError()
if (isNullUInt(1U)) throw AssertionError()
if (isNullUInt2(1U)) throw AssertionError()
if (!isNullUInt(null)) throw AssertionError()
if (!isNullUInt2(null)) throw AssertionError()
if (isNullULong(1UL)) throw AssertionError()
if (isNullULong2(1UL)) throw AssertionError()
if (!isNullULong(null)) throw AssertionError()
if (!isNullULong2(null)) throw AssertionError()
return "OK"
}
@@ -2,6 +2,7 @@
// IGNORE_BACKEND: JVM_IR
fun isZeroUInt(n: UInt?) = n == 0U
fun isZeroUInt2(n: UInt?): Boolean = n != null && n == 0u
fun isZeroULong(n: ULong?) = n == 0UL
fun box(): String {
@@ -9,6 +10,10 @@ fun box(): String {
if (isZeroUInt(1U)) throw AssertionError()
if (!isZeroUInt(0U)) throw AssertionError()
if (isZeroUInt2(null)) throw AssertionError()
if (isZeroUInt2(1U)) throw AssertionError()
if (!isZeroUInt2(0U)) throw AssertionError()
if (isZeroULong(null)) throw AssertionError()
if (isZeroULong(1UL)) throw AssertionError()
if (!isZeroULong(0UL)) throw AssertionError()
@@ -23877,6 +23877,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/unsignedTypes/boxConstValOfUnsignedType.kt");
}
@TestMetadata("boxedUnsignedEqualsZero.kt")
public void testBoxedUnsignedEqualsZero() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/boxedUnsignedEqualsZero.kt");
}
@TestMetadata("checkBasicUnsignedLiterals.kt")
public void testCheckBasicUnsignedLiterals() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt");
@@ -23877,6 +23877,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/unsignedTypes/boxConstValOfUnsignedType.kt");
}
@TestMetadata("boxedUnsignedEqualsZero.kt")
public void testBoxedUnsignedEqualsZero() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/boxedUnsignedEqualsZero.kt");
}
@TestMetadata("checkBasicUnsignedLiterals.kt")
public void testCheckBasicUnsignedLiterals() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt");
@@ -23882,6 +23882,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/unsignedTypes/boxConstValOfUnsignedType.kt");
}
@TestMetadata("boxedUnsignedEqualsZero.kt")
public void testBoxedUnsignedEqualsZero() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/boxedUnsignedEqualsZero.kt");
}
@TestMetadata("checkBasicUnsignedLiterals.kt")
public void testCheckBasicUnsignedLiterals() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt");
@@ -21042,6 +21042,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/unsignedTypes/boxConstValOfUnsignedType.kt");
}
@TestMetadata("boxedUnsignedEqualsZero.kt")
public void testBoxedUnsignedEqualsZero() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/boxedUnsignedEqualsZero.kt");
}
@TestMetadata("checkBasicUnsignedLiterals.kt")
public void testCheckBasicUnsignedLiterals() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt");
@@ -22087,6 +22087,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/unsignedTypes/boxConstValOfUnsignedType.kt");
}
@TestMetadata("boxedUnsignedEqualsZero.kt")
public void testBoxedUnsignedEqualsZero() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/boxedUnsignedEqualsZero.kt");
}
@TestMetadata("checkBasicUnsignedLiterals.kt")
public void testCheckBasicUnsignedLiterals() throws Exception {
runTest("compiler/testData/codegen/box/unsignedTypes/checkBasicUnsignedLiterals.kt");