rust-lang / rust-lang/rust

Binding both borrowed and unborrowed field becomes impossible in the 2024 edition

Open
#149,651 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-edition-2024 C-bug I-edition-triaged T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The following code compiles on the 2021 edition:

enum MyCow<'a, T> {
    Owned(T),
    Borrowed(&'a T)
}

enum Inner<T> {
    Foo(T)
}

fn my_match<T>(val: MyCow<'_, Inner<T>>) {
    match val {
        MyCow::Owned(Inner::Foo(ref inner)) | MyCow::Borrowed(Inner::Foo(ref inner)) => {}
    }
}

Here, the inner pattern has type &T, allowing a single match arm to handle both the MyCow::Owned and MyCow::Borrowed cases.

In the 2024 edition, this produces an error:

error: binding modifiers may only be written when the default binding mode is `move`
  --> src/lib.rs:12:74
   |
12 |         MyCow::Owned(Inner::Foo(ref inner)) | MyCow::Borrowed(Inner::Foo(ref inner)) => {}
   |                                                                          ^^^ binding modifier not allowed under `ref` default binding mode
   |
   = note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/match-ergonomics.html>
note: matching on a reference type with a non-reference pattern changes the default binding mode
  --> src/lib.rs:12:63
   |
12 |         MyCow::Owned(Inner::Foo(ref inner)) | MyCow::Borrowed(Inner::Foo(ref inner)) => {}
   |                                                               ^^^^^^^^^^^^^^^^^^^^^ this matches on type `&_`
help: remove the unnecessary binding modifier
   |
12 -         MyCow::Owned(Inner::Foo(ref inner)) | MyCow::Borrowed(Inner::Foo(ref inner)) => {}
12 +         MyCow::Owned(Inner::Foo(ref inner)) | MyCow::Borrowed(Inner::Foo(inner)) => {}
   |

Unfortunately, the suggestion does not work:

error[E0409]: variable `inner` is bound inconsistently across alternatives separated by `|`
  --> src/lib.rs:12:74
   |
12 |         MyCow::Owned(Inner::Foo(ref inner)) | MyCow::Borrowed(Inner::Foo(inner)) => {}
   |                                     ----- first binding                  ^^^^^ bound in different ways

No combination of inner/ref inner compiles under the 2024 edition. This forces the original code to either be split into separate match arms, or factored out into a closure/method.

Meta

rustc --version --verbose:

rustc 1.91.0 (f8297e351 2025-10-28)
binary: rustc
commit-hash: f8297e351a40c1439a467bbbb6879088047f50b3
commit-date: 2025-10-28
host: x86_64-unknown-linux-gnu
release: 1.91.0
LLVM version: 21.1.2

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 with the src/lib.rs reproducer and compare its 2021 and 2024 results, including the suggested diagnostic change. Read the Rust 2024 match-ergonomics edition guide, then trace the compiler's pattern-binding handling for or-patterns. Done means the valid shared binding behavior is restored or the diagnostic clearly documents the required workaround.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.