Misleading label & invalid suggestion on (multi-segment) assoc const of wrong type in pattern
Open
@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 => {}
|
- rustc states that path pattern
Owner::Kis 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. - Then, it suggests turning
Owner::KintoOwner::other_kwhich is basically always incorrect & never what the user intended.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.