mirror of
https://github.com/rust-lang/rust.git
synced 2026-06-01 14:10:03 +03:00
679f38c125
When encountering a bound coming from a derive macro, suggest manual impl of the trait.
Use the span for the specific param when adding bounds in builtin derive macros, so the diagnostic will point at them as well as the derive macro itself.
```
error[E0277]: can't compare `SomeNode` with `SomeNode`
--> f29.rs:24:15
|
24 | accept_eq(&node);
| --------- ^^^^^ no implementation for `SomeNode == SomeNode`
| |
| required by a bound introduced by this call
|
= note: -Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs:279:39
= help: the trait `PartialEq` is not implemented for `SomeNode`
note: required for `Id<SomeNode>` to implement `PartialEq`
--> f29.rs:3:10
|
3 | #[derive(PartialEq, Eq)]
| ^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
4 | pub struct Id<T>(PhantomData<T>);
| -
= help: consider manually implementing `PartialEq` to avoid undesired bounds
note: required by a bound in `accept_eq`
--> f29.rs:15:23
|
15 | fn accept_eq(_: &impl PartialEq) { }
| ^^^^^^^^^ required by this bound in `accept_eq`
help: consider annotating `SomeNode` with `#[derive(PartialEq)]`
|
13 + #[derive(PartialEq)]
14 | struct SomeNode();
|
```
35 lines
1.1 KiB
Rust
35 lines
1.1 KiB
Rust
// Second case reported in issue #108894.
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
pub struct Id<T>(PhantomData<T>);
|
|
|
|
// manual implementation which would break the usage of const patterns
|
|
// impl<T> PartialEq for Id<T> { fn eq(&self, _: &Id<T>) -> bool { true } }
|
|
// impl<T> Eq for Id<T> {}
|
|
|
|
// This derive is undesired but cannot be removed without
|
|
// breaking the usages below
|
|
// #[derive(PartialEq, Eq)]
|
|
struct SomeNode();
|
|
|
|
fn accept_eq(_: &impl PartialEq) { }
|
|
|
|
fn main() {
|
|
let node = Id::<SomeNode>(PhantomData);
|
|
|
|
// this will only work if
|
|
// - `Partial/Eq` is implemented manually, or
|
|
// - `SomeNode` also needlessly(?) implements `Partial/Eq`
|
|
accept_eq(&node); //~ ERROR can't compare `SomeNode` with `SomeNode`
|
|
|
|
const CONST_ID: Id::<SomeNode> = Id::<SomeNode>(PhantomData);
|
|
// this will work only when `Partial/Eq` is being derived
|
|
// otherwise: error: to use a constant of type `Id<SomeNode>` in a pattern,
|
|
// `Id<SomeNode>` must be annotated with `#[derive(PartialEq, Eq)]`
|
|
match node {
|
|
CONST_ID => {}
|
|
}
|
|
}
|