@@ -5,7 +5,7 @@ use super::{FnCtxt, Needs};
55use rustc:: ty:: adjustment:: { Adjust , Adjustment , AllowTwoPhase , AutoBorrow , AutoBorrowMutability } ;
66use rustc:: ty:: TyKind :: { Adt , Array , Char , FnDef , Never , Ref , Str , Tuple , Uint } ;
77use rustc:: ty:: { self , Ty , TypeFoldable } ;
8- use rustc_errors:: { self , struct_span_err, Applicability } ;
8+ use rustc_errors:: { self , struct_span_err, Applicability , DiagnosticBuilder } ;
99use rustc_hir as hir;
1010use rustc_infer:: infer:: type_variable:: { TypeVariableOrigin , TypeVariableOriginKind } ;
1111use 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+ }
0 commit comments