From 2ac70ee35f2f47e0464ba219441bb0eecf86e525 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 20 Jul 2012 14:40:30 +0400 Subject: [PATCH] KT-2457 Verify error when comparing not null value with null in when backend-side changes --- .../src/org/jetbrains/jet/codegen/ExpressionCodegen.java | 6 +++++- .../patternMatching/matchNotNullAgainstNullable.kt | 7 +++++++ compiler/testData/codegen/regressions/kt2457.kt | 8 ++++++++ .../org/jetbrains/jet/codegen/PatternMatchingTest.java | 8 ++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 compiler/testData/codegen/patternMatching/matchNotNullAgainstNullable.kt create mode 100644 compiler/testData/codegen/regressions/kt2457.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index a7db4dead14..dffe5e8d12e 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -2848,7 +2848,7 @@ The "returned" value of try expression with no finally is either the last expres } else if (pattern instanceof JetExpressionPattern) { if (expressionToMatch != null) { - final Type subjectType = expressionToMatch.type; + Type subjectType = expressionToMatch.type; expressionToMatch.dupReceiver(v); expressionToMatch.put(subjectType, v); JetExpression condExpression = ((JetExpressionPattern) pattern).getExpression(); @@ -2857,6 +2857,10 @@ The "returned" value of try expression with no finally is either the last expres Type condType; if (isNumberPrimitive(subjectType) || subjectType.getSort() == Type.BOOLEAN) { condType = asmType(condJetType); + if (!(isNumberPrimitive(condType) || condType.getSort() == Type.BOOLEAN)) { + subjectType = JetTypeMapper.boxType(subjectType); + expressionToMatch.coerce(subjectType, v); + } } else { condType = TYPE_OBJECT; diff --git a/compiler/testData/codegen/patternMatching/matchNotNullAgainstNullable.kt b/compiler/testData/codegen/patternMatching/matchNotNullAgainstNullable.kt new file mode 100644 index 00000000000..c0598674113 --- /dev/null +++ b/compiler/testData/codegen/patternMatching/matchNotNullAgainstNullable.kt @@ -0,0 +1,7 @@ +fun foo(i: Int, j: Int?): String = + when (i) { + j -> "OK" + else -> "Fail" + } + +fun box(): String = foo(0, 0) diff --git a/compiler/testData/codegen/regressions/kt2457.kt b/compiler/testData/codegen/regressions/kt2457.kt new file mode 100644 index 00000000000..e6f15becd08 --- /dev/null +++ b/compiler/testData/codegen/regressions/kt2457.kt @@ -0,0 +1,8 @@ +fun foo(i: Int) : Int = + when (i) { + 1 -> 1 + null -> 1 + else -> 1 + } + +fun box() : String = if (foo(1) == 1) "OK" else "fail" diff --git a/compiler/tests/org/jetbrains/jet/codegen/PatternMatchingTest.java b/compiler/tests/org/jetbrains/jet/codegen/PatternMatchingTest.java index f155c34376d..f9dfcc5f78e 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/PatternMatchingTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/PatternMatchingTest.java @@ -162,4 +162,12 @@ public class PatternMatchingTest extends CodegenTestCase { public void testKt2466() throws Exception { blackBoxFile("patternMatching/kt2466.kt"); } + + public void testMatchNotNullAgainstNullable() throws Exception { + blackBoxFile("patternMatching/matchNotNullAgainstNullable.kt"); + } + + public void testKt2457() throws Exception { + blackBoxFile("regressions/kt2457.kt"); + } }