Skip to content

Commit

Permalink
Merge pull request #42968 from mindula/fix-42327
Browse files Browse the repository at this point in the history
Fix NPE in lambda functions with check expressions
  • Loading branch information
chiranSachintha authored Jul 31, 2024
2 parents 7dbb5d5 + 13f84d8 commit b996088
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,8 @@ public void visit(BLangResourceFunction resourceFunction) {

@Override
public void visit(BLangFunction funcNode) {
BLangOnFailClause onFailClause = this.onFailClause;
this.onFailClause = null;
Map<Name, BLangStatement> prevXmlnsDecls = this.stmtsToBePropagatedToQuery;
if (!funcNode.flagSet.contains(Flag.QUERY_LAMBDA)) {
this.stmtsToBePropagatedToQuery = new HashMap<>();
Expand All @@ -1382,6 +1384,7 @@ public void visit(BLangFunction funcNode) {
}
this.stmtsToBePropagatedToQuery = prevXmlnsDecls;
result = funcNode;
this.onFailClause = onFailClause;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,8 @@ private void validateParams(BLangFunction funcNode, AnalyzerData data) {
}

private void visitFunction(BLangFunction funcNode, AnalyzerData data) {
boolean failureHandled = data.failureHandled;
data.failureHandled = false;
data.env = SymbolEnv.createFunctionEnv(funcNode, funcNode.symbol.scope, data.env);
data.returnWithinTransactionCheckStack.push(true);
data.returnTypes.push(new LinkedHashSet<>());
Expand All @@ -523,6 +525,7 @@ private void visitFunction(BLangFunction funcNode, AnalyzerData data) {
data.returnTypes.pop();
data.returnWithinTransactionCheckStack.pop();
data.transactionalFuncCheckStack.pop();
data.failureHandled = failureHandled;
}

private boolean isPublicInvokableNode(BLangInvokableNode invNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public void testCheckedErrorvsReturnTypeMismatch() {
BAssertUtil.validateError(compile, i++, ERROR_MISMATCH_ERR_MSG, 76, 20);
BAssertUtil.validateError(compile, i++, ERROR_MISMATCH_ERR_MSG, 76, 31);
BAssertUtil.validateError(compile, i++, ERROR_MISMATCH_ERR_MSG, 85, 13);
BAssertUtil.validateError(compile, i++, ERROR_MISMATCH_ERR_MSG, 92, 28);
BAssertUtil.validateError(compile, i++, ERROR_MISMATCH_ERR_MSG, 107, 24);
Assert.assertEquals(compile.getWarnCount(), 6);
Assert.assertEquals(compile.getErrorCount(), i - 6);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,32 @@ function testCheckedExprWithNoErrorType3() {
int|error i = error("Error");
int _ = check i;
}

function testOnFailWithCheckInLambdaFunction() {
function _ = function() {
do {
function _ = function() {
string _ = check getVal("");
};
} on fail {
}
};
}

function getVal(string str) returns string|error {
return "";
}

function testOnFailWithCheckInWorkers() {
string[] s = [];
worker A {
s.forEach(function(string k) {
string _ = check getVal2();
});
} on fail {
}
}

function getVal2() returns string|error {
return "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,18 @@ function assertEquality(any|error expected, any|error actual) {
panic error(ASSERTION_ERROR_REASON,
message = "expected '" + expectedValAsString + "', found '" + actualValAsString + "'");
}

function testOnFailWithCheckAndReturnType(function s = function() {
do {
function _ = function() returns error? {
string _ = check getVal("");
};
} on fail {
}
}) returns string|error {
return "";
}

function getVal(string s) returns string|error {
return "";
}

0 comments on commit b996088

Please sign in to comment.