JVM: Handle inline class equality in when statement with declaration

#KT-34268 Fixed
This commit is contained in:
Steven Schäfer
2019-10-16 19:23:49 +02:00
committed by Alexander Udalov
parent bbdbc2896b
commit 4878c7967a
7 changed files with 43 additions and 7 deletions
@@ -3297,7 +3297,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
else if (opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ || else if (opToken == KtTokens.EQEQ || opToken == KtTokens.EXCLEQ ||
opToken == KtTokens.EQEQEQ || opToken == KtTokens.EXCLEQEQEQ) { opToken == KtTokens.EQEQEQ || opToken == KtTokens.EXCLEQEQEQ) {
return generateEquals( return generateEquals(
expression.getLeft(), expression.getRight(), opToken, null, expression.getLeft(), expression.getRight(), opToken, null, null,
bindingContext.get(BindingContext.PRIMITIVE_NUMERIC_COMPARISON_INFO, expression) bindingContext.get(BindingContext.PRIMITIVE_NUMERIC_COMPARISON_INFO, expression)
); );
} }
@@ -3372,6 +3372,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
@Nullable KtExpression right, @Nullable KtExpression right,
@NotNull IElementType opToken, @NotNull IElementType opToken,
@Nullable StackValue pregeneratedSubject, @Nullable StackValue pregeneratedSubject,
@Nullable KotlinType subjectKotlinType,
@Nullable PrimitiveNumericComparisonInfo primitiveNumericComparisonInfo @Nullable PrimitiveNumericComparisonInfo primitiveNumericComparisonInfo
) { ) {
if (left == null || right == null) { if (left == null || right == null) {
@@ -3382,7 +3383,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
}); });
} }
KotlinType leftKotlinType = kotlinType(left); KotlinType leftKotlinType = (subjectKotlinType != null) ? subjectKotlinType : kotlinType(left);
KotlinType rightKotlinType = kotlinType(right); KotlinType rightKotlinType = kotlinType(right);
boolean leftIsInlineClass = leftKotlinType != null && InlineClassesUtilsKt.isInlineClassType(leftKotlinType); boolean leftIsInlineClass = leftKotlinType != null && InlineClassesUtilsKt.isInlineClassType(leftKotlinType);
boolean rightIsInlineClass = rightKotlinType != null && InlineClassesUtilsKt.isInlineClassType(rightKotlinType); boolean rightIsInlineClass = rightKotlinType != null && InlineClassesUtilsKt.isInlineClassType(rightKotlinType);
@@ -4892,10 +4893,10 @@ The "returned" value of try expression with no finally is either the last expres
return generateIsCheck(match, expression.getTypeReference(), expression.isNegated()); return generateIsCheck(match, expression.getTypeReference(), expression.isNegated());
} }
private StackValue generateExpressionMatch(StackValue expressionToMatch, KtExpression subjectExpression, KtExpression patternExpression) { private StackValue generateExpressionMatch(StackValue expressionToMatch, KtExpression subjectExpression, KotlinType subjectKotlinType, KtExpression patternExpression) {
if (expressionToMatch != null) { if (expressionToMatch != null) {
return generateEquals( return generateEquals(
subjectExpression, patternExpression, KtTokens.EQEQ, expressionToMatch, subjectExpression, patternExpression, KtTokens.EQEQ, expressionToMatch, subjectKotlinType,
bindingContext.get(BindingContext.PRIMITIVE_NUMERIC_COMPARISON_INFO, patternExpression) bindingContext.get(BindingContext.PRIMITIVE_NUMERIC_COMPARISON_INFO, patternExpression)
); );
} }
@@ -4996,6 +4997,7 @@ The "returned" value of try expression with no finally is either the last expres
else { else {
subjectLocal = -1; subjectLocal = -1;
subjectType = Type.VOID_TYPE; subjectType = Type.VOID_TYPE;
subjectKotlinType = null;
} }
Label begin = new Label(); Label begin = new Label();
@@ -5015,7 +5017,7 @@ The "returned" value of try expression with no finally is either the last expres
if (!whenEntry.isElse()) { if (!whenEntry.isElse()) {
KtWhenCondition[] conditions = whenEntry.getConditions(); KtWhenCondition[] conditions = whenEntry.getConditions();
for (int i = 0; i < conditions.length; i++) { for (int i = 0; i < conditions.length; i++) {
StackValue conditionValue = generateWhenCondition(subjectExpression, subjectType, subjectLocal, conditions[i]); StackValue conditionValue = generateWhenCondition(subjectExpression, subjectType, subjectKotlinType, subjectLocal, conditions[i]);
BranchedValue.Companion.condJump(conditionValue, nextCondition, true, v); BranchedValue.Companion.condJump(conditionValue, nextCondition, true, v);
if (i < conditions.length - 1) { if (i < conditions.length - 1) {
v.goTo(thisEntry); v.goTo(thisEntry);
@@ -5069,7 +5071,7 @@ The "returned" value of try expression with no finally is either the last expres
} }
} }
private StackValue generateWhenCondition(KtExpression subjectExpression, Type subjectType, int subjectLocal, KtWhenCondition condition) { private StackValue generateWhenCondition(KtExpression subjectExpression, Type subjectType, KotlinType subjectKotlinType, int subjectLocal, KtWhenCondition condition) {
if (condition instanceof KtWhenConditionInRange) { if (condition instanceof KtWhenConditionInRange) {
KtWhenConditionInRange conditionInRange = (KtWhenConditionInRange) condition; KtWhenConditionInRange conditionInRange = (KtWhenConditionInRange) condition;
return generateIn(StackValue.local(subjectLocal, subjectType), return generateIn(StackValue.local(subjectLocal, subjectType),
@@ -5083,7 +5085,7 @@ The "returned" value of try expression with no finally is either the last expres
} }
else if (condition instanceof KtWhenConditionWithExpression) { else if (condition instanceof KtWhenConditionWithExpression) {
KtExpression patternExpression = ((KtWhenConditionWithExpression) condition).getExpression(); KtExpression patternExpression = ((KtWhenConditionWithExpression) condition).getExpression();
return generateExpressionMatch(match, subjectExpression, patternExpression); return generateExpressionMatch(match, subjectExpression, subjectKotlinType, patternExpression);
} }
else { else {
throw new UnsupportedOperationException("unsupported kind of when condition"); throw new UnsupportedOperationException("unsupported kind of when condition");
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// KJS_WITH_FULL_RUNTIME
fun box(): String {
return when(val foo = 42UL) {
42UL -> "OK"
else -> "Fail"
}
}
@@ -12818,6 +12818,11 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/inlineClasses/kt28920_javaPrimitiveType.kt"); runTest("compiler/testData/codegen/box/inlineClasses/kt28920_javaPrimitiveType.kt");
} }
@TestMetadata("kt34268.kt")
public void testKt34268() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt34268.kt");
}
@TestMetadata("mangledDefaultParameterFunction.kt") @TestMetadata("mangledDefaultParameterFunction.kt")
public void testMangledDefaultParameterFunction() throws Exception { public void testMangledDefaultParameterFunction() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt"); runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt");
@@ -12823,6 +12823,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/inlineClasses/kt28920_javaPrimitiveType.kt"); runTest("compiler/testData/codegen/box/inlineClasses/kt28920_javaPrimitiveType.kt");
} }
@TestMetadata("kt34268.kt")
public void testKt34268() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt34268.kt");
}
@TestMetadata("mangledDefaultParameterFunction.kt") @TestMetadata("mangledDefaultParameterFunction.kt")
public void testMangledDefaultParameterFunction() throws Exception { public void testMangledDefaultParameterFunction() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt"); runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt");
@@ -11703,6 +11703,11 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/inlineClasses/kt28920_javaPrimitiveType.kt"); runTest("compiler/testData/codegen/box/inlineClasses/kt28920_javaPrimitiveType.kt");
} }
@TestMetadata("kt34268.kt")
public void testKt34268() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt34268.kt");
}
@TestMetadata("mangledDefaultParameterFunction.kt") @TestMetadata("mangledDefaultParameterFunction.kt")
public void testMangledDefaultParameterFunction() throws Exception { public void testMangledDefaultParameterFunction() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt"); runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt");
@@ -10098,6 +10098,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/kt28585.kt"); runTest("compiler/testData/codegen/box/inlineClasses/kt28585.kt");
} }
@TestMetadata("kt34268.kt")
public void testKt34268() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt34268.kt");
}
@TestMetadata("mangledDefaultParameterFunction.kt") @TestMetadata("mangledDefaultParameterFunction.kt")
public void testMangledDefaultParameterFunction() throws Exception { public void testMangledDefaultParameterFunction() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt"); runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt");
@@ -11253,6 +11253,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/inlineClasses/kt28585.kt"); runTest("compiler/testData/codegen/box/inlineClasses/kt28585.kt");
} }
@TestMetadata("kt34268.kt")
public void testKt34268() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/kt34268.kt");
}
@TestMetadata("mangledDefaultParameterFunction.kt") @TestMetadata("mangledDefaultParameterFunction.kt")
public void testMangledDefaultParameterFunction() throws Exception { public void testMangledDefaultParameterFunction() throws Exception {
runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt"); runTest("compiler/testData/codegen/box/inlineClasses/mangledDefaultParameterFunction.kt");