Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2201.3.x] Fix getting redeclared symbol error when the same symbol is used in two if-else blocks #39358

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2711,9 +2711,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)) {
gimantha marked this conversation as resolved.
Show resolved Hide resolved
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