KT-18731 Use reference equality to compare enums
Fix of https://youtrack.jetbrains.com/issue/KT-18731
This commit is contained in:
committed by
Dmitry Petrov
parent
bc54c95bc3
commit
a2a3043607
@@ -99,6 +99,7 @@ import static org.jetbrains.kotlin.codegen.inline.InlineCodegenUtilsKt.*;
|
||||
import static org.jetbrains.kotlin.resolve.BindingContext.*;
|
||||
import static org.jetbrains.kotlin.resolve.BindingContextUtils.getDelegationConstructorCall;
|
||||
import static org.jetbrains.kotlin.resolve.BindingContextUtils.isVarCapturedInClosure;
|
||||
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isEnumClass;
|
||||
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isEnumEntry;
|
||||
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isObject;
|
||||
import static org.jetbrains.kotlin.resolve.jvm.AsmTypes.*;
|
||||
@@ -3107,6 +3108,18 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
||||
);
|
||||
}
|
||||
|
||||
if ((opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ) &&
|
||||
(isEnumClass(bindingContext.getType(left).getConstructor().getDeclarationDescriptor()) ||
|
||||
isEnumClass(bindingContext.getType(right).getConstructor().getDeclarationDescriptor()))) {
|
||||
// Reference equality can be used for enums.
|
||||
return StackValue.cmp(
|
||||
opToken == KtTokens.EQEQ ? KtTokens.EQEQEQ : KtTokens.EXCLEQEQEQ,
|
||||
OBJECT_TYPE,
|
||||
genLazyUnlessProvided(pregeneratedLeft, left, leftType),
|
||||
genLazy(right, rightType)
|
||||
);
|
||||
}
|
||||
|
||||
return genEqualsForExpressionsPreferIEEE754Arithmetic(left, right, opToken, leftType, rightType, pregeneratedLeft);
|
||||
}
|
||||
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
enum class Bar {
|
||||
ONE,
|
||||
TWO
|
||||
}
|
||||
|
||||
fun isOne(i: Bar) = i == Bar.ONE
|
||||
|
||||
fun box(): String {
|
||||
return when (isOne(Bar.ONE) && !isOne(Bar.TWO)) {
|
||||
true -> "OK"
|
||||
else -> "Failure"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
enum class Bar {
|
||||
ONE,
|
||||
TWO
|
||||
}
|
||||
|
||||
fun isOne(i: Bar) = i == Bar.ONE
|
||||
|
||||
fun box(): String {
|
||||
return when (isOne(Bar.ONE) && !isOne(Bar.TWO)) {
|
||||
true -> "OK"
|
||||
else -> "Failure"
|
||||
}
|
||||
}
|
||||
|
||||
// 1 IF_ACMPNE
|
||||
// 0 INVOKESTATIC kotlin/jvm/internal/Intrinsics.areEqual \(Ljava/lang/Object;Ljava/lang/Object;\)Z
|
||||
Generated
+6
@@ -8541,6 +8541,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18731.kt")
|
||||
public void testKt18731() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/kt18731.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt20651.kt")
|
||||
public void testKt20651() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/kt20651.kt");
|
||||
|
||||
+6
@@ -8541,6 +8541,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18731.kt")
|
||||
public void testKt18731() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/kt18731.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt20651.kt")
|
||||
public void testKt20651() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/kt20651.kt");
|
||||
|
||||
@@ -1270,6 +1270,21 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/bytecodeText/enum")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Enum extends AbstractBytecodeTextTest {
|
||||
public void testAllFilesPresentInEnum() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/bytecodeText/enum"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18731.kt")
|
||||
public void testKt18731() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/enum/kt18731.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/codegen/bytecodeText/forLoop")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
+6
@@ -8541,6 +8541,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18731.kt")
|
||||
public void testKt18731() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/kt18731.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt20651.kt")
|
||||
public void testKt20651() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/kt20651.kt");
|
||||
|
||||
+6
@@ -9225,6 +9225,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt18731.kt")
|
||||
public void testKt18731() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/kt18731.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("kt20651.kt")
|
||||
public void testKt20651() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/enum/kt20651.kt");
|
||||
|
||||
Reference in New Issue
Block a user