ballerina-platform / ballerina-lang

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

Incorrect type narrowing for `x is int && x == 2` #34965

Open lochana-chathura opened 2 years ago

lochana-chathura commented 2 years ago

Description: $subject. Consider the following code.

function test1(2|"foo" x) {
    if x is int && x == 2 {
        2 _ = x; // OK
    } else {
        "foo" _ = x; // Gives expression of type 'never' or equivalent to type 'never' not allowed here
    }

    if x is 2 && x == 2 {
        2 _ = x; // OK
    } else {
        "foo" _ = x; // Gives an issue expression of type 'never' or equivalent to type 'never' not allowed here
    }

    if x == 2 && x == 2 {
        2 _ = x; // OK
    } else {
        "foo" _ = x; // Gives an issue expression of type 'never' or equivalent to type 'never' not allowed here
    }
}

Affected Versions: 2201.0.0

lochana-chathura commented 2 years ago

Following also does not work.

function test1(int|string x) {
    if x is int && !(x !is int) {
        int _ = x; // Type not narrowed. Gives incompatible types: expected 'int', found 'other'
    } else {
        string _ = x; // Type not narrowed. Gives incompatible types: expected 'string', found '(string|int)'
    }
}
lochana-chathura commented 2 years ago

When fixing this issue we need to clean up the comments added in PR #34964 for the following files.

TypeGuardTest.java
test_type_guard_type_narrow_4.bal
lochana-chathura commented 1 month ago

Following also does not work.

function test1(int|string x) {
    if x is int && !(x !is int) {
        int _ = x; // Type not narrowed. Gives incompatible types: expected 'int', found 'other'
    } else {
        string _ = x; // Type not narrowed. Gives incompatible types: expected 'string', found '(string|int)'
    }
}

The first error here has been fixed in the current semtype integration changes.

lochana-chathura commented 1 month ago

Description: $subject. Consider the following code.

function test1(2|"foo" x) {
    if x is int && x == 2 {
        2 _ = x; // OK
    } else {
        "foo" _ = x; // Gives expression of type 'never' or equivalent to type 'never' not allowed here
    }

    if x is 2 && x == 2 {
        2 _ = x; // OK
    } else {
        "foo" _ = x; // Gives an issue expression of type 'never' or equivalent to type 'never' not allowed here
    }

    if x == 2 && x == 2 {
        2 _ = x; // OK
    } else {
        "foo" _ = x; // Gives an issue expression of type 'never' or equivalent to type 'never' not allowed here
    }
}

Affected Versions: 2201.0.0

These 3 errors too have been fixed in the current semtype integration changes.