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

Fix records with function calls for default values failing code generation #43563

Merged
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 @@ -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 {};
}