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.10.x] Desugar field access expression to if statement expression #43773

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 @@ -6335,12 +6335,13 @@ public void visit(BLangSimpleVarRef varRefExpr) {

@Override
public void visit(BLangFieldBasedAccess.BLangNSPrefixedFieldBasedAccess nsPrefixedFieldBasedAccess) {
rewriteFieldBasedAccess(nsPrefixedFieldBasedAccess);
rewriteFieldBasedAccess(nsPrefixedFieldBasedAccess, true);
}

private void rewriteFieldBasedAccess(BLangFieldBasedAccess fieldAccessExpr) {
private void rewriteFieldBasedAccess(BLangFieldBasedAccess fieldAccessExpr, boolean prefixedFieldBased) {
if (safeNavigate(fieldAccessExpr)) {
result = rewriteExpr(rewriteSafeNavigationExpr(fieldAccessExpr));
result = prefixedFieldBased ? rewriteExpr(rewriteSafeNavigationExpr(fieldAccessExpr)) :
rewriteExpr(rewriteSafeFieldBasedAccessExpr(fieldAccessExpr));
return;
}

Expand Down Expand Up @@ -6434,7 +6435,7 @@ private void rewriteFieldBasedAccess(BLangFieldBasedAccess fieldAccessExpr) {

@Override
public void visit(BLangFieldBasedAccess fieldAccessExpr) {
rewriteFieldBasedAccess(fieldAccessExpr);
rewriteFieldBasedAccess(fieldAccessExpr, false);
}

private BLangNode rewriteObjectMemberAccessAsField(BLangFieldBasedAccess fieldAccessExpr) {
Expand Down Expand Up @@ -10056,6 +10057,98 @@ private boolean safeNavigate(BLangAccessExpression accessExpr) {
return false;
}

private BLangExpression rewriteSafeFieldBasedAccessExpr(BLangAccessExpression accessExpr) {
/*
* Following is the structure of the safe navigation desugar.
* Before :
* type A {
* B b;
* }
* type B {
* int c;
* }
* int? res = a?.b?.c
*
* After desugar :
* any|error temp_result;
* temp_result = a;
* if temp_result !is error? {
* temp_result = (<A> temp_result).b;
* if temp_result !is error? {
* temp_result = (<B> temp_result).c;
* }
* }
* int? res = <int> temp_result;
*/
Location pos = accessExpr.pos;
// Create block statement to hold desugared statements.
BLangBlockStmt blockStmt = ASTBuilderUtil.createBlockStmt(pos);

// Create a temp variable to hold the intermediate result of the access expression.
String matchTempResultVarName = GEN_VAR_PREFIX.value + "temp_result";
BLangSimpleVariableDef tempResultVarDef = createVarDef(matchTempResultVarName, symTable.anyOrErrorType,
null, pos);
blockStmt.addStatement(tempResultVarDef);
BLangSimpleVarRef tempResultVarRef = ASTBuilderUtil.createVariableRef(pos, tempResultVarDef.var.symbol);

createNestedIfForAccessExpression(blockStmt, accessExpr, tempResultVarRef, pos);

BLangStatementExpression stmtExpr = createStatementExpression(blockStmt,
types.addConversionExprIfRequired(tempResultVarRef, accessExpr.getBType()));
stmtExpr.setBType(accessExpr.getBType());
return stmtExpr;
}

private BLangIf createNestedIfForAccessExpression(BLangBlockStmt blockStmt, BLangAccessExpression accessExpr,
BLangSimpleVarRef tempResultVarRef, Location pos) {
NodeKind kind = accessExpr.expr.getKind();
if (kind == NodeKind.FIELD_BASED_ACCESS_EXPR) {
BLangIf ifStmt = createNestedIfForAccessExpression(blockStmt, (BLangAccessExpression) accessExpr.expr,
tempResultVarRef, pos);
blockStmt = ifStmt.body;
} else {
// this else block only reach one time in the recursive execution
// this will assign the initial value for the `tempResultVarRef` eg: `temp_result = a;` in the sample
blockStmt.addStatement(ASTBuilderUtil.createAssignmentStmt(pos, tempResultVarRef, accessExpr.expr));
}

// Condition
BLangType testTypeNode = null;
if (accessExpr.errorSafeNavigation && accessExpr.nilSafeNavigation) {
testTypeNode = getErrorOrNillTypeNode();
} else if (accessExpr.errorSafeNavigation) {
testTypeNode = getErrorTypeNode();
} else if (accessExpr.nilSafeNavigation) {
testTypeNode = getNillTypeNode();
}

BLangExpression conditionExpr;
if (testTypeNode == null) {
conditionExpr = getBooleanLiteral(true);
} else {
BLangTypeTestExpr isNilOrErrorTest = createTypeCheckExpr(pos, tempResultVarRef, testTypeNode);
isNilOrErrorTest.isNegation = true;
isNilOrErrorTest.setBType(symTable.booleanType);
conditionExpr = isNilOrErrorTest;
}

// If body
BLangAccessExpression tempAccessExpr = nodeCloner.cloneNode(accessExpr);
BType type = types.getSafeType(accessExpr.expr.getBType(), true, true);
tempAccessExpr.expr = types.addConversionExprIfRequired(tempResultVarRef, type);
tempAccessExpr.errorSafeNavigation = false;
tempAccessExpr.nilSafeNavigation = false;
BLangAssignment assignmentStmt =
ASTBuilderUtil.createAssignmentStmt(symTable.builtinPos, tempResultVarRef, tempAccessExpr);
BLangBlockStmt ifBody = ASTBuilderUtil.createBlockStmt(symTable.builtinPos);
ifBody.addStatement(assignmentStmt);

BLangIf ifStmt = ASTBuilderUtil.createIfStmt(pos, blockStmt);
ifStmt.setCondition(conditionExpr);
ifStmt.body = ifBody;
return ifStmt;
}

private BLangExpression rewriteSafeNavigationExpr(BLangAccessExpression accessExpr) {
BType originalExprType = accessExpr.getBType();
// Create a temp variable to hold the intermediate result of the acces expression.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ public void testValidXMLmapFieldAccess() {
BRunUtil.invoke(result, "testValidXMLmapFieldAccess");
}

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

@Test(dataProvider = "fieldAccessOnJsonTypedRecordFields")
public void testFieldAccessOnJsonTypedRecordFields(String function) {
BRunUtil.invoke(result, function);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,17 @@ function testValidXMLmapFieldAccess() {
assertEquals(y, null);
}

function testLargeChainingFieldAccess() {
// This test is to check the performance of the field access chaining and possible OOM and large method errors
json a = { "a": "b" };
anydata|error b = a?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?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.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b;
anydata|error d = a?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b?.b;
assertTrue(b is ());
assertTrue(c is ());
assertTrue(d is ());
}

isolated function isEqual(anydata|error val1, anydata|error val2) returns boolean {
if (val1 is anydata && val2 is anydata) {
return (val1 == val2);
Expand Down
Loading