ballerina-platform / ballerina-lang

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

[Bug]: Getting NPE for a simple test with the Dead Code Elimination #43552

Closed TharmiganK closed 3 days ago

TharmiganK commented 1 week ago

Description

Getting the following error when running a simple test assertion with string includes API.

This is found while checking url module with dead code elimination(Related task - https://github.com/ballerina-platform/ballerina-library/issues/7339)

[2024-11-06 13:02:06,298] SEVERE {b7a.log.crash} - Cannot invoke "io.ballerina.runtime.internal.values.FPValue.getFunction()" because "<local1>" is null 
java.lang.NullPointerException: Cannot invoke "io.ballerina.runtime.internal.values.FPValue.getFunction()" because "<local1>" is null
        at tharmigan.package$test.0.tests.test.test(tests/test.bal:6)
        at tharmigan.package$test.0.lambdas.$_generated1tests.test_execute-generated_1.test$lambda0$(tests/test_execute-generated_1.bal:4)
        at io.ballerina.runtime.internal.scheduling.SchedulerItem.execute(SchedulerItem.java:54)
        at io.ballerina.runtime.internal.scheduling.Scheduler.run(Scheduler.java:324)
        at io.ballerina.runtime.internal.scheduling.Scheduler.runSafely(Scheduler.java:291)
        at java.base/java.lang.Thread.run(Thread.java:840)

Steps to Reproduce

Try to run the following test with dead code elimination feature:

import ballerina/test;

@test:Config
function test() {
    string result = "a";
    test:assertTrue(result.includes(" "));
}
Compiling source
        tharmigan/package:0.1.0
Duration for unused BIR node analysis : 1ms
    tharmigan/package:0.1.0 : 1ms

Running Tests

        package
[2024-11-06 13:02:06,298] SEVERE {b7a.log.crash} - Cannot invoke "io.ballerina.runtime.internal.values.FPValue.getFunction()" because "<local1>" is null 
java.lang.NullPointerException: Cannot invoke "io.ballerina.runtime.internal.values.FPValue.getFunction()" because "<local1>" is null
        at tharmigan.package$test.0.tests.test.test(tests/test.bal:6)
        at tharmigan.package$test.0.lambdas.$_generated1tests.test_execute-generated_1.test$lambda0$(tests/test_execute-generated_1.bal:4)
        at io.ballerina.runtime.internal.scheduling.SchedulerItem.execute(SchedulerItem.java:54)
        at io.ballerina.runtime.internal.scheduling.Scheduler.run(Scheduler.java:324)
        at io.ballerina.runtime.internal.scheduling.Scheduler.runSafely(Scheduler.java:291)
        at java.base/java.lang.Thread.run(Thread.java:840)

        test has failed.

                [fail] test:

                    error("java.lang.NullPointerException: Cannot invoke "io.ballerina.runtime.internal.values.FPValue.getFunction()" because "<local1>" is null")
                                callableName: test moduleName: tharmigan.package$test.0.tests.test fileName: tests/test.bal lineNumber: 6
                                callableName: test$lambda0$ moduleName: tharmigan.package$test.0.tests.test_execute-generated_1 fileName: tests/test_execute-generated_1.bal lineNumber: 4

                0 passing
                1 failing
                0 skipped

                Test execution time : 0.048s
error: there are test failures

Affected Version(s)

Ballerina SwanLake Update 11(2201.11.x)

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 week ago

Seems to be happening with langlib function calls.

Reproducible when the function is in the source too, rather than tests.

Source

public function includesFn(string a, string b) returns boolean {
    return a.includes(b);
}

Tests

import ballerina/test;

@test:Config
function test() {
    boolean res = includesFn("foo", "bar");
    test:assertFalse(res);
}

Issue could be related to io.ballerina.projects.UsedBIRNodeAnalyzer.InvocationData#registerNodes since the function pool is not populated for langlibs.

MaryamZi commented 6 days ago

Seems like the error goes away when the start index is provided.

E.g.,

return a.includes(b, 4);
MaryamZi commented 3 days ago

We've added some fixes which are available with 2201.11.0-20241111-101700-af28a18f, can you please check with this version?

TharmiganK commented 3 days ago

We've added some fixes which are available with 2201.11.0-20241111-101700-af28a18f, can you please check with this version?

With the provided timestamped version, the tests are running without any NPE issues. Hence closing this issue

github-actions[bot] commented 3 days ago

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.