Skip to content

Commit

Permalink
Merge pull request #43563 from Thushara-Piyasekara/fix-record-value-o…
Browse files Browse the repository at this point in the history
…ptimizer

Fix records with function calls for default values failing code generation
  • Loading branch information
rdulmina authored Nov 28, 2024
2 parents b9bd73d + 6a957cc commit f1c60dc
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ private boolean containsOnlyConstantLoad(BIRNode.BIRFunction defaultFunction) {
return false;
}
return switch (firstBB.instructions.size()) {
case 1 -> firstBB.instructions.get(0).kind == CONST_LOAD;
case 2 -> firstBB.instructions.get(0).kind == CONST_LOAD && firstBB.instructions.get(1).kind == TYPE_CAST;
case 1 -> firstBB.instructions.get(0).kind == CONST_LOAD && firstBB.instructions.get(0).lhsOp.variableDcl
.kind == VarKind.RETURN;
case 2 -> firstBB.instructions.get(0).kind == CONST_LOAD && firstBB.instructions.get(1).kind == TYPE_CAST
&& firstBB.instructions.get(1).lhsOp.variableDcl.kind == VarKind.RETURN;
default -> false;
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.ballerinalang.test.record;

import org.ballerinalang.test.BCompileUtil;
import org.ballerinalang.test.BRunUtil;
import org.ballerinalang.test.CompileResult;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
* Test cases for record default functions in Ballerina.
*/
public class RecordDefaultFunctionsTest {

private CompileResult compileResult;

@BeforeClass
public void setup() {
compileResult = BCompileUtil.compile("test-src/record/record_default_functions.bal");
}

@Test(description = "Test record type with function invocations for default values")
public void testRecordWithDefaultFunction() {
Object returns = BRunUtil.invoke(compileResult, "recordDefaultFunctionWithArgument");
Object returns2 = BRunUtil.invoke(compileResult, "recordDefaultFunction");
Assert.assertNotNull(returns);
Assert.assertNotNull(returns2);
}

@AfterClass
public void tearDown() {
compileResult = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

isolated function getAsInt(string s) returns int {
return 777;
}

type Bar record {
int a = getAsInt("777");
};

function recordDefaultFunctionWithArgument() returns Bar {
return {};
}

isolated function returnString() returns string {
return "hello";
}

type Foo record {
string b = returnString();
};

function recordDefaultFunction () returns Foo {
return {};
}

0 comments on commit f1c60dc

Please sign in to comment.