Skip to content

Commit 2e07892

Browse files
Do not emit note suggesting to implement trait to foreign type
Update tests Extend to other operations Refractor check in a separate function Fix more tests
1 parent 75b98fb commit 2e07892

24 files changed

+17
-77
lines changed

src/librustc_typeck/check/op.rs

Lines changed: 17 additions & 16 deletions
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

Lines changed: 0 additions & 4 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 4 deletions
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

Lines changed: 0 additions & 4 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 0 additions & 2 deletions
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

0 commit comments

Comments
 (0)