@@ -54,7 +54,6 @@ pub trait InferCtxtExt<'tcx> {
5454 obligation : & PredicateObligation < ' tcx > ,
5555 err : & mut DiagnosticBuilder < ' tcx > ,
5656 trait_ref : ty:: PolyTraitRef < ' tcx > ,
57- points_at_arg : bool ,
5857 ) ;
5958
6059 fn get_closure_name (
@@ -69,15 +68,13 @@ pub trait InferCtxtExt<'tcx> {
6968 obligation : & PredicateObligation < ' tcx > ,
7069 err : & mut DiagnosticBuilder < ' _ > ,
7170 trait_ref : ty:: Binder < ' tcx , ty:: TraitRef < ' tcx > > ,
72- points_at_arg : bool ,
7371 ) ;
7472
7573 fn suggest_add_reference_to_arg (
7674 & self ,
7775 obligation : & PredicateObligation < ' tcx > ,
7876 err : & mut DiagnosticBuilder < ' _ > ,
7977 trait_ref : & ty:: Binder < ' tcx , ty:: TraitRef < ' tcx > > ,
80- points_at_arg : bool ,
8178 has_custom_message : bool ,
8279 ) -> bool ;
8380
@@ -93,7 +90,6 @@ pub trait InferCtxtExt<'tcx> {
9390 obligation : & PredicateObligation < ' tcx > ,
9491 err : & mut DiagnosticBuilder < ' _ > ,
9592 trait_ref : ty:: Binder < ' tcx , ty:: TraitRef < ' tcx > > ,
96- points_at_arg : bool ,
9793 ) ;
9894
9995 fn suggest_semicolon_removal (
@@ -490,16 +486,19 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
490486 obligation : & PredicateObligation < ' tcx > ,
491487 err : & mut DiagnosticBuilder < ' tcx > ,
492488 trait_ref : ty:: PolyTraitRef < ' tcx > ,
493- points_at_arg : bool ,
494489 ) {
495490 // It only make sense when suggesting dereferences for arguments
496- if !points_at_arg {
491+ let code = if let ObligationCauseCode :: FunctionArgumentObligation { parent_code, .. } =
492+ & obligation. cause . code
493+ {
494+ std:: rc:: Rc :: clone ( parent_code)
495+ } else {
497496 return ;
498- }
497+ } ;
499498 let param_env = obligation. param_env ;
500499 let body_id = obligation. cause . body_id ;
501500 let span = obligation. cause . span ;
502- let real_trait_ref = match & obligation . cause . code {
501+ let real_trait_ref = match & * code {
503502 ObligationCauseCode :: ImplDerivedObligation ( cause)
504503 | ObligationCauseCode :: DerivedObligation ( cause)
505504 | ObligationCauseCode :: BuiltinDerivedObligation ( cause) => cause. parent_trait_ref ,
@@ -584,7 +583,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
584583 obligation : & PredicateObligation < ' tcx > ,
585584 err : & mut DiagnosticBuilder < ' _ > ,
586585 trait_ref : ty:: Binder < ' tcx , ty:: TraitRef < ' tcx > > ,
587- points_at_arg : bool ,
588586 ) {
589587 let self_ty = match trait_ref. self_ty ( ) . no_bound_vars ( ) {
590588 None => return ,
@@ -656,11 +654,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
656654 }
657655 _ => return ,
658656 } ;
659- if points_at_arg {
657+ if matches ! ( obligation . cause . code , ObligationCauseCode :: FunctionArgumentObligation { .. } ) {
660658 // When the obligation error has been ensured to have been caused by
661659 // an argument, the `obligation.cause.span` points at the expression
662- // of the argument, so we can provide a suggestion. This is signaled
663- // by `points_at_arg`. Otherwise, we give a more general note.
660+ // of the argument, so we can provide a suggestion. Otherwise, we give
661+ // a more general note.
664662 err. span_suggestion_verbose (
665663 obligation. cause . span . shrink_to_hi ( ) ,
666664 & msg,
@@ -677,7 +675,6 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
677675 obligation : & PredicateObligation < ' tcx > ,
678676 err : & mut DiagnosticBuilder < ' _ > ,
679677 trait_ref : & ty:: Binder < ' tcx , ty:: TraitRef < ' tcx > > ,
680- points_at_arg : bool ,
681678 has_custom_message : bool ,
682679 ) -> bool {
683680 let span = obligation. cause . span ;
@@ -686,9 +683,14 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
686683 ExpnKind :: Desugaring ( DesugaringKind :: ForLoop ( ForLoopLoc :: IntoIter ) )
687684 ) ;
688685
689- if !points_at_arg && !points_at_for_iter {
690- return false ;
691- }
686+ let code =
687+ if let ( ObligationCauseCode :: FunctionArgumentObligation { parent_code, .. } , false ) =
688+ ( & obligation. cause . code , points_at_for_iter)
689+ {
690+ std:: rc:: Rc :: clone ( parent_code)
691+ } else {
692+ return false ;
693+ } ;
692694
693695 // List of traits for which it would be nonsensical to suggest borrowing.
694696 // For instance, immutable references are always Copy, so suggesting to
@@ -787,7 +789,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
787789 return false ;
788790 } ;
789791
790- if let ObligationCauseCode :: ImplDerivedObligation ( obligation) = & obligation . cause . code {
792+ if let ObligationCauseCode :: ImplDerivedObligation ( obligation) = & * code {
791793 let expected_trait_ref = obligation. parent_trait_ref . skip_binder ( ) ;
792794 let new_imm_trait_ref =
793795 ty:: TraitRef :: new ( obligation. parent_trait_ref . def_id ( ) , imm_substs) ;
@@ -799,7 +801,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
799801 return try_borrowing ( new_mut_trait_ref, expected_trait_ref, true , & [ ] ) ;
800802 }
801803 } else if let ObligationCauseCode :: BindingObligation ( _, _)
802- | ObligationCauseCode :: ItemObligation ( _) = & obligation . cause . code
804+ | ObligationCauseCode :: ItemObligation ( _) = & * code
803805 {
804806 if try_borrowing (
805807 ty:: TraitRef :: new ( trait_ref. def_id , imm_substs) ,
@@ -891,8 +893,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
891893 obligation : & PredicateObligation < ' tcx > ,
892894 err : & mut DiagnosticBuilder < ' _ > ,
893895 trait_ref : ty:: Binder < ' tcx , ty:: TraitRef < ' tcx > > ,
894- points_at_arg : bool ,
895896 ) {
897+ let points_at_arg = matches ! (
898+ obligation. cause. code,
899+ ObligationCauseCode :: FunctionArgumentObligation { .. } ,
900+ ) ;
901+
896902 let span = obligation. cause . span ;
897903 if let Ok ( snippet) = self . tcx . sess . source_map ( ) . span_to_snippet ( span) {
898904 let refs_number =
@@ -2289,6 +2295,21 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
22892295 )
22902296 } ) ;
22912297 }
2298+ ObligationCauseCode :: FunctionArgumentObligation {
2299+ arg_hir_id : _,
2300+ call_hir_id : _,
2301+ ref parent_code,
2302+ } => {
2303+ ensure_sufficient_stack ( || {
2304+ self . note_obligation_cause_code (
2305+ err,
2306+ predicate,
2307+ & parent_code,
2308+ obligated_types,
2309+ seen_requirements,
2310+ )
2311+ } ) ;
2312+ }
22922313 ObligationCauseCode :: CompareImplMethodObligation {
22932314 item_name,
22942315 trait_item_def_id,
0 commit comments