Skip to content

Commit

Permalink
Merge pull request #38353 from pcnfernando/issue_37699
Browse files Browse the repository at this point in the history
Consider RHS true possibility when narrowing false path in a binary exp with logical AND
  • Loading branch information
hasithaa authored Jan 11, 2024
2 parents ee64af9 + 99101ac commit a26ad33
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,12 @@ private NarrowedTypes getNarrowedTypesForBinaryOp(Map<BVarSymbol, NarrowedTypes>
var nonLoggingContext = Types.IntersectionContext.typeTestIntersectionCalculationContext();
if (operator == OperatorKind.AND) {
trueType = types.getTypeIntersection(nonLoggingContext, lhsTrueType, rhsTrueType, this.env);
BType tmpType = types.getTypeIntersection(nonLoggingContext, lhsTrueType, rhsFalseType, this.env);
falseType = getTypeUnion(lhsFalseType, tmpType);
BType tmpType1 = types.getTypeIntersection(nonLoggingContext, lhsTrueType, rhsFalseType, this.env);
BType tmpType2 = types.getTypeIntersection(nonLoggingContext, lhsFalseType, rhsTrueType, this.env);
if (tmpType1.tag == TypeTags.SEMANTIC_ERROR) {
tmpType1 = tmpType2;
}
falseType = getTypeUnion(lhsFalseType, tmpType1);
} else {
BType tmpType = types.getTypeIntersection(nonLoggingContext, lhsFalseType, rhsTrueType, this.env);
trueType = lhsTypes.containsKey(symbol) ? getTypeUnion(lhsTrueType, tmpType) :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public void testTypeGuardSemanticsNegative() {
// 190, 17);
// BAssertUtil.validateError(negativeResult, i++,
// "incompatible types: expected 'string', found '(boolean|int|string)'", 192, 20);
BAssertUtil.validateError(negativeResult, i++,
"incompatible types: expected 'string', found 'boolean'", 192, 20);
BAssertUtil.validateError(negativeResult, i++, "incompatible types: expected 'int', found '(int|boolean)'",
199, 17);
BAssertUtil.validateError(negativeResult, i++, "incompatible types: expected 'string', found '(float|string)'",
Expand Down Expand Up @@ -751,6 +753,11 @@ public void testCustomCircularTupleTypeWithIsCheck() {
BRunUtil.invoke(result, "testCustomCircularTupleTypeWithIsCheck");
}

@Test
public void testSingletonTypeNarrowedTypeDesc() {
BRunUtil.invoke(result, "testSingletonTypeNarrowedTypeDesc");
}

@Test
public void testTypeGuardsAccountingForSemTypes1() {
CompileResult result = BCompileUtil.compile("test-src/statements/ifelse/test_type_guard_sem_types_1.bal");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,47 @@ function checkIsExpressionWithCircularTuple2(Type t) returns string? {
}
}

function testSingletonTypeNarrowedTypeDesc() {
assertEquality(testSingletonTypeNarrowedTypeDesc1(1,1), 1);
assertEquality(testSingletonTypeNarrowedTypeDesc2(3,1), 3);
assertEquality(testSingletonTypeNarrowedTypeDesc3("","abc"), "abc");
}

function testSingletonTypeNarrowedTypeDesc1(int foo, int bar) returns int {
if foo != 1 {
return foo;
} else if bar != 1 && foo == 1 {
return bar;
} else if foo == 1 && bar == 1 {
return 1;
}
return -1;
}

function testSingletonTypeNarrowedTypeDesc2(1|2|3 foo, int bar) returns int {
if foo == 1 {
return foo;
} else if bar != 1 && foo == 2 {
return bar;
} else if foo == 3 && bar == 1 {
return 3;
}
return -1;
}

function testSingletonTypeNarrowedTypeDesc3(string foo, string bar) returns string {
if foo != "" {
return foo;
}
if bar != "" && foo == "" {
return bar;
}
if foo == "" && bar == "" {
return "";
}
return "";
}

function assertTrue(anydata actual) {
assertEquality(true, actual);
}
Expand Down

0 comments on commit a26ad33

Please sign in to comment.