ballerina-lang icon indicating copy to clipboard operation
ballerina-lang copied to clipboard

Operating on a type narrowed variable in a `where` clause results in the runtime crashing

Open MaryamZi opened this issue 3 years ago • 3 comments

Description: $title. Works if the logic is extracted out to a function.

Steps to reproduce:

import ballerina/io;

type Employee record {|
    string name;
    decimal salary;
|};

function filterEmployees(Employee[] employees, decimal? target) returns Employee[] {
    return from Employee employee in employees
        where target == () || target < employee.salary // crashes
        // where salaryMatches(employee, target) // OK
        select employee;
}

function salaryMatches(Employee employee, decimal? target) returns boolean => target == () || target < employee?.salary;

public function main() {
    io:println(filterEmployees([
        {name: "Jo", salary: 20.0},
        {name: "May", salary: 12.2},
        {name: "Sam", salary: 10.0},
        {salary: 10.0, name: "Rob"}
    ], 10.0));
}
error: {ballerina}TypeCastError {"message":"incompatible types: '()' cannot be cast to 'decimal'"}
        at ballerina.lang.query.0._FilterFunction:process(types.bal:505)
           ballerina.lang.query.0._SelectFunction:process(types.bal:588)
           ballerina.lang.query.0:$anon$method$delegate$_StreamFunction.process$0(types.bal:91)

Affected Versions: 2201.1.0-rc1

MaryamZi avatar May 11 '22 02:05 MaryamZi

This is working as expected on 2201.3.0 rc1

pcnfernando avatar Nov 03 '22 07:11 pcnfernando

Keep this open to add tests

pcnfernando avatar Nov 03 '22 07:11 pcnfernando

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.

github-actions[bot] avatar Nov 03 '22 09:11 github-actions[bot]