ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.68k stars 751 forks source link

[Bug]: java null pointer exception when calling function value inside query expression #40412

Open heshanpadmasiri opened 1 year ago

heshanpadmasiri commented 1 year ago

Description

Given a list of function values, if you try to call them inside a query expression (to create a list of return values) you get a java null pointer exception.

Steps to Reproduce

import ballerina/io;
public function main() {
    ParamGenerator[] gens = [intGen, intGen];
    int[] tmp = from ParamGenerator gen in gens select gen();
    io:println(tmp);
}
function intGen() returns int {
    return 1;
}

type ParamGenerator function() returns int;

Trying to compile the above code results in

[2023-05-13 08:49:51,248] SEVERE {b7a.log.crash} - null 
java.lang.NullPointerException
        at org.wso2.ballerinalang.compiler.bir.optimizer.BIROptimizer$RHSTempVarOptimizer.visit(BIROptimizer.java:159)
        at org.wso2.ballerinalang.compiler.bir.model.BIRNode$BIRBasicBlock.accept(BIRNode.java:474)
        at org.wso2.ballerinalang.compiler.bir.optimizer.BIROptimizer$RHSTempVarOptimizer.lambda$visit$3(BIROptimizer.java:121)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
        at org.wso2.ballerinalang.compiler.bir.optimizer.BIROptimizer$RHSTempVarOptimizer.visit(BIROptimizer.java:121)
        at org.wso2.ballerinalang.compiler.bir.model.BIRNode$BIRFunction.accept(BIRNode.java:432)
        at org.wso2.ballerinalang.compiler.bir.optimizer.BIROptimizer$RHSTempVarOptimizer.lambda$visit$1(BIROptimizer.java:107)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
        at org.wso2.ballerinalang.compiler.bir.optimizer.BIROptimizer$RHSTempVarOptimizer.visit(BIROptimizer.java:107)
        at org.wso2.ballerinalang.compiler.bir.model.BIRNode$BIRPackage.accept(BIRNode.java:89)
        at org.wso2.ballerinalang.compiler.bir.optimizer.BIROptimizer.optimizePackage(BIROptimizer.java:83)
        at org.wso2.ballerinalang.compiler.bir.BIRGen.genBIR(BIRGen.java:287)
        at io.ballerina.projects.internal.CompilerPhaseRunner.birGen(CompilerPhaseRunner.java:220)
        at io.ballerina.projects.internal.CompilerPhaseRunner.performBirGenPhases(CompilerPhaseRunner.java:157)
        at io.ballerina.projects.ModuleContext.generateCodeInternal(ModuleContext.java:451)
        at io.ballerina.projects.ModuleCompilationState$4.generatePlatformSpecificCode(ModuleCompilationState.java:132)
        at io.ballerina.projects.ModuleContext.generatePlatformSpecificCode(ModuleContext.java:385)
        at io.ballerina.projects.JBallerinaBackend.performCodeGen(JBallerinaBackend.java:170)
        at io.ballerina.projects.JBallerinaBackend.<init>(JBallerinaBackend.java:139)
        at io.ballerina.projects.JBallerinaBackend.lambda$from$0(JBallerinaBackend.java:117)
        at java.base/java.util.HashMap.computeIfAbsent(HashMap.java:1134)
        at io.ballerina.projects.PackageCompilation.getCompilerBackend(PackageCompilation.java:171)
        at io.ballerina.projects.JBallerinaBackend.from(JBallerinaBackend.java:116)
        at io.ballerina.cli.task.CompileTask.execute(CompileTask.java:191)
        at io.ballerina.cli.TaskExecutor.executeTasks(TaskExecutor.java:40)
        at io.ballerina.cli.cmd.RunCommand.execute(RunCommand.java:221)
        at java.base/java.util.Optional.ifPresent(Optional.java:183)
        at io.ballerina.cli.launcher.Main.main(Main.java:51)

ERROR [.:(1:1,1:1)] Compilation failed due to an unhandled exception
error: compilation contains errors

Affected Version(s)

2201.5.0

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 commented 1 year ago

Same with

type Book record {
    readonly int id;
};

type BookUpdate record {

};

function update(function (Book) returns BookUpdate? fn) returns error? {
    stream<Book, error?> bookStr = new;
    [int, BookUpdate][] ids = check from Book book in bookStr
                                                let BookUpdate? update = fn(book)
                                                where update !is ()
                                                select [book.id, update];

    foreach [int, BookUpdate] [id, bookUpdate] in ids {

    }
}
MaryamZi commented 2 months ago

Seems to have been fixed now.