ballerina-platform / ballerina-lang

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

[Bug]: NullPointerException: Cannot read field "tag" because "referredType" is null #41474

Open matthesinator opened 11 months ago

matthesinator commented 11 months ago

Description

I'm getting the following error while compiling when trying out a very basic program. I get the same error when trying the ballerinax/newsapi package.

[2023-10-05 15:56:52,414] SEVERE {b7a.log.crash} - Cannot read field "tag" because "referredType" is null 
java.lang.NullPointerException: Cannot read field "tag" because "referredType" is null
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:2232)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.accept(BLangRecordLiteral.java:81)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeExpr(CodeAnalyzer.java:3514)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeExprs(CodeAnalyzer.java:3585)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:2706)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit.accept(BLangTypeInit.java:70)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeExpr(CodeAnalyzer.java:3514)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:3196)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.expressions.BLangCheckedExpr.accept(BLangCheckedExpr.java:70)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeExpr(CodeAnalyzer.java:3514)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:1717)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.BLangSimpleVariable.accept(BLangSimpleVariable.java:59)
        at org.wso2.ballerinalang.compiler.tree.SimpleBLangNodeAnalyzer.visitNode(SimpleBLangNodeAnalyzer.java:215)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeNode(CodeAnalyzer.java:349)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyzeTopLevelNodes(CodeAnalyzer.java:338)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:326)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.visit(CodeAnalyzer.java:280)
        at org.wso2.ballerinalang.compiler.tree.BLangPackage.accept(BLangPackage.java:172)
        at org.wso2.ballerinalang.compiler.tree.SimpleBLangNodeAnalyzer.visitNode(SimpleBLangNodeAnalyzer.java:215)
        at org.wso2.ballerinalang.compiler.semantics.analyzer.CodeAnalyzer.analyze(CodeAnalyzer.java:314)
        at io.ballerina.projects.internal.CompilerPhaseRunner.codeAnalyze(CompilerPhaseRunner.java:196)
        at io.ballerina.projects.internal.CompilerPhaseRunner.performTypeCheckPhases(CompilerPhaseRunner.java:119)
        at io.ballerina.projects.ModuleContext.compileInternal(ModuleContext.java:435)
        at io.ballerina.projects.ModuleCompilationState$1.compile(ModuleCompilationState.java:45)
        at io.ballerina.projects.ModuleContext.compile(ModuleContext.java:383)
        at io.ballerina.projects.PackageCompilation.compileModulesInternal(PackageCompilation.java:208)
        at io.ballerina.projects.PackageCompilation.compileModules(PackageCompilation.java:192)
        at io.ballerina.projects.PackageCompilation.compile(PackageCompilation.java:99)
        at io.ballerina.projects.PackageCompilation.from(PackageCompilation.java:94)
        at io.ballerina.projects.PackageContext.getPackageCompilation(PackageContext.java:242)
        at io.ballerina.projects.Package.getCompilation(Package.java:150)
        at io.ballerina.projects.Package.runCodeGeneratorPlugins(Package.java:319)
        at io.ballerina.cli.task.CompileTask.execute(CompileTask.java:142)
        at io.ballerina.cli.TaskExecutor.executeTasks(TaskExecutor.java:40)
        at io.ballerina.cli.cmd.RunCommand.execute(RunCommand.java:223)
        at java.base/java.util.Optional.ifPresent(Optional.java:178)
        at io.ballerina.cli.launcher.Main.main(Main.java:58)

ERROR [.:(1:1,1:1)] Compilation failed due to: Cannot read field "tag" because "referredType" is null
error: compilation contains errors

Steps to Reproduce

Compile the following:

import ballerina/io;
import ballerinax/openweathermap;

openweathermap:ApiKeysConfig config = {
    appid: "--- api key here ---"
};

openweathermap:Client myclient = check new Client(config, {});

public function main() {
    // openweathermap:CurrentWeatherData result = check openweatherClient->getCurretWeatherData("Colombo");;
    io:println("Weather: ");
}

When removing the curly braces from check new Client(config, {});, I get a different error.

Affected Version(s)

Ballerina 2201.8.0 (Swan Lake Update 8) Language specification 2023R1 Update Tool 1.4.0

OS, DB, other environment details and versions

Manjaro Linux

javac 20.0.2 openjdk 20.0.2 2023-07-18 OpenJDK Runtime Environment (build 20.0.2+9) OpenJDK 64-Bit Server VM (build 20.0.2+9, mixed mode, sharing)

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

MaryamZi commented 10 months ago

While the crash is due to a bug, the issue here seems to be due to the unknown type on the RHS of

openweathermap:Client myclient = check new Client(config, {});

This looks for a Client type in the current module.

Changing this to use openweathermap:Client

openweathermap:Client myclient = check new openweathermap:Client(config, {});

or omitting Client (which will result in the type being inferred from the expected type)

openweathermap:Client myclient = check new (config, {});

will result in successful compilation.

matthesinator commented 10 months ago

Yes that worked, thank you! Not sure if I should close this or leave it open?

MaryamZi commented 10 months ago

Not sure if I should close this or leave it open?

Let's keep this open since the crash has to be fixed and the sample should result in a compilation error instead (e.g., unknown type 'Client').

SasinduDilshara commented 8 months ago

Reason - https://github.com/ballerina-platform/ballerina-lang/issues/41788#issuecomment-1888477708