@@ -5,7 +5,7 @@ use super::{FnCtxt, Needs};
5
5
use rustc:: ty:: adjustment:: { Adjust , Adjustment , AllowTwoPhase , AutoBorrow , AutoBorrowMutability } ;
6
6
use rustc:: ty:: TyKind :: { Adt , Array , Char , FnDef , Never , Ref , Str , Tuple , Uint } ;
7
7
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 } ;
9
9
use rustc_hir as hir;
10
10
use rustc_infer:: infer:: type_variable:: { TypeVariableOrigin , TypeVariableOriginKind } ;
11
11
use rustc_span:: Span ;
@@ -321,11 +321,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
321
321
lhs_ty, missing_trait
322
322
) ) ;
323
323
} 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) ;
329
325
}
330
326
}
331
327
err. emit ( ) ;
@@ -467,11 +463,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
467
463
lhs_ty, missing_trait
468
464
) ) ;
469
465
} 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) ;
475
467
}
476
468
}
477
469
err. emit ( ) ;
@@ -707,11 +699,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
707
699
hir:: UnOp :: UnNot => "std::ops::Not" ,
708
700
hir:: UnOp :: UnDeref => "std::ops::UnDerf" ,
709
701
} ;
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) ;
715
703
}
716
704
}
717
705
err. emit ( ) ;
@@ -929,3 +917,16 @@ fn is_builtin_binop<'tcx>(lhs: Ty<'tcx>, rhs: Ty<'tcx>, op: hir::BinOp) -> bool
929
917
}
930
918
}
931
919
}
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
+ }
0 commit comments