Skip to content

[MIR-opt] matches!(x, Some(_)) should simplify to Eq(Discriminant, Const) #131219

Description

Repro: https://rust.godbolt.org/z/4dvWdqcn5

Today, this check

pub fn my_is_eq<T>(x: &std::cmp::Ordering) -> bool { matches!(x, std::cmp::Ordering::Equal)}

simplifies as expected down to just

    bb0: {
        _2 = discriminant((*_1));
        _0 = Eq(copy _2, const 0_i8);
        return;
    }

Checking for Some in an Option, however,

pub fn my_is_some<T>(x: &Option<T>) -> bool { matches!(x, Some(_)) }

doesn't and ends up taking a whole five basic blocks

    bb0: {
        _2 = discriminant((*_1));
        switchInt(move _2) -> [1: bb2, 0: bb1, otherwise: bb4];
    }

    bb1: {
        _0 = const false;
        goto -> bb3;
    }

    bb2: {
        _0 = const true;
        goto -> bb3;
    }

    bb3: {
        return;
    }

    bb4: {
        unreachable;
    }

That's wasteful, and shouldn't happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-mir-optArea: MIR optimizationsC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions