ballerina-lang
ballerina-lang copied to clipboard
Operating on a type narrowed variable in a `where` clause results in the runtime crashing
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
This is working as expected on 2201.3.0 rc1
Keep this open to add tests
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.