-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clarify the 'use a constant in a pattern' error message #108548
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @petrochenkov (or someone else) soon. Please see the contribution instructions for more information. |
I don't think that this error message should mention StructuralEq, but regardless, it should be done as a separate "note:" section on the diagnostic, and not in the primary message of the error. |
Ok I attempted that |
CI is failing. |
fixed an accidental edit. tests unaffected |
@rustbot ready |
Why not? To me the message explains the what but not the why. Seems like a helpful piece of information. |
My only concern is I'm not sure if it's conventional to document never-stable, compiler implementation details in user-facing (non-nightly-feature-related) error messages... Maybe someone else from @rust-lang/wg-diagnostics can help make a call here. This also could really use a test demonstrating the note. otherwise r=me on the implementation though. |
I think we could mention |
☔ The latest upstream changes (presumably #108682) made this pull request unmergeable. Please resolve the merge conflicts. |
This has a conflict Would appreciate an additional note (maybe above the one you added) that mentions that manual impls of |
@@ -315,7 +315,7 @@ mir_build_moved = value is moved into `{$name}` here | |||
mir_build_union_pattern = cannot use unions in constant patterns | |||
|
|||
mir_build_type_not_structural = | |||
to use a constant of type `{$non_sm_ty}` in a pattern, `{$non_sm_ty}` must be annotated with `#[derive(PartialEq, Eq)]` | |||
to use a constant of type `{$non_sm_ty}` in a pattern, `{$non_sm_ty}` must be annotated with `#[derive(PartialEq, Eq)]`. the traits must be derived, manual `impl`s are not sufficient |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please make this into a note, not the primary message. same below.
also, why did you take away the link to Structural{Partial}Eq
? That was fine, as long as it was paired with an additional note that explains what those traits means in practice.
Can we also make sure that there's a test exercising this message? |
I see tests in |
You need to give the second note another name and declare it in the diagnostic struct -- the first is being overwritten. Fluent file slug names are not deduplicated. |
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
@@ -6,6 +6,8 @@ LL | FOO_REF_REF => {}, | |||
| | |||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | |||
= note: for more information, see issue #62411 <https://github.com/rust-lang/rust/issues/62411> | |||
= note: the traits must be derived, manual `impl`s are not sufficient | |||
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details | |||
= note: `-D indirect-structural-match` implied by `-D warnings` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unsure why this appears in the middle of these other notes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it really matters.
@bors r+ rollup |
…iaskrgr Rollup of 9 pull requests Successful merges: - rust-lang#108548 (Clarify the 'use a constant in a pattern' error message) - rust-lang#109565 (Improve documentation for E0223) - rust-lang#109661 (Fix LVI test post LLVM 16 update) - rust-lang#109667 (Always set `RUSTC_BOOTSTRAP` with `x doc`) - rust-lang#109669 (Update books) - rust-lang#109678 (Don't shadow the `dep_node` var in `incremental_verify_ich_failed`) - rust-lang#109682 (Add `#[inline]` to CStr trait implementations) - rust-lang#109685 (Make doc comment a little bit more accurate) - rust-lang#109687 (Document the heuristics IsTerminal uses on Windows) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Clarify the 'use a constant in a pattern' error message ```rs use std::borrow::Cow; const ERROR_CODE: Cow<'_, str> = Cow::Borrowed("23505"); fn main() { let x = Cow::from("23505"); match x { ERROR_CODE => {} } } ``` ``` error: to use a constant of type `Cow` in a pattern, `Cow` must be annotated with `#[derive(PartialEq, Eq)]` --> src/main.rs:9:9 | 9 | ERROR_CODE => {} | ^^^^^^^^^^ error: could not compile `playground` due to previous error ``` It seems helpful to link to StructuralEq in this message. I was a little confused, because `Cow<'_, str>` implements PartialEq and Eq, but they're not derived, which I learned is necessary for structural equality and using constants in patterns (thanks to the Rust community Discord server) For tests, should I update every occurrence of this message? I see tests where this is still a warning and I'm not sure if I should update those.
It seems helpful to link to StructuralEq in this message. I was a little confused, because
Cow<'_, str>
implements PartialEq and Eq, but they're not derived, which I learned is necessary for structural equality and using constants in patterns (thanks to the Rust community Discord server)For tests, should I update every occurrence of this message? I see tests where this is still a warning and I'm not sure if I should update those.