ballerina-platform / ballerina-lang

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

[Bug]: Wrong hint and error at compile time with `distinct error` type test #38129

Open Nadeeshan96 opened 2 years ago

Nadeeshan96 commented 2 years ago

Description

$title

Steps to Reproduce

type ClientError distinct error;
type ListenerError distinct error;

type PayloadBindingError distinct ClientError | distinct ListenerError;

type DistinctClientError distinct ClientError;

type DistinctClientError1 DistinctClientError;
type DistinctClientError2 distinct ClientError;

function getGenericClientError() returns ClientError {
    return error DistinctClientError("Whoops!");
}

public function main() {
    var r = getGenericClientError();

    _ = r is DistinctClientError; // no hint, correct
    _ = r is distinct ClientError; // gives hint "unnecessary condition: expression will always evaluate to 'true'", this is wrong
    _ = r is DistinctClientError1; // no hint, correct
    _ = r is DistinctClientError2; // no hint, correct

    if (r is distinct ClientError) {
        _ = 2;
    } else {
        _ = 3; // gives wrong compilation error: unreachable code
    }

    DistinctClientError err = error DistinctClientError("err");
    _ = err is PayloadBindingError; // gives hint "unnecessary condition: expression will always evaluate to 'true'", this is wrong
}

gives

Compiling source
        iss.bal
HINT [iss.bal:(19:9,19:34)] unnecessary condition: expression will always evaluate to 'true'
HINT [iss.bal:(23:9,23:34)] unnecessary condition: expression will always evaluate to 'true'
ERROR [iss.bal:(26:9,26:15)] unreachable code
HINT [iss.bal:(30:9,30:35)] unnecessary condition: expression will always evaluate to 'true'
error: compilation contains errors

Affected Version(s)

Ballerina 2201.2.0 (Swan Lake Update 2)

OS, DB, other environment details and versions

No response

Related area

-> Compilation

Related issue(s) (optional)

https://github.com/ballerina-platform/ballerina-lang/issues/36142

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

MaryamZi commented 1 year ago

r is distinct ClientError and err is PayloadBindingError also evaluate to true at runtime at the moment (https://github.com/ballerina-platform/ballerina-lang/issues/38130).