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]: Invalid diagnostic when initializing readonly record with object as a field #42749

Open
xlight05 opened this issue May 14, 2024 · 1 comment
Labels
Area/CodeAnalysis Data flow , Code and Closure analysis related issues #Compiler Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Question

Comments

@xlight05
Copy link
Contributor

xlight05 commented May 14, 2024

Description

$Subject. Consider the below code. Code works fine when we remoe readonly from the type def.

Consider the following code -

Getting following diagnostics from the compiler -
cannot initialize abstract object 'null'
too many arguments in call to 'new()'

type TokenHandler readonly & record {
    http:Client meow;
};

function foo() {
    TokenHandler handler = {
        meow: check new ("http://localhost:8080")
    };
}

Steps to Reproduce

No response

Affected Version(s)

No response

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

@ballerina-bot ballerina-bot added needTriage The issue has to be inspected and labeled manually userCategory/Compilation labels May 14, 2024
@nipunayf nipunayf added Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Area/CodeAnalysis Data flow , Code and Closure analysis related issues #Compiler and removed needTriage The issue has to be inspected and labeled manually userCategory/Compilation labels May 14, 2024
@xlight05 xlight05 changed the title [Bug]: Unable to initialize readonly record with object as a field [Bug]: Invalid diagnostic when initializing readonly record with object as a field May 14, 2024
@MaryamZi
Copy link
Member

This is not a bug.

Since the field is in a readonly type, the type of the field is effectively http:Client & readonly, and when you use a readonly intersection with a non-readonly class you get an object type, not a class. So you can't use new to provide a value for such a field, because new isn't allowed with object types, only classes.

Moreover, new http:Client("http://localhost:8080") wouldn't work since it creates a mutable object, not an immutable one. You need to provide an immutable value that is also a subtype of http:Client.

Simplified sample:

class MutableObject {
    string url;

    function init(string url) returns error? {
        self.url = url;
    }
}

readonly class ImmutableObject {
    string url;

    function init(string url) returns error? {
        self.url = url;
    }
}

type TokenHandler readonly & record {
    MutableObject meow;
};

function foo() {
    TokenHandler _ = {
        meow: check new ("http://localhost:8080") // error
    };

    TokenHandler _ = {
        meow: check new ImmutableObject("http://localhost:8080") // OK
    };

    TokenHandler _ = {
        meow: object { // OK, constructed as immutable object
            string url = "http://localhost:8080";
        }
    };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/CodeAnalysis Data flow , Code and Closure analysis related issues #Compiler Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Question
Projects
None yet
Development

No branches or pull requests

4 participants