From cd18b16407816a31c3562ec0900468d6fc2ba2b1 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 25 Mar 2015 19:08:02 +0300 Subject: [PATCH] Partial body resolve: fixed for ! and parenthesis in conditions --- .../kotlin/resolve/lazy/PartialBodyResolveFilter.kt | 12 ++++++++++++ .../partialBodyResolve/IfNegatedNotNullReturn.dump | 6 ++++++ .../partialBodyResolve/IfNegatedNotNullReturn.kt | 4 ++++ .../resolve/PartialBodyResolveTestGenerated.java | 6 ++++++ 4 files changed, 28 insertions(+) create mode 100644 idea/testData/resolve/partialBodyResolve/IfNegatedNotNullReturn.dump create mode 100644 idea/testData/resolve/partialBodyResolve/IfNegatedNotNullReturn.kt diff --git a/idea/ide-common/src/org/jetbrains/kotlin/resolve/lazy/PartialBodyResolveFilter.kt b/idea/ide-common/src/org/jetbrains/kotlin/resolve/lazy/PartialBodyResolveFilter.kt index e4386445724..3443193f6c0 100644 --- a/idea/ide-common/src/org/jetbrains/kotlin/resolve/lazy/PartialBodyResolveFilter.kt +++ b/idea/ide-common/src/org/jetbrains/kotlin/resolve/lazy/PartialBodyResolveFilter.kt @@ -319,6 +319,18 @@ class PartialBodyResolveFilter( val cast = condition.getLeftHandSide().smartCastExpressionName().singletonOrEmptySet() return if (condition.isNegated()) Pair(setOf(), cast) else Pair(cast, setOf()) } + + is JetPrefixExpression -> { + if (condition.getOperationToken() == JetTokens.EXCL) { + val operand = condition.getBaseExpression() ?: return emptyResult + return possiblySmartCastInCondition(operand).swap() + } + } + + is JetParenthesizedExpression -> { + val operand = condition.getExpression() ?: return emptyResult + return possiblySmartCastInCondition(operand) + } } return emptyResult diff --git a/idea/testData/resolve/partialBodyResolve/IfNegatedNotNullReturn.dump b/idea/testData/resolve/partialBodyResolve/IfNegatedNotNullReturn.dump new file mode 100644 index 00000000000..fc45fa6928d --- /dev/null +++ b/idea/testData/resolve/partialBodyResolve/IfNegatedNotNullReturn.dump @@ -0,0 +1,6 @@ +Resolve target: value-parameter val p: kotlin.Any? smart-cast to kotlin.Any +---------------------------------------------- +fun foo(p: Any?) { + if (!(p != null)) return + p.hashCode() +} diff --git a/idea/testData/resolve/partialBodyResolve/IfNegatedNotNullReturn.kt b/idea/testData/resolve/partialBodyResolve/IfNegatedNotNullReturn.kt new file mode 100644 index 00000000000..e1ba368fce9 --- /dev/null +++ b/idea/testData/resolve/partialBodyResolve/IfNegatedNotNullReturn.kt @@ -0,0 +1,4 @@ +fun foo(p: Any?) { + if (!(p != null)) return + p.hashCode() +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/resolve/PartialBodyResolveTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/resolve/PartialBodyResolveTestGenerated.java index dddb8ab94e2..a1704942d29 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/resolve/PartialBodyResolveTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/resolve/PartialBodyResolveTestGenerated.java @@ -144,6 +144,12 @@ public class PartialBodyResolveTestGenerated extends AbstractPartialBodyResolveT doTest(fileName); } + @TestMetadata("IfNegatedNotNullReturn.kt") + public void testIfNegatedNotNullReturn() throws Exception { + String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/IfNegatedNotNullReturn.kt"); + doTest(fileName); + } + @TestMetadata("IfNotIsError.kt") public void testIfNotIsError() throws Exception { String fileName = JetTestUtils.navigationMetadata("idea/testData/resolve/partialBodyResolve/IfNotIsError.kt");