From 22005b014d778ba10f01924709ce182ce8ac1ff9 Mon Sep 17 00:00:00 2001 From: lasinicl Date: Tue, 24 Jan 2023 15:20:39 +0530 Subject: [PATCH 1/2] Fix adding unnecessary type narrowed info --- .../compiler/semantics/analyzer/SemanticAnalyzer.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java index 2d12df31002f..5d392a2876ec 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/semantics/analyzer/SemanticAnalyzer.java @@ -2835,9 +2835,7 @@ public void visit(BLangIf ifNode, AnalyzerData data) { Map existingNarrowedTypeInfo = ifNode.expr.narrowedTypeInfo; for (Map.Entry entry : data.narrowedTypeInfo.entrySet()) { BVarSymbol key = entry.getKey(); - if (!existingNarrowedTypeInfo.containsKey(key)) { - existingNarrowedTypeInfo.put(key, entry.getValue()); - } else { + if (existingNarrowedTypeInfo.containsKey(key)) { BType.NarrowedTypes existingNarrowTypes = existingNarrowedTypeInfo.get(key); BUnionType unionType = BUnionType.create(null, existingNarrowTypes.trueType, existingNarrowTypes.falseType); From a3f887f3366b5a78384401c91811aeef19c55f82 Mon Sep 17 00:00:00 2001 From: lasinicl Date: Tue, 24 Jan 2023 15:21:09 +0530 Subject: [PATCH 2/2] Add tests --- .../statements/ifelse/IfElseStmtTest.java | 16 +++ .../test-src/statements/ifelse/if-stmt.bal | 119 ++++++++++++++++++ 2 files changed, 135 insertions(+) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/ifelse/IfElseStmtTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/ifelse/IfElseStmtTest.java index a1aae88fa785..5cf4dcf4a5d2 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/ifelse/IfElseStmtTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/statements/ifelse/IfElseStmtTest.java @@ -27,6 +27,7 @@ import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; /** @@ -287,6 +288,21 @@ public void ifStmtTypeNarrowingTest() { Assert.assertEquals(returns.toString(), "ballerina"); } + @Test(dataProvider = "dataToTestSymbolsInIfElseStmt") + public void testSymbolsInIfElseStmt(String functionName) { + BRunUtil.invoke(result, functionName); + } + + @DataProvider + public Object[] dataToTestSymbolsInIfElseStmt() { + return new Object[]{ + "testSymbolsInIfElse1", + "testSymbolsInIfElse2", + "testSymbolsInIfElse3", + "testSymbolsInIfElse4" + }; + } + @AfterClass public void tearDown() { result = null; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/statements/ifelse/if-stmt.bal b/tests/jballerina-unit-test/src/test/resources/test-src/statements/ifelse/if-stmt.bal index eeb81927c3a2..85b32b70cbd1 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/statements/ifelse/if-stmt.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/statements/ifelse/if-stmt.bal @@ -217,6 +217,125 @@ function testResetTypeNarrowingWithBlockStmt() { } } +function testSymbolsInIfElse1() { + int? x = (); + if x is () { + int a = 3; + int b = 12; + b += 1; + if b == 13 { + a += 1; + b = 1; + } + } else { + int a = 3; + int b = 12; + } +} + +function testSymbolsInIfElse2() { + int? x = (); + if x is () { + int a = 2; + int b = 6; + b += 1; + if b == 7 { + a += 1; + b = 1; + if a == 3 { + a += 2; + b += 2; + } + } + int c = 10; + int d = 20; + if c == 7 { + a += 1; + b = 1; + if a == 6 { + a += 2; + b += 2; + } + } + } else { + int a = 5; + int b = 12; + if a == 4 { + int c = 2; + int d = 3; + } else { + int c = 2; + int d = 3; + } + } +} + +function testSymbolsInIfElse3() { + int|string? x = (); + if x is () { + int a = 12; + int b = 12; + b += 1; + if b == 13 { + a += 1; + b = 1; + } + if a == 2 { + a += 2; + b += 2; + } + } else if x is string { + int a = 2022; + int b = 12; + } else { + int a = 10; + int b = 12; + } +} + +function testSymbolsInIfElse4() { + int|string? x = (); + if x is () { + int? a = 12; + int? b = 12; + if b is int { + b += 1; + int? c = 1; + if c is int { + int|string? d = (); + if d is int { + c = 2; + a = 2; + } + if d is string { + d = 2; + b = 3; + } + } else { + int d = 2; + a = 2; + b = 3; + c = 10; + } + } + if a is int { + if a == 0 { + int d = 12; + } + int c = 10; + } + } else { + int a = 12; + int b = 10; + if a == 10 { + int c = 10; + if c == 10 { + int d = 12; + } + } + } +} + function assertTrue(any|error actual) { assertEquality(true, actual); }