Skip to content

Commit 8da95ac

Browse files
authored
Rollup merge of #147281 - fee1-dead-contrib:clarify-binop-diag, r=jackh726
Make diagnostics clearer for binop-related errors in foreign crates Fixes redundant language and bad grammar.
2 parents 4602127 + 3c6ae00 commit 8da95ac

File tree

10 files changed

+47
-71
lines changed

10 files changed

+47
-71
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3046,46 +3046,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30463046
}
30473047

30483048
foreign_preds.sort_by_key(|pred: &&ty::TraitPredicate<'_>| pred.trait_ref.to_string());
3049-
let foreign_def_ids = foreign_preds
3050-
.iter()
3051-
.filter_map(|pred| match pred.self_ty().kind() {
3052-
ty::Adt(def, _) => Some(def.did()),
3053-
_ => None,
3054-
})
3055-
.collect::<FxIndexSet<_>>();
3056-
let mut foreign_spans: MultiSpan = foreign_def_ids
3057-
.iter()
3058-
.filter_map(|def_id| {
3059-
let span = self.tcx.def_span(*def_id);
3060-
if span.is_dummy() { None } else { Some(span) }
3061-
})
3062-
.collect::<Vec<_>>()
3063-
.into();
3064-
for pred in &foreign_preds {
3065-
if let ty::Adt(def, _) = pred.self_ty().kind() {
3066-
foreign_spans.push_span_label(
3067-
self.tcx.def_span(def.did()),
3068-
format!("not implement `{}`", pred.trait_ref.print_trait_sugared()),
3069-
);
3049+
3050+
for pred in foreign_preds {
3051+
let ty = pred.self_ty();
3052+
let ty::Adt(def, _) = ty.kind() else { continue };
3053+
let span = self.tcx.def_span(def.did());
3054+
if span.is_dummy() {
3055+
continue;
30703056
}
3071-
}
3072-
if foreign_spans.primary_span().is_some() {
3073-
let msg = if let [foreign_pred] = foreign_preds.as_slice() {
3074-
format!(
3075-
"the foreign item type `{}` doesn't implement `{}`",
3076-
foreign_pred.self_ty(),
3077-
foreign_pred.trait_ref.print_trait_sugared()
3078-
)
3079-
} else {
3080-
format!(
3081-
"the foreign item type{} {} implement required trait{} for this \
3082-
operation to be valid",
3083-
pluralize!(foreign_def_ids.len()),
3084-
if foreign_def_ids.len() > 1 { "don't" } else { "doesn't" },
3085-
pluralize!(foreign_preds.len()),
3086-
)
3087-
};
3088-
err.span_note(foreign_spans, msg);
3057+
let mut mspan: MultiSpan = span.into();
3058+
mspan.push_span_label(span, format!("`{ty}` is defined in another crate"));
3059+
err.span_note(
3060+
mspan,
3061+
format!("`{ty}` does not implement `{}`", pred.trait_ref.print_trait_sugared()),
3062+
);
30893063
}
30903064

30913065
let preds: Vec<_> = errors

tests/ui/array-slice-vec/vec-res-add.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ LL | let k = i + j;
66
| |
77
| Vec<R>
88
|
9-
note: the foreign item type `Vec<R>` doesn't implement `Add`
9+
note: `Vec<R>` does not implement `Add`
1010
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
1111
|
12-
= note: not implement `Add`
12+
= note: `Vec<R>` is defined in another crate
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/autoref-autoderef/autoderef-box-no-add.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ LL | let z: isize = a.x + b.y;
66
| |
77
| Box<isize>
88
|
9-
note: the foreign item type `Box<isize>` doesn't implement `Add`
9+
note: `Box<isize>` does not implement `Add`
1010
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
1111
::: $SRC_DIR/alloc/src/boxed.rs:LL:COL
1212
|
13-
= note: not implement `Add`
13+
= note: `Box<isize>` is defined in another crate
1414

1515
error[E0369]: cannot add `Box<isize>` to `Box<isize>`
1616
--> $DIR/autoderef-box-no-add.rs:31:33
@@ -20,11 +20,11 @@ LL | let answer: isize = forty.a + two.a;
2020
| |
2121
| Box<isize>
2222
|
23-
note: the foreign item type `Box<isize>` doesn't implement `Add`
23+
note: `Box<isize>` does not implement `Add`
2424
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
2525
::: $SRC_DIR/alloc/src/boxed.rs:LL:COL
2626
|
27-
= note: not implement `Add`
27+
= note: `Box<isize>` is defined in another crate
2828

2929
error: aborting due to 2 previous errors
3030

tests/ui/binop/binary-op-not-allowed-issue-125631.stderr

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ note: an implementation of `PartialEq` might be missing for `T1`
1111
|
1212
LL | struct T1;
1313
| ^^^^^^^^^ must implement `PartialEq`
14-
note: the foreign item type `std::io::Error` doesn't implement `PartialEq`
14+
note: `std::io::Error` does not implement `PartialEq`
1515
--> $SRC_DIR/std/src/io/error.rs:LL:COL
1616
|
17-
= note: not implement `PartialEq`
17+
= note: `std::io::Error` is defined in another crate
1818
help: consider annotating `T1` with `#[derive(PartialEq)]`
1919
|
2020
LL + #[derive(PartialEq)]
@@ -29,13 +29,14 @@ LL | (Error::new(ErrorKind::Other, "2"), thread::current())
2929
LL | == (Error::new(ErrorKind::Other, "2"), thread::current());
3030
| ^^ ------------------------------------------------------ (std::io::Error, Thread)
3131
|
32-
note: the foreign item types don't implement required traits for this operation to be valid
33-
--> $SRC_DIR/std/src/io/error.rs:LL:COL
34-
|
35-
= note: not implement `PartialEq`
32+
note: `Thread` does not implement `PartialEq`
3633
--> $SRC_DIR/std/src/thread/mod.rs:LL:COL
3734
|
38-
= note: not implement `PartialEq`
35+
= note: `Thread` is defined in another crate
36+
note: `std::io::Error` does not implement `PartialEq`
37+
--> $SRC_DIR/std/src/io/error.rs:LL:COL
38+
|
39+
= note: `std::io::Error` is defined in another crate
3940

4041
error[E0369]: binary operation `==` cannot be applied to type `(std::io::Error, Thread, T1, T2)`
4142
--> $DIR/binary-op-not-allowed-issue-125631.rs:14:9
@@ -52,13 +53,14 @@ LL | struct T1;
5253
| ^^^^^^^^^ must implement `PartialEq`
5354
LL | struct T2;
5455
| ^^^^^^^^^ must implement `PartialEq`
55-
note: the foreign item types don't implement required traits for this operation to be valid
56-
--> $SRC_DIR/std/src/io/error.rs:LL:COL
57-
|
58-
= note: not implement `PartialEq`
56+
note: `Thread` does not implement `PartialEq`
5957
--> $SRC_DIR/std/src/thread/mod.rs:LL:COL
6058
|
61-
= note: not implement `PartialEq`
59+
= note: `Thread` is defined in another crate
60+
note: `std::io::Error` does not implement `PartialEq`
61+
--> $SRC_DIR/std/src/io/error.rs:LL:COL
62+
|
63+
= note: `std::io::Error` is defined in another crate
6264
help: consider annotating `T1` with `#[derive(PartialEq)]`
6365
|
6466
LL + #[derive(PartialEq)]

tests/ui/binop/binop-bitxor-str.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ LL | fn main() { let x = "a".to_string() ^ "b".to_string(); }
66
| |
77
| String
88
|
9-
note: the foreign item type `String` doesn't implement `BitXor`
9+
note: `String` does not implement `BitXor`
1010
--> $SRC_DIR/alloc/src/string.rs:LL:COL
1111
|
12-
= note: not implement `BitXor`
12+
= note: `String` is defined in another crate
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/error-codes/E0067.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ LL | LinkedList::new() += 1;
66
| |
77
| cannot use `+=` on type `LinkedList<_>`
88
|
9-
note: the foreign item type `LinkedList<_>` doesn't implement `AddAssign<{integer}>`
9+
note: `LinkedList<_>` does not implement `AddAssign<{integer}>`
1010
--> $SRC_DIR/alloc/src/collections/linked_list.rs:LL:COL
1111
::: $SRC_DIR/alloc/src/collections/linked_list.rs:LL:COL
1212
|
13-
= note: not implement `AddAssign<{integer}>`
13+
= note: `LinkedList<_>` is defined in another crate
1414

1515
error[E0067]: invalid left-hand side of assignment
1616
--> $DIR/E0067.rs:4:23

tests/ui/operator-recovery/box-arithmetic-14915.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ LL | println!("{}", x + 1);
66
| |
77
| Box<isize>
88
|
9-
note: the foreign item type `Box<isize>` doesn't implement `Add<{integer}>`
9+
note: `Box<isize>` does not implement `Add<{integer}>`
1010
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
1111
::: $SRC_DIR/alloc/src/boxed.rs:LL:COL
1212
|
13-
= note: not implement `Add<{integer}>`
13+
= note: `Box<isize>` is defined in another crate
1414

1515
error: aborting due to 1 previous error
1616

tests/ui/pattern/pattern-tyvar-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ LL | fn foo(t: Bar) -> isize { match t { Bar::T1(_, Some(x)) => { return x * 3;
66
| |
77
| Vec<isize>
88
|
9-
note: the foreign item type `Vec<isize>` doesn't implement `Mul<{integer}>`
9+
note: `Vec<isize>` does not implement `Mul<{integer}>`
1010
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
1111
|
12-
= note: not implement `Mul<{integer}>`
12+
= note: `Vec<isize>` is defined in another crate
1313

1414
error: aborting due to 1 previous error
1515

tests/ui/typeck/assign-non-lval-derefmut.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ LL | x.lock().unwrap() += 1;
1919
| |
2020
| cannot use `+=` on type `std::sync::MutexGuard<'_, usize>`
2121
|
22-
note: the foreign item type `std::sync::MutexGuard<'_, usize>` doesn't implement `AddAssign<{integer}>`
22+
note: `std::sync::MutexGuard<'_, usize>` does not implement `AddAssign<{integer}>`
2323
--> $SRC_DIR/std/src/sync/poison/mutex.rs:LL:COL
2424
|
25-
= note: not implement `AddAssign<{integer}>`
25+
= note: `std::sync::MutexGuard<'_, usize>` is defined in another crate
2626
help: `+=` can be used on `usize` if you dereference the left-hand side
2727
|
2828
LL | *x.lock().unwrap() += 1;
@@ -51,10 +51,10 @@ LL | y += 1;
5151
| |
5252
| cannot use `+=` on type `std::sync::MutexGuard<'_, usize>`
5353
|
54-
note: the foreign item type `std::sync::MutexGuard<'_, usize>` doesn't implement `AddAssign<{integer}>`
54+
note: `std::sync::MutexGuard<'_, usize>` does not implement `AddAssign<{integer}>`
5555
--> $SRC_DIR/std/src/sync/poison/mutex.rs:LL:COL
5656
|
57-
= note: not implement `AddAssign<{integer}>`
57+
= note: `std::sync::MutexGuard<'_, usize>` is defined in another crate
5858
help: `+=` can be used on `usize` if you dereference the left-hand side
5959
|
6060
LL | *y += 1;

tests/ui/typeck/minus-string.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ error[E0600]: cannot apply unary operator `-` to type `String`
44
LL | -"foo".to_string();
55
| ^^^^^^^^^^^^^^^^^^ cannot apply unary operator `-`
66
|
7-
note: the foreign item type `String` doesn't implement `Neg`
7+
note: `String` does not implement `Neg`
88
--> $SRC_DIR/alloc/src/string.rs:LL:COL
99
|
10-
= note: not implement `Neg`
10+
= note: `String` is defined in another crate
1111

1212
error: aborting due to 1 previous error
1313

0 commit comments

Comments
 (0)