Skip to content

Commit

Permalink
Merge pull request #40679 from ushirask/pr_39358
Browse files Browse the repository at this point in the history
Fix getting redeclared symbol error when the same symbol is used in two if-else blocks
  • Loading branch information
hasithaa authored Aug 15, 2023
2 parents f262094 + 27f05db commit b59f6f1
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2837,9 +2837,7 @@ public void visit(BLangIf ifNode, AnalyzerData data) {
Map<BVarSymbol, BType.NarrowedTypes> existingNarrowedTypeInfo = ifNode.expr.narrowedTypeInfo;
for (Map.Entry<BVarSymbol, BType.NarrowedTypes> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit b59f6f1

Please sign in to comment.