Skip to content

Commit c499570

Browse files
authored
Rollup merge of rust-lang#69217 - LeSeulArtichaut:remove-lint-impl-op, r=estebank
Do not emit note suggesting to implement operation trait to foreign type When a binary operation isn't valid, you will get a lint proposing to add a trait implementation to make the operation possible. However, this cannot be done for foreign types, such as types from `core` or `std`. For example: ``` = note: an implementation of `std::ops::Add` might be missing for `std::option::Option<i8>` ``` As mentioned in rust-lang#60497 (comment): > The note suggesting implementing Add<i8> should only be emitted if Option<i8> were local to the current crate, which it isn't, so in this case it shouldn't be emitted. (I will use the CI to check tests for me, or my computer will just burn... and running IDEs is not possible on a pile of ashes) r? @estebank
2 parents 1cf0194 + 2e07892 commit c499570

24 files changed

+17
-77
lines changed

src/librustc_typeck/check/op.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{FnCtxt, Needs};
55
use rustc::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
66
use rustc::ty::TyKind::{Adt, Array, Char, FnDef, Never, Ref, Str, Tuple, Uint};
77
use rustc::ty::{self, Ty, TypeFoldable};
8-
use rustc_errors::{self, struct_span_err, Applicability};
8+
use rustc_errors::{self, struct_span_err, Applicability, DiagnosticBuilder};
99
use rustc_hir as hir;
1010
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1111
use rustc_span::Span;
@@ -321,11 +321,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
321321
lhs_ty, missing_trait
322322
));
323323
} else if !suggested_deref {
324-
err.note(&format!(
325-
"an implementation of `{}` might \
326-
be missing for `{}`",
327-
missing_trait, lhs_ty
328-
));
324+
suggest_impl_missing(&mut err, lhs_ty, &missing_trait);
329325
}
330326
}
331327
err.emit();
@@ -467,11 +463,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
467463
lhs_ty, missing_trait
468464
));
469465
} else if !suggested_deref && !involves_fn {
470-
err.note(&format!(
471-
"an implementation of `{}` might \
472-
be missing for `{}`",
473-
missing_trait, lhs_ty
474-
));
466+
suggest_impl_missing(&mut err, lhs_ty, &missing_trait);
475467
}
476468
}
477469
err.emit();
@@ -707,11 +699,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
707699
hir::UnOp::UnNot => "std::ops::Not",
708700
hir::UnOp::UnDeref => "std::ops::UnDerf",
709701
};
710-
err.note(&format!(
711-
"an implementation of `{}` might \
712-
be missing for `{}`",
713-
missing_trait, operand_ty
714-
));
702+
suggest_impl_missing(&mut err, operand_ty, &missing_trait);
715703
}
716704
}
717705
err.emit();
@@ -929,3 +917,16 @@ fn is_builtin_binop<'tcx>(lhs: Ty<'tcx>, rhs: Ty<'tcx>, op: hir::BinOp) -> bool
929917
}
930918
}
931919
}
920+
921+
/// If applicable, note that an implementation of `trait` for `ty` may fix the error.
922+
fn suggest_impl_missing(err: &mut DiagnosticBuilder<'_>, ty: Ty<'_>, missing_trait: &str) {
923+
if let Adt(def, _) = ty.peel_refs().kind {
924+
if def.did.is_local() {
925+
err.note(&format!(
926+
"an implementation of `{}` might \
927+
be missing for `{}`",
928+
missing_trait, ty
929+
));
930+
}
931+
}
932+
}

src/test/ui/autoderef-full-lval.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | let z: isize = a.x + b.y;
55
| --- ^ --- std::boxed::Box<isize>
66
| |
77
| std::boxed::Box<isize>
8-
|
9-
= note: an implementation of `std::ops::Add` might be missing for `std::boxed::Box<isize>`
108

119
error[E0369]: cannot add `std::boxed::Box<isize>` to `std::boxed::Box<isize>`
1210
--> $DIR/autoderef-full-lval.rs:21:33
@@ -15,8 +13,6 @@ LL | let answer: isize = forty.a + two.a;
1513
| ------- ^ ----- std::boxed::Box<isize>
1614
| |
1715
| std::boxed::Box<isize>
18-
|
19-
= note: an implementation of `std::ops::Add` might be missing for `std::boxed::Box<isize>`
2016

2117
error: aborting due to 2 previous errors
2218

src/test/ui/binop/binop-bitxor-str.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | fn main() { let x = "a".to_string() ^ "b".to_string(); }
55
| --------------- ^ --------------- std::string::String
66
| |
77
| std::string::String
8-
|
9-
= note: an implementation of `std::ops::BitXor` might be missing for `std::string::String`
108

119
error: aborting due to previous error
1210

src/test/ui/binop/binop-mul-bool.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | fn main() { let x = true * false; }
55
| ---- ^ ----- bool
66
| |
77
| bool
8-
|
9-
= note: an implementation of `std::ops::Mul` might be missing for `bool`
108

119
error: aborting due to previous error
1210

src/test/ui/binop/binop-typeck.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | let z = x + y;
55
| - ^ - {integer}
66
| |
77
| bool
8-
|
9-
= note: an implementation of `std::ops::Add` might be missing for `bool`
108

119
error: aborting due to previous error
1210

src/test/ui/const-generics/array-impls/core-traits-no-impls-length-33.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ LL | [0_usize; 33] == [1_usize; 33]
2323
| ------------- ^^ ------------- [usize; 33]
2424
| |
2525
| [usize; 33]
26-
|
27-
= note: an implementation of `std::cmp::PartialEq` might be missing for `[usize; 33]`
2826

2927
error[E0369]: binary operation `<` cannot be applied to type `[usize; 33]`
3028
--> $DIR/core-traits-no-impls-length-33.rs:19:19
@@ -33,8 +31,6 @@ LL | [0_usize; 33] < [1_usize; 33]
3331
| ------------- ^ ------------- [usize; 33]
3432
| |
3533
| [usize; 33]
36-
|
37-
= note: an implementation of `std::cmp::PartialOrd` might be missing for `[usize; 33]`
3834

3935
error[E0277]: the trait bound `&[usize; 33]: std::iter::IntoIterator` is not satisfied
4036
--> $DIR/core-traits-no-impls-length-33.rs:24:14

src/test/ui/destructuring-assignment/note-unsupported.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ LL | (a, b) += (3, 4);
1616
| ------^^^^^^^^^^
1717
| |
1818
| cannot use `+=` on type `({integer}, {integer})`
19-
|
20-
= note: an implementation of `std::ops::AddAssign` might be missing for `({integer}, {integer})`
2119

2220
error[E0067]: invalid left-hand side of assignment
2321
--> $DIR/note-unsupported.rs:7:12
@@ -48,8 +46,6 @@ LL | [a, b] += [3, 4];
4846
| ------^^^^^^^^^^
4947
| |
5048
| cannot use `+=` on type `[{integer}; 2]`
51-
|
52-
= note: an implementation of `std::ops::AddAssign` might be missing for `[{integer}; 2]`
5349

5450
error[E0067]: invalid left-hand side of assignment
5551
--> $DIR/note-unsupported.rs:11:12

src/test/ui/error-codes/E0067.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | LinkedList::new() += 1;
55
| -----------------^^^^^
66
| |
77
| cannot use `+=` on type `std::collections::LinkedList<_>`
8-
|
9-
= note: an implementation of `std::ops::AddAssign` might be missing for `std::collections::LinkedList<_>`
108

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

src/test/ui/error-festival.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ LL | x += 2;
2323
| -^^^^^
2424
| |
2525
| cannot use `+=` on type `&str`
26-
|
27-
= note: an implementation of `std::ops::AddAssign` might be missing for `&str`
2826

2927
error[E0599]: no method named `z` found for reference `&str` in the current scope
3028
--> $DIR/error-festival.rs:16:7

src/test/ui/for/for-loop-type-error.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | let x = () + ();
55
| -- ^ -- ()
66
| |
77
| ()
8-
|
9-
= note: an implementation of `std::ops::Add` might be missing for `()`
108

119
error: aborting due to previous error
1210

src/test/ui/issues/issue-14915.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | println!("{}", x + 1);
55
| - ^ - {integer}
66
| |
77
| std::boxed::Box<isize>
8-
|
9-
= note: an implementation of `std::ops::Add` might be missing for `std::boxed::Box<isize>`
108

119
error: aborting due to previous error
1210

src/test/ui/issues/issue-24363.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ LL | ()+()
1111
| --^-- ()
1212
| |
1313
| ()
14-
|
15-
= note: an implementation of `std::ops::Add` might be missing for `()`
1614

1715
error: aborting due to 2 previous errors
1816

src/test/ui/issues/issue-31076.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | let x = 5 + 6;
55
| - ^ - {integer}
66
| |
77
| {integer}
8-
|
9-
= note: an implementation of `std::ops::Add` might be missing for `{integer}`
108

119
error[E0369]: cannot add `i32` to `i32`
1210
--> $DIR/issue-31076.rs:15:18
@@ -15,8 +13,6 @@ LL | let y = 5i32 + 6i32;
1513
| ---- ^ ---- i32
1614
| |
1715
| i32
18-
|
19-
= note: an implementation of `std::ops::Add` might be missing for `i32`
2016

2117
error: aborting due to 2 previous errors
2218

src/test/ui/issues/issue-35668.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | a.iter().map(|a| a*a)
55
| -^- &T
66
| |
77
| &T
8-
|
9-
= note: an implementation of `std::ops::Mul` might be missing for `&T`
108

119
error: aborting due to previous error
1210

src/test/ui/issues/issue-40610.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | () + f(&[1.0]);
55
| -- ^ --------- ()
66
| |
77
| ()
8-
|
9-
= note: an implementation of `std::ops::Add` might be missing for `()`
108

119
error: aborting due to previous error
1210

