diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIRRecordValueOptimizer.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIRRecordValueOptimizer.java index 5b1f5c0e8e7d..d48f5bd7a759 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIRRecordValueOptimizer.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/optimizer/BIRRecordValueOptimizer.java @@ -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; }; } diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/RecordDefaultFunctionsTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/RecordDefaultFunctionsTest.java new file mode 100644 index 000000000000..2f847f20e133 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/record/RecordDefaultFunctionsTest.java @@ -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; + } +} diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/record/record_default_functions.bal b/tests/jballerina-unit-test/src/test/resources/test-src/record/record_default_functions.bal new file mode 100644 index 000000000000..fba33ebbf979 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/record/record_default_functions.bal @@ -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 {}; +}