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]: Module-level variables not getting type-narrowed within the function scope #42028

Closed
NipunaRanasinghe opened this issue Jan 22, 2024 · 4 comments
Labels
Priority/Blocker Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug

Comments

@NipunaRanasinghe
Copy link
Contributor

Description

Refer to the below code, which is a simplified version to reproduce the issue.
This fails with the compilation error operator '+' not defined for '(string|int)' and 'string'.

string|int value = "foo";

function foo() {
    if value is string {
        value += "bar";
    }
}

Steps to Reproduce

No response

Affected Version(s)

2201.8.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

@ballerina-bot ballerina-bot added needTriage The issue has to be inspected and labeled manually userCategory/Compilation labels Jan 22, 2024
@LakshanWeerasinghe LakshanWeerasinghe added Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Priority/Blocker and removed needTriage The issue has to be inspected and labeled manually userCategory/Compilation labels Jan 22, 2024
@LakshanWeerasinghe
Copy link
Contributor

The spec currently allow type narrowing for "local variable or parameter" only. Even if make this final this won't work.

@LakshanWeerasinghe
Copy link
Contributor

In this case a work around can be suggested,

string|int value = "foo";

public function main() {
    string|int v = value;
    if v is string {
        v += "bar";
    }
    io:println(v);
    io:println(value);
}

Here we assign the module level variable to a local variable and use it for type narrowing.

@LakshanWeerasinghe
Copy link
Contributor

closing since this is a duplicate of spec issue.

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
Priority/Blocker Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug
Projects
None yet
Development

No branches or pull requests

3 participants