From de85d3df7215b5dd74d7ed76f803af250a97a975 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Wed, 25 Sep 2013 20:43:41 +0400 Subject: [PATCH] KT-4009 Unnecessary attempt to do a smart cast causes an error #KT-4009 Fixed do not add smart casted types for vars to constraint system --- .../lang/resolve/calls/CandidateResolver.java | 2 ++ .../tests/smartCasts/inference/kt4009.kt | 21 +++++++++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 5 +++++ 3 files changed, 28 insertions(+) create mode 100644 compiler/testData/diagnostics/tests/smartCasts/inference/kt4009.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java index 1dd3020ab3c..161a5adc12f 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/CandidateResolver.java @@ -665,6 +665,8 @@ public class CandidateResolver { DataFlowValue dataFlowValue = DataFlowValueFactory.createDataFlowValue( JetPsiUtil.unwrapFromBlock(argumentExpression), type, trace.getBindingContext()); + if (!dataFlowValue.isStableIdentifier()) return type; + Set possibleTypes = dataFlowInfoForArgument.getPossibleTypes(dataFlowValue); if (possibleTypes.isEmpty()) return type; diff --git a/compiler/testData/diagnostics/tests/smartCasts/inference/kt4009.kt b/compiler/testData/diagnostics/tests/smartCasts/inference/kt4009.kt new file mode 100644 index 00000000000..6fef9f3402e --- /dev/null +++ b/compiler/testData/diagnostics/tests/smartCasts/inference/kt4009.kt @@ -0,0 +1,21 @@ +trait PsiElement { + fun getText(): String + fun getParent(): PsiElement +} + +trait JetExpression : PsiElement + +fun foo1(e: PsiElement) { + var current: PsiElement? = e + var first = true + while (current != null) { + if (current is JetExpression && first) { + println(current!!.getText()) // error: smart cast not possible. But it's not needed in fact! + } + + current = current?.getParent() + } +} + +//from library +fun println(any: Any?) = throw Exception("$any") \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 994b7a62c75..747a6dbf332 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -5608,6 +5608,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/smartCasts/inference/kt2851.kt"); } + @TestMetadata("kt4009.kt") + public void testKt4009() throws Exception { + doTest("compiler/testData/diagnostics/tests/smartCasts/inference/kt4009.kt"); + } + } public static Test innerSuite() {