Skip to content

Optimize const pattern guards for StructuralEq types #138664

Open
@TimNN

Description

@TimNN
Contributor

With inline_const_pat being removed in #138492, it might be nice to optimize codegen of pat if pat == const { .. }.

Right now

match a {
  1 => 42,
  b if b == 7 => 100,
  _ => 99,
}

Produces a SwitchInt and then a separate equality check.

Changing the second pattern to 7 => 100 produces a single SwitchInt.

I assume that the optimizer can recover this (at least for simpler cases), but it might be nice for the compiler to generate the simplified MIR directly (especially with inline_const_pat gone).

Activity

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Mar 18, 2025
added
C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing such
on Mar 18, 2025
added
A-patternsRelating to patterns and pattern matching
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Mar 22, 2025
removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-patternsRelating to patterns and pattern matchingC-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

        Participants

        @TimNN@fmease@lolbinarycat@jieyouxu@rustbot

        Issue actions

          Optimize `const` pattern guards for `StructuralEq` types · Issue #138664 · rust-lang/rust