From 5c63dd413a7816054ad52a2a47e13b94db603960 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Wed, 22 Jul 2020 11:08:51 +0700 Subject: [PATCH] IDEA-246334 Dataflow to here: filtering `long` variable with `<= 0` yields no results. GitOrigin-RevId: 80981136f8e8541d4c5b872ccf851de9e550aa72 --- .../src/com/intellij/slicer/DfaBasedFilter.java | 12 +++++++++--- .../slice/backward/FilterDoubleByInt.java | 6 ++++++ .../codeInsight/slice/backward/FilterLongByInt.java | 6 ++++++ .../com/intellij/java/slicer/SliceBackwardTest.java | 2 ++ 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/slice/backward/FilterDoubleByInt.java create mode 100644 java/java-tests/testData/codeInsight/slice/backward/FilterLongByInt.java diff --git a/java/java-impl/src/com/intellij/slicer/DfaBasedFilter.java b/java/java-impl/src/com/intellij/slicer/DfaBasedFilter.java index 3bb9cfba365c..1bd4a38a84d7 100644 --- a/java/java-impl/src/com/intellij/slicer/DfaBasedFilter.java +++ b/java/java-impl/src/com/intellij/slicer/DfaBasedFilter.java @@ -53,9 +53,10 @@ final class DfaBasedFilter { private boolean allowed(@NotNull PsiElement element, boolean assertionsDisabled) { if (myDfType instanceof DfConstantType && element instanceof PsiLiteralValue) { - Object constValue = ((DfConstantType)myDfType).getValue(); + DfConstantType dfConstantType = (DfConstantType)myDfType; + Object constValue = dfConstantType.getValue(); if (!(constValue instanceof PsiElement)) { - Object value = ((PsiLiteralValue)element).getValue(); + Object value = TypeConversionUtil.computeCastTo(((PsiLiteralValue)element).getValue(), dfConstantType.getPsiType()); return Objects.equals(value, constValue); } } @@ -87,7 +88,12 @@ final class DfaBasedFilter { CommonDataflow.DataflowResult result = CommonDataflow.getDataflowResult(expression); if (result == null) return DfTypes.TOP; expression = PsiUtil.skipParenthesizedExprDown(expression); - return assertionsDisabled ? result.getDfTypeNoAssertions(expression) : result.getDfType(expression); + DfType type = assertionsDisabled ? result.getDfTypeNoAssertions(expression) : result.getDfType(expression); + if (myDfType instanceof DfLongType && type instanceof DfIntType) { + // Implicit widening conversion + return DfTypes.longRange(((DfIntType)type).getRange()); + } + return type; } @Override diff --git a/java/java-tests/testData/codeInsight/slice/backward/FilterDoubleByInt.java b/java/java-tests/testData/codeInsight/slice/backward/FilterDoubleByInt.java new file mode 100644 index 000000000000..7eac7ee52f97 --- /dev/null +++ b/java/java-tests/testData/codeInsight/slice/backward/FilterDoubleByInt.java @@ -0,0 +1,6 @@ +class Test { + public static void main(String[] args) { + double x = 0; + System.out.println(x); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/slice/backward/FilterLongByInt.java b/java/java-tests/testData/codeInsight/slice/backward/FilterLongByInt.java new file mode 100644 index 000000000000..31752521332b --- /dev/null +++ b/java/java-tests/testData/codeInsight/slice/backward/FilterLongByInt.java @@ -0,0 +1,6 @@ +class Test { + public static void main(String[] args) { + long x = 0; + System.out.println(x); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/slicer/SliceBackwardTest.java b/java/java-tests/testSrc/com/intellij/java/slicer/SliceBackwardTest.java index bfefd73aff0d..2c3efd051c5a 100644 --- a/java/java-tests/testSrc/com/intellij/java/slicer/SliceBackwardTest.java +++ b/java/java-tests/testSrc/com/intellij/java/slicer/SliceBackwardTest.java @@ -114,6 +114,8 @@ public class SliceBackwardTest extends SliceTestCase { public void testFilterPropagateBoolean2() throws Exception { doTest("false");} public void testFilterAssertionViolation() throws Exception { doTest("-1");} public void testReturnParameter() throws Exception { doTest(); } + public void testFilterLongByInt() throws Exception { doTest("<=0"); } + public void testFilterDoubleByInt() throws Exception { doTest("0.0"); } public void testStackFilterSimple() throws Exception { doTest("null", "MainTest:test", "MainTest:foo", "MainTest:main");