rust-lang / rust-lang/rust

Optimize `const` pattern guards for `StructuralEq` types

Open
#138,664 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-patterns C-optimization T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the issue's guarded-pattern example and inspect how the compiler lowers pat if pat == const into MIR, especially the separate equality check and SwitchInt. Compare that lowering with the direct 7 => 100 pattern and determine what would make the generated MIR equivalent; done means simpler cases produce a single SwitchInt without the extra equality check.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.