Skip to content

[Bug]: var _ wildcard binding pattern in match statement never matches when match expression type contains error #44648

Description

@senuriwick

Description

When a match statement's subject expression has a type that includes error (e.g. JSON field access j.kind which produces json|error), the var _ wildcard binding pattern never matches at runtime even when it should.

Steps to Reproduce

  1. Create a Ballerina file with the following code:
import ballerina/io;

function getKind(json j) returns string {
    match j.kind {
        "a" => {
            return "A";
        }
        error(var _) => {
            return "E";
        }
        var _ => {
            return "B";
        }
    }
}

public function main() {
    json myData = {kind: "b"};
    string result = getKind(myData);
    io:println("Returned value: ", result);
}
  1. Observe the output:
Returned value:

Expected: Returned value: B
The var _ clause should match "b" since it is a non-error json value.

Affected Version(s)

No response

OS, DB, other environment details and versions

No response

Related area

-> Compilation

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions