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 issue with global variable as default value for class field and function parameter #40774

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ public void visit(BLangSimpleVarRef.BLangLocalVarRef localVarRef) {
private void updateClosureVariable(BVarSymbol varSymbol, BLangInvokableNode encInvokable, Location pos) {
Set<Flag> flagSet = encInvokable.flagSet;
boolean isClosure = !flagSet.contains(Flag.QUERY_LAMBDA) && flagSet.contains(Flag.LAMBDA) &&
!flagSet.contains(Flag.ATTACHED);
!flagSet.contains(Flag.ATTACHED) && varSymbol.owner.tag != SymTag.PACKAGE;
if (!varSymbol.closure && isClosure) {
SymbolEnv encInvokableEnv = findEnclosingInvokableEnv(env, encInvokable);
BSymbol resolvedSymbol =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ public void testTypeRef() {
BRunUtil.invoke(compileResult, "testTypeRefInClass");
}

@Test
public void testModuleVariableReferencingClass() {
BRunUtil.invoke(compileResult, "testModuleVariableReferencingClass");
}

@Test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the correct file for this test? Maybe both can go in org.ballerinalang.test.object.ObjectTest?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to ObjectTest

public void testLocalObjectConstructorReferencingModuleVariable() {
BRunUtil.invoke(compileResult, "testLocalObjectConstructorReferencingModuleVariable");
}

@Test
public void testSimpleDistinctClass() {
BRunUtil.invoke(distinctCompUnit, "testDistinctAssignability");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ class Per {
int i = 0;
}

final int classI = 111222;

class ModuleVariableReferencingClass {
int i = classI;
}

function value(int k = classI) returns int {
return k;
}

ModuleVariableReferencingClass c1 = new;

function testModuleVariableReferencingClass() {
ModuleVariableReferencingClass c = new;
assertEquality(c.i, 111222);
chiranSachintha marked this conversation as resolved.
Show resolved Hide resolved
assertEquality(c1.i, 111222);
}

function testLocalObjectConstructorReferencingModuleVariable() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the names uniform? They are both doing the same thing, just different constructs, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as testClassReferencingModuleVariable and testLocalObjectConstructorReferencingModuleVariable

var value = object {
int i = classI;
function init() {
chiranSachintha marked this conversation as resolved.
Show resolved Hide resolved
}
};
assertEquality(value.i, 111222);
}

function assertEquality(any|error expected, any|error actual) {
if expected is anydata && actual is anydata && expected == actual {
return;
Expand Down
Loading