Skip to content

Suboptimal code for non-linear is-pattern expressions compared to switch #59615

@alrz

Description

@alrz
class C {
    string Worst(object i) {
        // uses a boolean flag for the result and a conditional jump
        return i is 1 or C(2) ? "aaa" : "bbb";
    }
    string Switch(object i) {
        // falls back to switch arms when possible; this info isn't available for `is` expressions
        return i switch { 1 or C(2) => "aaa", _ => "bbb" };
    }
    string Best(object i) {
        // ideally it should be lowered as `||` - still "linear" without using a flag
        return i is 1 || i is C(2) ? "aaa" : "bbb";
    }
    void Deconstruct(out int i) => throw null;
}

Relates to #45679

(sharplab.io)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-CompilersCode Gen QualityRoom for improvement in the quality of the compiler's generated code

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions