rust-lang / rust-lang/rust

Misleading label & invalid suggestion on (multi-segment) assoc const of wrong type in pattern

Open
#153,926 5 comments 0 reactions 1 assignee View on GitHub

@ChronoCoders is already working on this.

Since Jun 17, 2026.

A-associated-items A-diagnostics A-patterns D-incorrect D-invalid-suggestion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

Given

struct Owner;
impl Owner { const K: &str = ""; }

fn scope(source: std::borrow::Cow<'_, str>) {
    match source {
        Owner::K => {}
        _ => {}
    }
}

rustc emits the following:

error[E0308]: mismatched types
 --> src/lib.rs:6:9
  |
2 | impl Owner { const K: &str = ""; }
  |              ------------- associated constant defined here
...
5 |     match source {
  |           ------ this expression has type `Cow<'_, str>`
6 |         Owner::K => {}
  |         ^^^^^^^^
  |         |
  |         expected `Cow<'_, str>`, found `&str`
  |         `K` is interpreted as an associated constant, not a new binding
  |
  = note:   expected enum `Cow<'_, str>`
          found reference `&'static str`
help: try wrapping the pattern in `std::borrow::Cow::Borrowed`
  |
6 |         std::borrow::Cow::Borrowed(Owner::K) => {}
  |         +++++++++++++++++++++++++++        +
help: introduce a new binding instead
  |
6 -         Owner::K => {}
6 +         Owner::other_k => {}
  |
  1. rustc states that path pattern Owner::K is not interpreted as a binding pattern which is correct but nobody assumed that it was, a multi-segment path is never a pattern binding. Mentioning it is irrelevant, misleading & confusing.
  2. Then, it suggests turning Owner::K into Owner::other_k which is basically always incorrect & never what the user intended.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.