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

Conflicting error messages using check inside a type guard #28769

Closed
hasithaa opened this issue Feb 19, 2021 · 1 comment · Fixed by #29282
Closed

Conflicting error messages using check inside a type guard #28769

hasithaa opened this issue Feb 19, 2021 · 1 comment · Fixed by #29282
Assignees
Labels
Area/TypeChecker Type Checker related issues #Compiler Lang/TypeNarrowing Issue related to type narrowing and Inferring Planning/SwanLakeDump All issues planned for Swan Lake GA release Priority/Blocker Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug

Comments

@hasithaa
Copy link
Contributor

Observed with SL-Alpha2 packs.

import ballerina/file;

function test1() returns error? {
    var result = file:remove("somePath");
    if !(result is file:FileNotFoundError) && !(result is ()) {
        check result; 
        // Error 1: variable assignment is required(BCE2526)
        // Error 2: invalid usage of the 'check' expression operator: all expression types are equivalent to error type
    }
}

function test2() returns error? {
    var result = file:remove("somePath");
    if !(result is file:FileNotFoundError) && !(result is ()) {
        var x = check result; // Error: invalid usage of the 'check' expression operator: all expression types are equivalent to error type(BCE3031)
    }

}
@hasithaa hasithaa added Priority/Blocker Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Area/TypeChecker Type Checker related issues #Compiler CompilerSLDump Lang/TypeNarrowing Issue related to type narrowing and Inferring labels Feb 19, 2021
@chiranSachintha chiranSachintha self-assigned this Mar 3, 2021
@MaryamZi
Copy link
Member

MaryamZi commented Mar 5, 2021

The spec currently does not allow using check result as used here even though the implementation allows it.

checking-keyword is allowed in statements only if it is part of a call-stmt.

call-stmt := call-expr ;
call-expr :=
   function-call-expr
   | method-call-expr
   | checking-keyword call-expr

Related spec discussion - ballerina-platform/ballerina-spec#588

As discussed, we may have to fix this deviation first.

However, even when the checking-keyword is used with a function/method call, and the function/method only returns error(s), this compilation error can be observed.

function err() returns error => error("Error!");

public function main() returns error? {
     check err();

     // $ bal run foo.bal 

     // Compiling source
     //      foo.bal
     // ERROR [foo.bal:(4:6,4:18)] variable assignment is required
     // ERROR [foo.bal:(4:12,4:17)] invalid usage of the 'check' expression operator: all expression types are equivalent to error type
}

But the spec does not seem to have such a restriction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/TypeChecker Type Checker related issues #Compiler Lang/TypeNarrowing Issue related to type narrowing and Inferring Planning/SwanLakeDump All issues planned for Swan Lake GA release Priority/Blocker Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants