Skip to content

Commit

Permalink
Do not emit note suggesting to implement trait to foreign type
Browse files Browse the repository at this point in the history
Update tests

Extend to other operations

Refractor check in a separate function

Fix more tests
  • Loading branch information
LeSeulArtichaut committed Feb 17, 2020
1 parent 75b98fb commit 2e07892
Show file tree
Hide file tree
Showing 24 changed files with 17 additions and 77 deletions.
33 changes: 17 additions & 16 deletions src/librustc_typeck/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
));
}
}
}
4 changes: 0 additions & 4 deletions src/test/ui/autoderef-full-lval.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/binop/binop-bitxor-str.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/binop/binop-mul-bool.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/binop/binop-typeck.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 0 additions & 4 deletions src/test/ui/destructuring-assignment/note-unsupported.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/error-codes/E0067.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/error-festival.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/for/for-loop-type-error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ LL | let x = () + ();
| -- ^ -- ()
| |
| ()
|
= note: an implementation of `std::ops::Add` might be missing for `()`

error: aborting due to previous error

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/issues/issue-14915.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/issues/issue-24363.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ LL | ()+()
| --^-- ()
| |
| ()
|
= note: an implementation of `std::ops::Add` might be missing for `()`

error: aborting due to 2 previous errors

Expand Down
4 changes: 0 additions & 4 deletions src/test/ui/issues/issue-31076.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/issues/issue-35668.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/issues/issue-40610.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/issues/issue-41394.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/test/ui/issues/issue-59488.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/minus-string.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/pattern/pattern-tyvar-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions src/test/ui/span/issue-39018.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/traits/trait-resolution-in-overloaded-op.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/unop-neg-bool.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/vec/vec-res-add.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 2e07892

Please sign in to comment.