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

Make function rest parameters final #41844

Merged
merged 5 commits into from
Jan 23, 2024
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 @@ -498,6 +498,11 @@ public void visit(BLangFunction funcNode, AnalyzerData data) {
//set function param flag to final
funcNode.symbol.params.forEach(param -> param.flags |= Flags.FUNCTION_FINAL);

BVarSymbol restParamSym = funcNode.symbol.restParam;
if (restParamSym != null) {
restParamSym.flags |= Flags.FUNCTION_FINAL;
}

if (!funcNode.flagSet.contains(Flag.WORKER)) {
// annotation validation for workers is done for the invocation.
funcNode.annAttachments.forEach(annotationAttachment -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,21 @@ public void setup() {
public void testFinalFailCase() {
CompileResult compileResultNegative = BCompileUtil.compile(
"test-src/types/finaltypes/test_implicitly_final_negative.bal");
Assert.assertEquals(compileResultNegative.getErrorCount(), 8);
BAssertUtil.validateError(compileResultNegative, 0, "cannot assign a value to function argument 'a'", 11, 5);
BAssertUtil.validateError(compileResultNegative, 1, "cannot assign a value to function argument 'a'", 17, 5);
BAssertUtil.validateError(compileResultNegative, 2, "cannot assign a value to function argument 'f'", 22, 5);
BAssertUtil.validateError(compileResultNegative, 3, "cannot assign a value to function argument 's'", 23, 5);
BAssertUtil.validateError(compileResultNegative, 4, "cannot assign a value to function argument 'b'", 24, 5);
BAssertUtil.validateError(compileResultNegative, 5, "cannot assign a value to function argument 'j'", 25, 5);
BAssertUtil.validateError(compileResultNegative, 6, "cannot assign a value to function argument 'a'", 38, 5);
BAssertUtil.validateError(compileResultNegative, 7, "invalid assignment: 'listener' declaration is final",
int i = 0;
BAssertUtil.validateError(compileResultNegative, i++, "cannot assign a value to function argument 'a'", 11, 5);
BAssertUtil.validateError(compileResultNegative, i++, "cannot assign a value to function argument 'a'", 17, 5);
BAssertUtil.validateError(compileResultNegative, i++, "cannot assign a value to function argument 'f'", 22, 5);
BAssertUtil.validateError(compileResultNegative, i++, "cannot assign a value to function argument 's'", 23, 5);
BAssertUtil.validateError(compileResultNegative, i++, "cannot assign a value to function argument 'b'", 24, 5);
BAssertUtil.validateError(compileResultNegative, i++, "cannot assign a value to function argument 'j'", 25, 5);
BAssertUtil.validateError(compileResultNegative, i++, "cannot assign a value to function argument 'a'", 38, 5);
BAssertUtil.validateError(compileResultNegative, i++, "invalid assignment: 'listener' declaration is final",
45, 5);
BAssertUtil.validateError(compileResultNegative, i++, "cannot assign a value to function argument 'p2'", 49, 5);
BAssertUtil.validateError(compileResultNegative, i++, "cannot assign a value to function argument 'b'", 53, 9);
BAssertUtil.validateError(compileResultNegative, i++, "cannot assign a value to function argument 'b'", 59, 9);
BAssertUtil.validateError(compileResultNegative, i++, "cannot assign a value to function argument 'b'", 66, 9);
Assert.assertEquals(compileResultNegative.getErrorCount(), i);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,25 @@ listener test:MockListener ml = new (8080);
public function testChangingListenerVariableAfterDefining() {
ml = new test:MockListener(8081);
}

function testRestParamFinal(string p1, string... p2) {
p2 = ["a", "b"];
}
LakshanWeerasinghe marked this conversation as resolved.
Show resolved Hide resolved

function (int a, int... b) testModuleLevelRestParamFinal = function (int i, int... b) {
b = [];
};

public function testLocalLevelRestParamFinal() {
int[] arr = [];
function (int a, int... b) func = function (int i, int... b) {
b = arr;
};
}

public function testLocalLevelRestParamFinalWithVar() {
int[] arr = [];
var func = function (int i, int... b) {
b = arr;
};
}
Loading