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

Add support for error binding patterns in the on fail clause #41049

Merged
merged 56 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
1062f62
Add tests and changes for on fail error bp support
suleka96 Apr 12, 2023
72fa0e2
Update pull_request_ubuntu_build.yml
suleka96 Apr 13, 2023
d30d0c5
Update pull_request_windows_build.yml
suleka96 Apr 13, 2023
9df8d27
Update parser logic and tests
suleka96 Apr 18, 2023
c43bb84
Update for each test
suleka96 Apr 18, 2023
bd2cf04
Update tree modifier
suleka96 Apr 18, 2023
c3c12af
Skip recovery formatter tests
suleka96 Apr 19, 2023
b3f361a
Merge branch 'on-fail-fix' of https://github.com/suleka96/ballerina-l…
suleka96 Apr 19, 2023
983e08b
Add nodeBuilder change
suleka96 Apr 19, 2023
8d57c80
Update column pos for tests
suleka96 Apr 19, 2023
0a735b1
Add semantic analyzer changes
suleka96 Apr 21, 2023
18d8ca7
Add new bp tests
suleka96 Apr 23, 2023
6edaf15
Add dataFlow, codeAnalyzer and desugar changes
suleka96 Apr 24, 2023
512d310
Update parser logic
suleka96 Apr 25, 2023
9e46ae6
Remove import
suleka96 Apr 25, 2023
6644e9a
Merge branch 'on-fail-fix' of https://github.com/suleka96/ballerina-l…
suleka96 Apr 25, 2023
702e132
Add all tests
suleka96 May 19, 2023
d275b77
Update desugar code
suleka96 May 22, 2023
664c9a2
Update Do tests
suleka96 May 23, 2023
0f1c4e5
Update transaction test
suleka96 May 24, 2023
29ba158
Remove helper method
suleka96 May 26, 2023
41c7246
Merge pull request #40221 from suleka96/on-fail-fix
MaryamZi May 26, 2023
c2f300b
Merge branch 'on-fail-fix' of https://github.com/suleka96/ballerina-l…
suleka96 Jun 8, 2023
563e1d4
Merge branch 'on-fail-error-bp' of https://github.com/ballerina-platf…
suleka96 Jun 8, 2023
ff22128
Merge pull request #40293 from suleka96/on-fail-compiler
MaryamZi Jul 18, 2023
78bfb88
Resolve conflicts and merge branch 'master' of https://github.com/bal…
MaryamZi Jul 18, 2023
b88ee2f
Merge pull request #41047 from MaryamZi/on-fail-error-bp
MaryamZi Jul 18, 2023
38e6d1f
Address basic review suggestions
MaryamZi Jul 18, 2023
8597873
Fix analysis with checkpanic and remove duplicate error
MaryamZi Jul 19, 2023
261c4a1
Fix error variable symbol and update tests
MaryamZi Jul 19, 2023
f58f3f7
Merge pull request #41055 from MaryamZi/on-fail-error-bp
MaryamZi Jul 19, 2023
767d87c
Refactor code
MaryamZi Jul 20, 2023
ebed5f6
Refactor code
MaryamZi Jul 20, 2023
9518f4f
Add tests
MaryamZi Jul 20, 2023
c2a827f
Remove the feature branch from workflows
MaryamZi Jul 21, 2023
1d252eb
Merge branch 'master' of https://github.com/ballerina-platform/baller…
MaryamZi Jul 21, 2023
e45ac1a
Fix diagnostic ID
MaryamZi Jul 21, 2023
6abc96d
Add back original NodeFactory method and deprecate it
MaryamZi Jul 24, 2023
a44aaba
Refactor code
MaryamZi Jul 24, 2023
c2f9a0a
Merge pull request #41163 from ballerina-platform/master
MaryamZi Aug 7, 2023
d76338e
Merge pull request #41200 from ballerina-platform/master
MaryamZi Aug 14, 2023
f736039
Resolve conflicts and merge branch 'master' of https://github.com/bal…
MaryamZi Aug 14, 2023
92da94e
Resolve conflicts and merge branch 'master' of https://github.com/bal…
MaryamZi Aug 16, 2023
389b411
Update now invalid test
MaryamZi Aug 16, 2023
199f272
Resolve conflicts and merge branch 'master' of https://github.com/bal…
MaryamZi Aug 18, 2023
2d5c1bb
Merge pull request #41344 from ballerina-platform/master
MaryamZi Sep 5, 2023
963520d
Merge pull request #41379 from ballerina-platform/master
MaryamZi Sep 13, 2023
ec995aa
Merge pull request #41486 from ballerina-platform/master
MaryamZi Oct 9, 2023
521dcd5
Resolve conflicts and merge branch 'master' of https://github.com/bal…
MaryamZi Oct 23, 2023
222f18a
Address review suggestion
MaryamZi Oct 24, 2023
05a39ea
Merge branch 'master' of https://github.com/ballerina-platform/baller…
MaryamZi Nov 2, 2023
1d709e3
Merge branch 'master' of https://github.com/ballerina-platform/baller…
MaryamZi Nov 15, 2023
bbbbf35
Improve errors
MaryamZi Nov 17, 2023
9041b64
Merge branch 'master' of https://github.com/ballerina-platform/baller…
MaryamZi Nov 21, 2023
781e3ee
Fix check for errors
MaryamZi Nov 21, 2023
a99b983
Merge pull request #41760 from ballerina-platform/master
MaryamZi Nov 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,6 @@ public enum DiagnosticErrorCode implements DiagnosticCode {

FAIL_EXPR_NO_MATCHING_ERROR_RETURN_IN_ENCL_INVOKABLE(
"BCE3035", "fail.expr.no.matching.error.return.in.encl.invokable"),
INCOMPATIBLE_ON_FAIL_ERROR_DEFINITION("BCE3036", "on.fail.no.matching.error"),

START_REQUIRE_INVOCATION("BCE3037", "start.require.invocation"),
INVALID_EXPR_STATEMENT("BCE3038", "invalid.expr.statement"),
Expand Down Expand Up @@ -809,7 +808,9 @@ public enum DiagnosticErrorCode implements DiagnosticCode {
QUERY_CONSTRUCT_TYPES_CANNOT_BE_USED_WITH_COLLECT("BCE4049", "query.construct.types.cannot.be.used.with.collect"),
VARIABLE_IS_SEQUENCED_MORE_THAN_ONCE("BCE4050", "variable.is.sequenced.more.than.once"),
INVALID_GROUPING_KEY_TYPE("BCE4051", "invalid.grouping.key.type"),
NAMED_ARG_NOT_ALLOWED_FOR_REST_PARAM("BCE4052", "named.arg.not.allowed.for.rest.param")
NAMED_ARG_NOT_ALLOWED_FOR_REST_PARAM("BCE4052", "named.arg.not.allowed.for.rest.param"),

INVALID_BINDING_PATTERN_IN_ON_FAIL("BCS4053", "invalid.binding.pattern.in.on.fail")
MaryamZi marked this conversation as resolved.
Show resolved Hide resolved
;

private String diagnosticId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.wso2.ballerinalang.compiler.tree.BLangService;
import org.wso2.ballerinalang.compiler.tree.BLangSimpleVariable;
import org.wso2.ballerinalang.compiler.tree.BLangTupleVariable;
import org.wso2.ballerinalang.compiler.tree.BLangVariable;
import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangBindingPattern;
import org.wso2.ballerinalang.compiler.tree.bindingpatterns.BLangCaptureBindingPattern;
import org.wso2.ballerinalang.compiler.tree.clauses.BLangMatchClause;
Expand Down Expand Up @@ -552,6 +553,21 @@ static BLangRecordVariableDef createRecordVariableDef(Location pos, BLangRecordV
return variableDef;
}

static BLangErrorVariable createErrorVariable(Location pos, BType type, BLangExpression expr,
BLangSimpleVariable message, BLangVariable cause,
BLangSimpleVariable restDetail,
List<BLangErrorVariable.BLangErrorDetailEntry> detail) {
final BLangErrorVariable errVariable = (BLangErrorVariable) TreeBuilder.createErrorVariableNode();
errVariable.pos = pos;
errVariable.setBType(type);
errVariable.expr = expr;
errVariable.message = message;
errVariable.cause = cause;
errVariable.restDetail = restDetail;
errVariable.detail = detail;
return errVariable;
}

static BLangErrorVariableDef createErrorVariableDef(Location pos, BLangErrorVariable variable) {
final BLangErrorVariableDef variableDef =
(BLangErrorVariableDef) TreeBuilder.createErrorVariableDefinitionNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5163,18 +5163,8 @@ private BLangFail createOnFailInvocation(BLangOnFailClause onFailClause, BLangFa
onFailBody.stmts.addAll(onFailClause.body.stmts);
env.scope.entries.putAll(onFailClause.body.scope.entries);
onFailBody.failureBreakMode = onFailClause.body.failureBreakMode;
VariableDefinitionNode onFailVarDefNode = onFailClause.variableDefinitionNode;

if (onFailVarDefNode != null) {
BVarSymbol onFailErrorVariableSymbol =
((BLangSimpleVariableDef) onFailVarDefNode).var.symbol;
BLangSimpleVariable errorVar = ASTBuilderUtil.createVariable(onFailErrorVariableSymbol.pos,
onFailErrorVariableSymbol.name.value, onFailErrorVariableSymbol.type, rewrite(fail.expr, env),
onFailErrorVariableSymbol);
BLangSimpleVariableDef errorVarDef = ASTBuilderUtil.createVariableDef(onFailClause.pos, errorVar);
env.scope.define(onFailErrorVariableSymbol.name, onFailErrorVariableSymbol);
onFailBody.stmts.add(0, errorVarDef);
}
handleOnFailErrorVarDefNode(onFailClause.variableDefinitionNode, onFailBody, fail);

if (onFailClause.isInternal && fail.exprStmt != null) {
if (fail.exprStmt instanceof BLangPanic) {
Expand All @@ -5193,6 +5183,37 @@ onFailErrorVariableSymbol.name.value, onFailErrorVariableSymbol.type, rewrite(fa
return fail;
}

private void handleOnFailErrorVarDefNode(VariableDefinitionNode onFailVarDefNode, BLangBlockStmt onFailBody,
BLangFail fail) {
if (onFailVarDefNode == null) {
return;
}

BLangVariable variableNode = (BLangVariable) onFailVarDefNode.getVariable();
onFailBody.stmts.add(0,
variableNode.getKind() == NodeKind.ERROR_VARIABLE ?
handleErrorBPInOnFail((BLangErrorVariable) variableNode, fail) :
handleCaptureBPInOnFail((BLangSimpleVariable) variableNode, fail));
}

private BLangStatement handleErrorBPInOnFail(BLangErrorVariable varNode, BLangFail fail) {
BLangErrorVariable errorVar = ASTBuilderUtil.createErrorVariable(varNode.pos,
varNode.getBType(), rewrite(fail.expr, env), varNode.message, varNode.cause, varNode.restDetail,
varNode.detail);
BLangErrorVariableDef errorVarDef = ASTBuilderUtil.createErrorVariableDef(varNode.pos, errorVar);
return rewrite(errorVarDef, env);
}

private BLangStatement handleCaptureBPInOnFail(BLangSimpleVariable varNode, BLangFail fail) {
BVarSymbol onFailErrorVariableSymbol = varNode.symbol;
BLangSimpleVariable errorVar = ASTBuilderUtil.createVariable(onFailErrorVariableSymbol.pos,
onFailErrorVariableSymbol.name.value, onFailErrorVariableSymbol.type, rewrite(fail.expr, env),
onFailErrorVariableSymbol);
BLangSimpleVariableDef errorVarDef = ASTBuilderUtil.createVariableDef(onFailClause.pos, errorVar);
env.scope.define(onFailErrorVariableSymbol.name, onFailErrorVariableSymbol);
return errorVarDef;
}

private BLangBlockStmt rewriteNestedOnFail(BLangOnFailClause onFailClause, BLangFail fail) {
BLangOnFailClause currentOnFail = this.onFailClause;

Expand All @@ -5201,18 +5222,8 @@ private BLangBlockStmt rewriteNestedOnFail(BLangOnFailClause onFailClause, BLang
onFailBody.scope = onFailClause.body.scope;
onFailBody.mapSymbol = onFailClause.body.mapSymbol;
onFailBody.failureBreakMode = onFailClause.body.failureBreakMode;
VariableDefinitionNode onFailVarDefNode = onFailClause.variableDefinitionNode;

if (onFailVarDefNode != null) {
BVarSymbol onFailErrorVariableSymbol =
((BLangSimpleVariableDef) onFailVarDefNode).var.symbol;
BLangSimpleVariable errorVar = ASTBuilderUtil.createVariable(onFailErrorVariableSymbol.pos,
onFailErrorVariableSymbol.name.value, onFailErrorVariableSymbol.type, rewrite(fail.expr, env),
onFailErrorVariableSymbol);
BLangSimpleVariableDef errorVarDef = ASTBuilderUtil.createVariableDef(onFailClause.pos, errorVar);
onFailBody.scope.define(onFailErrorVariableSymbol.name, onFailErrorVariableSymbol);
onFailBody.stmts.add(0, errorVarDef);
}

handleOnFailErrorVarDefNode(onFailClause.variableDefinitionNode, onFailBody, fail);

int currentOnFailIndex = this.enclosingOnFailClause.indexOf(this.onFailClause);
int enclosingOnFailIndex = currentOnFailIndex <= 0 ? this.enclosingOnFailClause.size() - 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3949,25 +3949,13 @@ public BLangNode transform(OnFailClauseNode onFailClauseNode) {
Location pos = getPosition(onFailClauseNode);
BLangOnFailClause onFailClause = (BLangOnFailClause) TreeBuilder.createOnFailClauseNode();
onFailClause.pos = pos;
onFailClauseNode.typeDescriptor().ifPresent(typeDescriptorNode -> {
BLangSimpleVariableDef variableDefinitionNode =
(BLangSimpleVariableDef) TreeBuilder.createSimpleVariableDefinitionNode();
BLangSimpleVariable var = (BLangSimpleVariable) TreeBuilder.createSimpleVariableNode();
boolean isDeclaredWithVar = typeDescriptorNode.kind() == SyntaxKind.VAR_TYPE_DESC;
var.isDeclaredWithVar = isDeclaredWithVar;
if (!isDeclaredWithVar) {
var.setTypeNode(createTypeNode(typeDescriptorNode));
}
var.pos = pos;
onFailClauseNode.failErrorName().ifPresent(identifierToken -> {
var.setName(this.createIdentifier(identifierToken));
var.name.pos = getPosition(identifierToken);
variableDefinitionNode.setVariable(var);
variableDefinitionNode.pos = getPosition(typeDescriptorNode,
identifierToken);
});
onFailClause.isDeclaredWithVar = isDeclaredWithVar;
markVariableWithFlag(variableDefinitionNode.getVariable(), Flag.FINAL);
onFailClauseNode.typedBindingPattern().ifPresent(typedBindingPatternNode -> {
VariableDefinitionNode variableDefinitionNode =
createBLangVarDef(getPosition(typedBindingPatternNode), typedBindingPatternNode,
Optional.empty(), Optional.empty());
onFailClause.isDeclaredWithVar =
typedBindingPatternNode.typeDescriptor().kind() == SyntaxKind.VAR_TYPE_DESC;
markVariableWithFlag((BLangVariable) variableDefinitionNode.getVariable(), Flag.FINAL);
onFailClause.variableDefinitionNode = variableDefinitionNode;
});
BLangBlockStmt blockNode = (BLangBlockStmt) transform(onFailClauseNode.blockStatement());
Expand Down
Loading