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]: No error when assinging a mutable value to a union where the relevent member type is immutable #41383

Closed
rdulmina opened this issue Sep 14, 2023 · 2 comments
Assignees
Labels
Area/Compiler Fix/BreakingChange Fix containing a backward incompatible change. Priority/High Reason/Invalid Issue is invalid. Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug

Comments

@rdulmina
Copy link
Contributor

rdulmina commented Sep 14, 2023

Description

Consider the below sample

type Foo record {
    int i;
};

type Bar record {
    int i;
    int j;
};


public function main() {
    Bar b = {i: 1, j: 2};
    Bar & readonly|Foo a = b; // no error
    Bar & readonly|string a = b; // error as expected
    Bar & readonly a = b; // error as expected
}

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

@rdulmina rdulmina added Type/Bug Priority/High Area/Compiler Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Fix/BreakingChange Fix containing a backward incompatible change. labels Sep 14, 2023
@LakshanWeerasinghe LakshanWeerasinghe self-assigned this Nov 21, 2023
@LakshanWeerasinghe
Copy link
Contributor

The Foo type is assignable to the Bar type, making the Bar & readonly|Foo union type also assignable to the Bar type.
This is due to the fact that Foo is an open record, with its fields constituting a subset of the fields present in the Bar type.

Bar b = {i: 1, j: 2};
Foo foo = b;
Bar & readonly|Foo a = b;

If we add a new field to the Foo type that is not present in the Bar type, it will result in an error.

type Foo record {
    int i;
    int z;
};

type Bar record {
    int i;
    int j;
};

public function main() {
    Bar b = {i: 1, j: 2};
    Bar & readonly|Foo a = b; // error as expected
}

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.

@LakshanWeerasinghe LakshanWeerasinghe added the Reason/Invalid Issue is invalid. label Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/Compiler Fix/BreakingChange Fix containing a backward incompatible change. Priority/High Reason/Invalid Issue is invalid. 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

2 participants