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

[Bug]: NPE when a variable from a structured binding pattern is used in an anonymous function expression #41236

Closed
MaryamZi opened this issue Aug 18, 2023 · 2 comments · Fixed by #41894
Assignees
Labels
Reason/EngineeringMistake The issue occurred due to a mistake made in the past. Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug
Milestone

Comments

@MaryamZi
Copy link
Member

Description

$title.

Steps to Reproduce

type Doctor record {
    string name;
    string category;    
};

function addDoctor(Doctor doctor, string[] categories) returns error? {
    var {category} = doctor; // also getting an unused variable warning
    if !categories.some(
        existingCategory => existingCategory == category) {
        
    }
}

or

type Doctor record {
    string name;
    string category;    
};

function addDoctor(Doctor doctor, string[] categories) returns error? {
    var {category} = doctor; // also getting an unused variable warning
    if !some(categories, existingCategory => existingCategory == category) {
        
    }
}

function some(string[] arr, function (string s) returns boolean func) returns boolean {
    foreach string item in arr {
        if func(item) {
            return true;
        }
    }
    return false;
}
[2023-08-18 09:22:48,015] SEVERE {b7a.log.crash} - null 
java.lang.NullPointerException
        at org.wso2.ballerinalang.compiler.bir.codegen.JvmInstructionGen.getJVMIndexOfVarRef(JvmInstructionGen.java:1323)
        at org.wso2.ballerinalang.compiler.bir.codegen.JvmInstructionGen.loadVar(JvmInstructionGen.java:2141)
        at org.wso2.ballerinalang.compiler.bir.codegen.JvmInstructionGen.generateFPLoadIns(JvmInstructionGen.java:1760)
        at org.wso2.ballerinalang.compiler.bir.codegen.JvmInstructionGen.generateInstructions(JvmInstructionGen.java:2336)
        at org.wso2.ballerinalang.compiler.bir.codegen.JvmCodeGenUtil.getLastScopeFromBBInsGen(JvmCodeGenUtil.java:591)
        at org.wso2.ballerinalang.compiler.bir.codegen.methodgen.MethodGen.generateBasicBlocks(MethodGen.java:558)
        at org.wso2.ballerinalang.compiler.bir.codegen.methodgen.MethodGen.genJMethodForBFunc(MethodGen.java:274)
        at org.wso2.ballerinalang.compiler.bir.codegen.methodgen.MethodGen.generateMethod(MethodGen.java:175)
        at org.wso2.ballerinalang.compiler.bir.codegen.JvmPackageGen.lambda$generateModuleClasses$0(JvmPackageGen.java:433)
        at java.base/java.util.HashMap.forEach(HashMap.java:1337)
        at org.wso2.ballerinalang.compiler.bir.codegen.JvmPackageGen.generateModuleClasses(JvmPackageGen.java:388)
        at org.wso2.ballerinalang.compiler.bir.codegen.JvmPackageGen.generate(JvmPackageGen.java:800)
        at org.wso2.ballerinalang.compiler.bir.codegen.CodeGenerator.generate(CodeGenerator.java:101)
        at org.wso2.ballerinalang.compiler.bir.codegen.CodeGenerator.generate(CodeGenerator.java:72)
        at io.ballerina.projects.JBallerinaBackend.performCodeGen(JBallerinaBackend.java:321)
        at io.ballerina.projects.ModuleContext.generateCodeInternal(ModuleContext.java:480)
        at io.ballerina.projects.ModuleCompilationState$4.generatePlatformSpecificCode(ModuleCompilationState.java:132)
        at io.ballerina.projects.ModuleContext.generatePlatformSpecificCode(ModuleContext.java:387)
        at io.ballerina.projects.JBallerinaBackend.performCodeGen(JBallerinaBackend.java:173)
        at io.ballerina.projects.JBallerinaBackend.<init>(JBallerinaBackend.java:142)
        at io.ballerina.projects.JBallerinaBackend.lambda$from$0(JBallerinaBackend.java:120)
        at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1134)
        at io.ballerina.projects.PackageCompilation.getCompilerBackend(PackageCompilation.java:172)
        at io.ballerina.projects.JBallerinaBackend.from(JBallerinaBackend.java:119)
        at io.ballerina.cli.task.CompileTask.execute(CompileTask.java:201)
        at io.ballerina.cli.TaskExecutor.executeTasks(TaskExecutor.java:40)
        at io.ballerina.cli.cmd.BuildCommand.execute(BuildCommand.java:288)
        at java.base/java.util.Optional.ifPresent(Optional.java:183)
        at io.ballerina.cli.launcher.Main.main(Main.java:56)

Affected Version(s)

2201.7.1

OS, DB, other environment details and versions

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@MaryamZi MaryamZi added Type/Bug Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. labels Aug 18, 2023
@poorna2152 poorna2152 self-assigned this Dec 7, 2023
@chiranSachintha
Copy link
Member

type Doctor record {
    string name;
    string category;
};

function addDoctor(Doctor doctor, string[] categories) {
    var {category} = doctor;
    var addCategory = function() {
        _ = category;
    };
}

When considering the above example, we desugared it as follows:

type Doctor record {
    string name;
    string category;
};

function addDoctor(Doctor doctor, string[] categories) {
    {
        map<(any|error)> recordVar_1 = <map<(any|error)>> doctor;
        string category = <string> recordVar_1["category"];
    }
    
    var addCategory = function() {
        _ = category;
    };
}

Since we added category variable to a block scope, we can't access category within the function scope. In the above example, after desugaring, we use category in the lambda scope, but there is no way to access category because it is defined in the block scope. So IMO, we should address this issue in the desugaring phase.

Copy link

This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now.

      - Reason/EngineeringMistake - The issue occurred due to a mistake made in the past.
      - Reason/Regression - The issue has introduced a regression.
      - Reason/MultipleComponentInteraction - Issue occured due to interactions in multiple components.
      - Reason/Complex - Issue occurred due to complex scenario.
      - Reason/Invalid - Issue is invalid.
      - Reason/Other - None of the above cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Reason/EngineeringMistake The issue occurred due to a mistake made in the past. Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants