Rollup merge of #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 https://github.com/rust-lang/rust/issues/60497#issuecomment-562665539:
> 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
This commit is contained in:
Mazdak Farrokhzad 2020-02-18 22:16:29 +01:00 committed by GitHub
commit c499570b5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 17 additions and 77 deletions

View File

@ -5,7 +5,7 @@ use super::{FnCtxt, Needs};
use rustc::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
use rustc::ty::TyKind::{Adt, Array, Char, FnDef, Never, Ref, Str, Tuple, Uint};
use rustc::ty::{self, Ty, TypeFoldable};
use rustc_errors::{self, struct_span_err, Applicability};
use rustc_errors::{self, struct_span_err, Applicability, DiagnosticBuilder};
use rustc_hir as hir;
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
use rustc_span::Span;
@ -321,11 +321,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
lhs_ty, missing_trait
));
} else if !suggested_deref {
err.note(&format!(
"an implementation of `{}` might \
be missing for `{}`",
missing_trait, lhs_ty
));
suggest_impl_missing(&mut err, lhs_ty, &missing_trait);
}
}
err.emit();
@ -467,11 +463,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
lhs_ty, missing_trait
));
} else if !suggested_deref && !involves_fn {
err.note(&format!(
"an implementation of `{}` might \
be missing for `{}`",
missing_trait, lhs_ty
));
suggest_impl_missing(&mut err, lhs_ty, &missing_trait);
}
}
err.emit();
@ -707,11 +699,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
hir::UnOp::UnNot => "std::ops::Not",
hir::UnOp::UnDeref => "std::ops::UnDerf",
};
err.note(&format!(
"an implementation of `{}` might \
be missing for `{}`",
missing_trait, operand_ty
));
suggest_impl_missing(&mut err, operand_ty, &missing_trait);
}
}
err.emit();
@ -929,3 +917,16 @@ fn is_builtin_binop<'tcx>(lhs: Ty<'tcx>, rhs: Ty<'tcx>, op: hir::BinOp) -> bool
}
}
}
/// If applicable, note that an implementation of `trait` for `ty` may fix the error.
fn suggest_impl_missing(err: &mut DiagnosticBuilder<'_>, ty: Ty<'_>, missing_trait: &str) {
if let Adt(def, _) = ty.peel_refs().kind {
if def.did.is_local() {
err.note(&format!(
"an implementation of `{}` might \
be missing for `{}`",
missing_trait, ty
));
}
}
}

View File

@ -5,8 +5,6 @@ LL | let z: isize = a.x + b.y;
| --- ^ --- std::boxed::Box<isize>
| |
| std::boxed::Box<isize>
|
= note: an implementation of `std::ops::Add` might be missing for `std::boxed::Box<isize>`
error[E0369]: cannot add `std::boxed::Box<isize>` to `std::boxed::Box<isize>`
--> $DIR/autoderef-full-lval.rs:21:33
@ -15,8 +13,6 @@ LL | let answer: isize = forty.a + two.a;
| ------- ^ ----- std::boxed::Box<isize>
| |
| std::boxed::Box<isize>
|
= note: an implementation of `std::ops::Add` might be missing for `std::boxed::Box<isize>`
error: aborting due to 2 previous errors

View File

@ -5,8 +5,6 @@ LL | fn main() { let x = "a".to_string() ^ "b".to_string(); }
| --------------- ^ --------------- std::string::String
| |
| std::string::String
|
= note: an implementation of `std::ops::BitXor` might be missing for `std::string::String`
error: aborting due to previous error

View File

@ -5,8 +5,6 @@ LL | fn main() { let x = true * false; }
| ---- ^ ----- bool
| |
| bool
|
= note: an implementation of `std::ops::Mul` might be missing for `bool`
error: aborting due to previous error

View File

@ -5,8 +5,6 @@ LL | let z = x + y;
| - ^ - {integer}
| |
| bool
|
= note: an implementation of `std::ops::Add` might be missing for `bool`
error: aborting due to previous error

View File

@ -23,8 +23,6 @@ LL | [0_usize; 33] == [1_usize; 33]
| ------------- ^^ ------------- [usize; 33]
| |
| [usize; 33]
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `[usize; 33]`
error[E0369]: binary operation `<` cannot be applied to type `[usize; 33]`
--> $DIR/core-traits-no-impls-length-33.rs:19:19
@ -33,8 +31,6 @@ LL | [0_usize; 33] < [1_usize; 33]
| ------------- ^ ------------- [usize; 33]
| |
| [usize; 33]
|
= note: an implementation of `std::cmp::PartialOrd` might be missing for `[usize; 33]`
error[E0277]: the trait bound `&[usize; 33]: std::iter::IntoIterator` is not satisfied
--> $DIR/core-traits-no-impls-length-33.rs:24:14

View File

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

View File

@ -5,8 +5,6 @@ LL | LinkedList::new() += 1;
| -----------------^^^^^
| |
| cannot use `+=` on type `std::collections::LinkedList<_>`
|
= note: an implementation of `std::ops::AddAssign` might be missing for `std::collections::LinkedList<_>`
error[E0067]: invalid left-hand side of assignment
--> $DIR/E0067.rs:4:23

View File

@ -23,8 +23,6 @@ LL | x += 2;
| -^^^^^
| |
| cannot use `+=` on type `&str`
|
= note: an implementation of `std::ops::AddAssign` might be missing for `&str`
error[E0599]: no method named `z` found for reference `&str` in the current scope
--> $DIR/error-festival.rs:16:7

View File

@ -5,8 +5,6 @@ LL | let x = () + ();
| -- ^ -- ()
| |
| ()
|
= note: an implementation of `std::ops::Add` might be missing for `()`
error: aborting due to previous error

View File

@ -5,8 +5,6 @@ LL | println!("{}", x + 1);
| - ^ - {integer}
| |
| std::boxed::Box<isize>
|
= note: an implementation of `std::ops::Add` might be missing for `std::boxed::Box<isize>`
error: aborting due to previous error

View File

@ -11,8 +11,6 @@ LL | ()+()
| --^-- ()
| |
| ()
|
= note: an implementation of `std::ops::Add` might be missing for `()`
error: aborting due to 2 previous errors

View File

@ -5,8 +5,6 @@ LL | let x = 5 + 6;
| - ^ - {integer}
| |
| {integer}
|
= note: an implementation of `std::ops::Add` might be missing for `{integer}`
error[E0369]: cannot add `i32` to `i32`
--> $DIR/issue-31076.rs:15:18
@ -15,8 +13,6 @@ LL | let y = 5i32 + 6i32;
| ---- ^ ---- i32
| |
| i32
|
= note: an implementation of `std::ops::Add` might be missing for `i32`
error: aborting due to 2 previous errors

View File

@ -5,8 +5,6 @@ LL | a.iter().map(|a| a*a)
| -^- &T
| |
| &T
|
= note: an implementation of `std::ops::Mul` might be missing for `&T`
error: aborting due to previous error

View File

@ -5,8 +5,6 @@ LL | () + f(&[1.0]);
| -- ^ --------- ()
| |
| ()
|
= note: an implementation of `std::ops::Add` might be missing for `()`
error: aborting due to previous error

View File

@ -5,8 +5,6 @@ LL | A = "" + 1
| -- ^ - {integer}
| |
| &str
|
= note: an implementation of `std::ops::Add` might be missing for `&str`
error[E0080]: evaluation of constant value failed
--> $DIR/issue-41394.rs:7:9

View File

@ -58,8 +58,6 @@ LL | foo > bar;
| --- ^ --- fn(i64) -> i64 {bar}
| |
| fn() -> i32 {foo}
|
= note: an implementation of `std::cmp::PartialOrd` might be missing for `fn() -> i32 {foo}`
error[E0308]: mismatched types
--> $DIR/issue-59488.rs:25:11
@ -79,7 +77,6 @@ LL | assert_eq!(Foo::Bar, i);
| fn(usize) -> Foo {Foo::Bar}
| fn(usize) -> Foo {Foo::Bar}
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `fn(usize) -> Foo {Foo::Bar}`
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: `fn(usize) -> Foo {Foo::Bar}` doesn't implement `std::fmt::Debug`

View File

@ -3,8 +3,6 @@ error[E0600]: cannot apply unary operator `-` to type `std::string::String`
|
LL | fn main() { -"foo".to_string(); }
| ^^^^^^^^^^^^^^^^^^ cannot apply unary operator `-`
|
= note: an implementation of `std::ops::Neg` might be missing for `std::string::String`
error: aborting due to previous error

View File

@ -5,8 +5,6 @@ LL | fn foo(t: Bar) -> isize { match t { Bar::T1(_, Some(x)) => { return x * 3;
| - ^ - {integer}
| |
| std::vec::Vec<isize>
|
= note: an implementation of `std::ops::Mul` might be missing for `std::vec::Vec<isize>`
error: aborting due to previous error

View File

@ -560,8 +560,6 @@ error[E0600]: cannot apply unary operator `-` to type `bool`
|
LL | if -let 0 = 0 {}
| ^^^^^^^^^^ cannot apply unary operator `-`
|
= note: an implementation of `std::ops::Neg` might be missing for `bool`
error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
--> $DIR/disallowed-positions.rs:46:8
@ -748,8 +746,6 @@ error[E0600]: cannot apply unary operator `-` to type `bool`
|
LL | while -let 0 = 0 {}
| ^^^^^^^^^^ cannot apply unary operator `-`
|
= note: an implementation of `std::ops::Neg` might be missing for `bool`
error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
--> $DIR/disallowed-positions.rs:110:11
@ -927,8 +923,6 @@ error[E0600]: cannot apply unary operator `-` to type `bool`
|
LL | -let 0 = 0;
| ^^^^^^^^^^ cannot apply unary operator `-`
|
= note: an implementation of `std::ops::Neg` might be missing for `bool`
error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
--> $DIR/disallowed-positions.rs:183:5

View File

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

View File

@ -5,8 +5,6 @@ LL | a * b
| - ^ - f64
| |
| &T
|
= note: an implementation of `std::ops::Mul` might be missing for `&T`
error: aborting due to previous error

View File

@ -3,8 +3,6 @@ error[E0600]: cannot apply unary operator `-` to type `bool`
|
LL | -true;
| ^^^^^ cannot apply unary operator `-`
|
= note: an implementation of `std::ops::Neg` might be missing for `bool`
error: aborting due to previous error

View File

@ -5,8 +5,6 @@ LL | let k = i + j;
| - ^ - std::vec::Vec<R>
| |
| std::vec::Vec<R>
|
= note: an implementation of `std::ops::Add` might be missing for `std::vec::Vec<R>`
error: aborting due to previous error