Skip to content

Commit eabc072

Browse files
authored
Rollup merge of #104895 - chenyukang:yukang/fix-104884-serde, r=TaKO8Ki
Avoid Invalid code suggested when encountering unsatisfied trait bounds in derive macro code Fixes #104884
2 parents 3d64420 + 3980945 commit eabc072

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

compiler/rustc_middle/src/ty/diagnostics.rs

+6
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,12 @@ pub fn suggest_constraining_type_params<'a>(
355355
));
356356
}
357357

358+
// FIXME: remove the suggestions that are from derive, as the span is not correct
359+
suggestions = suggestions
360+
.into_iter()
361+
.filter(|(span, _, _)| !span.in_derive_expansion())
362+
.collect::<Vec<_>>();
363+
358364
if suggestions.len() == 1 {
359365
let (span, suggestion, msg) = suggestions.pop().unwrap();
360366

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
6+
extern crate proc_macro;
7+
8+
use proc_macro::TokenStream;
9+
10+
#[proc_macro_derive(AddImpl)]
11+
12+
pub fn derive(input: TokenStream) -> TokenStream {
13+
"use std::cmp::Ordering;
14+
15+
impl<T> Ord for PriorityQueue<T> {
16+
fn cmp(&self, other: &Self) -> Ordering {
17+
self.0.cmp(&self.height)
18+
}
19+
}
20+
"
21+
.parse()
22+
.unwrap()
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// aux-build:issue-104884.rs
2+
3+
use std::collections::BinaryHeap;
4+
5+
#[macro_use]
6+
extern crate issue_104884;
7+
8+
#[derive(PartialEq, Eq, PartialOrd, Ord)]
9+
struct PriorityQueueEntry<T> {
10+
value: T,
11+
}
12+
13+
#[derive(PartialOrd, AddImpl)]
14+
//~^ ERROR can't compare `PriorityQueue<T>` with `PriorityQueue<T>`
15+
//~| ERROR the trait bound `PriorityQueue<T>: Eq` is not satisfied
16+
//~| ERROR can't compare `T` with `T`
17+
18+
struct PriorityQueue<T>(BinaryHeap<PriorityQueueEntry<T>>);
19+
20+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
error[E0277]: can't compare `PriorityQueue<T>` with `PriorityQueue<T>`
2+
--> $DIR/issue-104884-trait-impl-sugg-err.rs:13:10
3+
|
4+
LL | #[derive(PartialOrd, AddImpl)]
5+
| ^^^^^^^^^^ no implementation for `PriorityQueue<T> == PriorityQueue<T>`
6+
|
7+
= help: the trait `PartialEq` is not implemented for `PriorityQueue<T>`
8+
note: required by a bound in `PartialOrd`
9+
--> $SRC_DIR/core/src/cmp.rs:LL:COL
10+
|
11+
LL | pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
12+
| ^^^^^^^^^^^^^^ required by this bound in `PartialOrd`
13+
= note: this error originates in the derive macro `PartialOrd` (in Nightly builds, run with -Z macro-backtrace for more info)
14+
15+
error[E0277]: the trait bound `PriorityQueue<T>: Eq` is not satisfied
16+
--> $DIR/issue-104884-trait-impl-sugg-err.rs:13:22
17+
|
18+
LL | #[derive(PartialOrd, AddImpl)]
19+
| ^^^^^^^ the trait `Eq` is not implemented for `PriorityQueue<T>`
20+
|
21+
note: required by a bound in `Ord`
22+
--> $SRC_DIR/core/src/cmp.rs:LL:COL
23+
|
24+
LL | pub trait Ord: Eq + PartialOrd<Self> {
25+
| ^^ required by this bound in `Ord`
26+
= note: this error originates in the derive macro `AddImpl` (in Nightly builds, run with -Z macro-backtrace for more info)
27+
28+
error[E0277]: can't compare `T` with `T`
29+
--> $DIR/issue-104884-trait-impl-sugg-err.rs:13:22
30+
|
31+
LL | #[derive(PartialOrd, AddImpl)]
32+
| ^^^^^^^ no implementation for `T < T` and `T > T`
33+
|
34+
note: required for `PriorityQueue<T>` to implement `PartialOrd`
35+
--> $DIR/issue-104884-trait-impl-sugg-err.rs:13:10
36+
|
37+
LL | #[derive(PartialOrd, AddImpl)]
38+
| ^^^^^^^^^^
39+
note: required by a bound in `Ord`
40+
--> $SRC_DIR/core/src/cmp.rs:LL:COL
41+
|
42+
LL | pub trait Ord: Eq + PartialOrd<Self> {
43+
| ^^^^^^^^^^^^^^^^ required by this bound in `Ord`
44+
= note: this error originates in the derive macro `AddImpl` which comes from the expansion of the derive macro `PartialOrd` (in Nightly builds, run with -Z macro-backtrace for more info)
45+
46+
error: aborting due to 3 previous errors
47+
48+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)