src/test/ui/issues/issue-41394.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | A = "" + 1
55
| -- ^ - {integer}
66
| |
77
| &str
8-
|
9-
= note: an implementation of `std::ops::Add` might be missing for `&str`
108

119
error[E0080]: evaluation of constant value failed
1210
--> $DIR/issue-41394.rs:7:9

src/test/ui/issues/issue-59488.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ LL | foo > bar;
5858
| --- ^ --- fn(i64) -> i64 {bar}
5959
| |
6060
| fn() -> i32 {foo}
61-
|
62-
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() -> i32 {foo}`
6361

6462
error[E0308]: mismatched types
6563
--> $DIR/issue-59488.rs:25:11
@@ -79,7 +77,6 @@ LL | assert_eq!(Foo::Bar, i);
7977
| fn(usize) -> Foo {Foo::Bar}
8078
| fn(usize) -> Foo {Foo::Bar}
8179
|
82-
= note: an implementation of `std::cmp::PartialEq` might be missing for `fn(usize) -> Foo {Foo::Bar}`
8380
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
8481

8582
error[E0277]: `fn(usize) -> Foo {Foo::Bar}` doesn't implement `std::fmt::Debug`

src/test/ui/minus-string.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error[E0600]: cannot apply unary operator `-` to type `std::string::String`
33
|
44
LL | fn main() { -"foo".to_string(); }
55
| ^^^^^^^^^^^^^^^^^^ cannot apply unary operator `-`
6-
|
7-
= note: an implementation of `std::ops::Neg` might be missing for `std::string::String`
86

97
error: aborting due to previous error
108

src/test/ui/pattern/pattern-tyvar-2.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | fn foo(t: Bar) -> isize { match t { Bar::T1(_, Some(x)) => { return x * 3;
55
| - ^ - {integer}
66
| |
77
| std::vec::Vec<isize>
8-
|
9-
= note: an implementation of `std::ops::Mul` might be missing for `std::vec::Vec<isize>`
108

119
error: aborting due to previous error
1210

src/test/ui/rfc-2497-if-let-chains/disallowed-positions.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,6 @@ error[E0600]: cannot apply unary operator `-` to type `bool`
560560
|
561561
LL | if -let 0 = 0 {}
562562
| ^^^^^^^^^^ cannot apply unary operator `-`
563-
|
564-
= note: an implementation of `std::ops::Neg` might be missing for `bool`
565563

566564
error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
567565
--> $DIR/disallowed-positions.rs:46:8
@@ -748,8 +746,6 @@ error[E0600]: cannot apply unary operator `-` to type `bool`
748746
|
749747
LL | while -let 0 = 0 {}
750748
| ^^^^^^^^^^ cannot apply unary operator `-`
751-
|
752-
= note: an implementation of `std::ops::Neg` might be missing for `bool`
753749

754750
error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
755751
--> $DIR/disallowed-positions.rs:110:11
@@ -927,8 +923,6 @@ error[E0600]: cannot apply unary operator `-` to type `bool`
927923
|
928924
LL | -let 0 = 0;
929925
| ^^^^^^^^^^ cannot apply unary operator `-`
930-
|
931-
= note: an implementation of `std::ops::Neg` might be missing for `bool`
932926

933927
error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
934928
--> $DIR/disallowed-positions.rs:183:5

src/test/ui/span/issue-39018.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ LL | let _ = &c + &d;
136136
| -- ^ -- &&str
137137
| |
138138
| &&str
139-
|
140-
= note: an implementation of `std::ops::Add` might be missing for `&&str`
141139

142140
error[E0369]: cannot add `&str` to `&&str`
143141
--> $DIR/issue-39018.rs:35:16
@@ -146,8 +144,6 @@ LL | let _ = &c + d;
146144
| -- ^ - &str
147145
| |
148146
| &&str
149-
|
150-
= note: an implementation of `std::ops::Add` might be missing for `&&str`
151147

152148
error[E0369]: cannot add `&&str` to `&str`
153149
--> $DIR/issue-39018.rs:36:15

src/test/ui/traits/trait-resolution-in-overloaded-op.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | a * b
55
| - ^ - f64
66
| |
77
| &T
8-
|
9-
= note: an implementation of `std::ops::Mul` might be missing for `&T`
108

119
error: aborting due to previous error
1210

src/test/ui/unop-neg-bool.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error[E0600]: cannot apply unary operator `-` to type `bool`
33
|
44
LL | -true;
55
| ^^^^^ cannot apply unary operator `-`
6-
|
7-
= note: an implementation of `std::ops::Neg` might be missing for `bool`
86

97
error: aborting due to previous error
108

src/test/ui/vec/vec-res-add.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | let k = i + j;
55
| - ^ - std::vec::Vec<R>
66
| |
77
| std::vec::Vec<R>
8-
|
9-
= note: an implementation of `std::ops::Add` might be missing for `std::vec::Vec<R>`
108

119
error: aborting due to previous error
1210

0 commit comments

Comments
 (0)