Skip to content

Commit

Permalink
Merge pull request #40774 from chiranSachintha/issue-40769
Browse files Browse the repository at this point in the history
Fix issue with global variable as default value for class field and function parameter
  • Loading branch information
chiranSachintha authored Aug 15, 2023
2 parents b59f6f1 + b58a604 commit 8c04b7b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,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 @@ -47,11 +47,13 @@ public class ObjectTest {

private CompileResult checkInInitializerResult;
private CompileResult checkFunctionReferencesResult;
private CompileResult checkObjectWithDefaultValuesResult;

@BeforeClass
public void setUp() {
checkInInitializerResult = BCompileUtil.compile("test-src/object/object_field_initializer_with_check.bal");
checkFunctionReferencesResult = BCompileUtil.compile("test-src/object/object_function_pointer.bal");
checkObjectWithDefaultValuesResult = BCompileUtil.compile("test-src/object/object-with-defaultable-field.bal");
}

@Test(description = "Test Basic object as struct")
Expand Down Expand Up @@ -127,8 +129,7 @@ public void testObjectWithSimpleInit() {

@Test(description = "Test object with defaultable field in init function")
public void testObjectWithDefaultableField() {
CompileResult compileResult = BCompileUtil.compile("test-src/object/object-with-defaultable-field.bal");
BArray returns = (BArray) BRunUtil.invoke(compileResult, "testObjectWithSimpleInit");
BArray returns = (BArray) BRunUtil.invoke(checkObjectWithDefaultValuesResult, "testObjectWithSimpleInit");

Assert.assertEquals(returns.size(), 4);
Assert.assertSame(returns.get(0).getClass(), Long.class);
Expand Down Expand Up @@ -941,6 +942,16 @@ public void testNonPublicSymbolsWarningInServiceClass() {
Assert.assertEquals(result.getDiagnostics().length, 0);
}

@Test
public void testClassWithModuleLevelVarAsDefaultValue() {
BRunUtil.invoke(checkObjectWithDefaultValuesResult, "testClassWithModuleLevelVarAsDefaultValue");
}

@Test
public void testObjectConstructorWithModuleLevelVarAsDefaultValue() {
BRunUtil.invoke(checkObjectWithDefaultValuesResult, "testObjectConstructorWithModuleLevelVarAsDefaultValue");
}

@AfterClass
public void tearDown() {
checkFunctionReferencesResult = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,39 @@ class Person {
self.month = val1;
}
}

final int classI = 111222;

class ModuleVariableReferencingClass {
int i = classI;
}

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

ModuleVariableReferencingClass c1 = new;

function testClassWithModuleLevelVarAsDefaultValue() {
ModuleVariableReferencingClass c = new;
assertEquality(111222, c.i);
assertEquality(111222, c1.i);
}

function testObjectConstructorWithModuleLevelVarAsDefaultValue() {
var value = object {
int i = classI;
};
assertEquality(111222, value.i);
}

const ASSERTION_ERROR_REASON = "AssertionError";

function assertEquality(anydata expected, anydata actual) {
if expected == actual {
return;
}

panic error(ASSERTION_ERROR_REASON,
message = "expected '" + expected.toString() + "', found '" + actual.toString () + "'");
}

0 comments on commit 8c04b7b

Please sign in to comment.