ballerina-platform / ballerina-spec

Ballerina Language and Platform Specifications
Other
167 stars 54 forks source link

Clarification on unreachability analysis following a query action #1142

Open SasinduDilshara opened 2 years ago

SasinduDilshara commented 2 years ago

Description

function returnNil() {
}

function bar() returns string|error {
    check from int _ in [1, 3, 5]
    do {        
        returnNil();
        return "inside the Do clause"; 
    };
    return "Outside the do clause"; // No `unreachable code` error
}

public function main() returns error? {
    io:println(check bar()); // inside the Do clause
}

Should the statements in a query action do clause affect the (un)reachability of the statements that comes after the query action?.

Spec says It is a compile error if a statement other than a panic-stmt is not reachable. According to that this should be give a compile error for unrechability of the code.

jclark commented 2 years ago

The second return is only unreachable if the do clause is guaranteed to be executed at least once. To determine that, you would need to look at whether the iterable to which from is applied is guaranteed non-empty. But that it is not considered by reachability analysis. The only thing it considers is conditions. https://ballerina.io/spec/lang/master/#section_7.4.3 ought to be explicit about this.

Note that from/do can give rise to unreachable code.

from int _ in arr
where false
do {
  foo(); // unreachable
}