fn add_static_impl_trait_suggestion(
        &self,
        infcx: &InferCtxt<'_, 'tcx>,
        diag: &mut DiagnosticBuilder<'_>,
        fr: RegionVid,
        // We need to pass `fr_name` - computing it again will label it twice.
        fr_name: RegionName,
        outlived_fr: RegionVid,
    ) {
        if let (Some(f), Some(ty::RegionKind::ReStatic)) =
            (self.to_error_region(fr), self.to_error_region(outlived_fr))
        {
            if let Some((ty::TyS {
                kind: ty::Opaque(did, substs),
                ..
            }, _)) = infcx
                .tcx
                .is_suitable_region(f)
                .map(|r| r.def_id)
                .map(|id| infcx.tcx.return_type_impl_trait(id))
                .unwrap_or(None)
            {
                // Check whether or not the impl trait return type is intended to capture
                // data with the static lifetime.
                //
                // eg. check for `impl Trait + 'static` instead of `impl Trait`.
                let has_static_predicate = {
                    let predicates_of = infcx.tcx.predicates_of(*did);
                    let bounds = predicates_of.instantiate(infcx.tcx, substs);

                    let mut found = false;
                    for predicate in bounds.predicates {
                        if let ty::Predicate::TypeOutlives(binder) = predicate {
                            if let ty::OutlivesPredicate(_, ty::RegionKind::ReStatic) =
                                binder.skip_binder()
                            {
                                found = true;
                                break;
                            }
                        }
                    }

                    found
                };

                debug!(
                    "add_static_impl_trait_suggestion: has_static_predicate={:?}",
                    has_static_predicate
                );
                let static_str = kw::StaticLifetime;
                // If there is a static predicate, then the only sensible suggestion is to replace
                // fr with `'static`.
                if has_static_predicate {
                    diag.help(&format!(
                        "consider replacing `{}` with `{}`",
                        fr_name, static_str,
                    ));
                } else {
                    // Otherwise, we should suggest adding a constraint on the return type.
                    let span = infcx.tcx.def_span(*did);
                    if let Ok(snippet) = infcx.tcx.sess.source_map().span_to_snippet(span) {
                        let suggestable_fr_name = if fr_name.was_named() {
                            fr_name.to_string()
                        } else {
                            "'_".to_string()
                        };

                        diag.span_suggestion(
                            span,
                            &format!(
                                "to allow this `impl Trait` to capture borrowed data with lifetime \
                                 `{}`, add `{}` as a constraint",
                                fr_name, suggestable_fr_name,
                            ),
                            format!("{} + {}", snippet, suggestable_fr_name),
                            Applicability::MachineApplicable,
                        );
                    }
                }
            }
        }
    }
/// Adds a suggestion to errors where a `impl Trait` is returned.
///
/// ```text
/// help: to allow this `impl Trait` to capture borrowed data with lifetime `'1`, add `'_` as
///       a constraint
///    |
/// LL |     fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
///    |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/// ```
Adds a suggestion to errors where a `impl Trait` is returned.
Adds a suggestion to errors where a ` impl Trait ` is returned .
fn add_static_impl_trait_suggestion(
        &self,
        infcx: &InferCtxt<'_, 'tcx>,
        diag: &mut DiagnosticBuilder<'_>,
        fr: RegionVid,
        // We need to pass `fr_name` - computing it again will label it twice.
        fr_name: RegionName,
        outlived_fr: RegionVid,
    ) {
        if let (Some(f), Some(ty::RegionKind::ReStatic)) =
            (self.to_error_region(fr), self.to_error_region(outlived_fr))
        {
            if let Some((ty::TyS {
                kind: ty::Opaque(did, substs),
                ..
            }, _)) = infcx
                .tcx
                .is_suitable_region(f)
                .map(|r| r.def_id)
                .map(|id| infcx.tcx.return_type_impl_trait(id))
                .unwrap_or(None)
            {
                // Check whether or not the impl trait return type is intended to capture
                // data with the static lifetime.
                //
                // eg. check for `impl Trait + 'static` instead of `impl Trait`.
                let has_static_predicate = {
                    let predicates_of = infcx.tcx.predicates_of(*did);
                    let bounds = predicates_of.instantiate(infcx.tcx, substs);
                    let mut found = false;
                    for predicate in bounds.predicates {
                        if let ty::Predicate::TypeOutlives(binder) = predicate {
                            if let ty::OutlivesPredicate(_, ty::RegionKind::ReStatic) =
                                binder.skip_binder()
                            {
                                found = true;
                                break;
                            }
                        }
                    }
                    found
                };
                debug!(
                    "add_static_impl_trait_suggestion: has_static_predicate={:?}",
                    has_static_predicate
                );
                let static_str = kw::StaticLifetime;
                // If there is a static predicate, then the only sensible suggestion is to replace
                // fr with `'static`.
                if has_static_predicate {
                    diag.help(&format!(
                        "consider replacing `{}` with `{}`",
                        fr_name, static_str,
                    ));
                } else {
                    // Otherwise, we should suggest adding a constraint on the return type.
                    let span = infcx.tcx.def_span(*did);
                    if let Ok(snippet) = infcx.tcx.sess.source_map().span_to_snippet(span) {
                        let suggestable_fr_name = if fr_name.was_named() {
                            fr_name.to_string()
                        } else {
                            "'_".to_string()
                        };
                        diag.span_suggestion(
                            span,
                            &format!(
                                "to allow this `impl Trait` to capture borrowed data with lifetime \
                                 `{}`, add `{}` as a constraint",
                                fr_name, suggestable_fr_name,
                            ),
                            format!("{} + {}", snippet, suggestable_fr_name),
                            Applicability::MachineApplicable,
                        );
                    }
                }
            }
        }
    }
fn add_static_impl_trait_suggestion ( & self , infcx : & InferCtxt < ' _ , ' tcx > , diag : & mut DiagnosticBuilder < ' _ > , fr : RegionVid , // We need to pass `fr_name` - computing it again will label it twice. fr_name : RegionName , outlived_fr : RegionVid , ) { if let ( Some ( f ) , Some ( ty :: RegionKind :: ReStatic ) ) = ( self . to_error_region ( fr ) , self . to_error_region ( outlived_fr ) ) { if let Some ( ( ty :: TyS { kind : ty :: Opaque ( did , substs ) , .. } , _ ) ) = infcx . tcx . is_suitable_region ( f ) . map ( | r | r . def_id ) . map ( | id | infcx . tcx . return_type_impl_trait ( id ) ) . unwrap_or ( None ) { // Check whether or not the impl trait return type is intended to capture // data with the static lifetime. // // eg. check for `impl Trait + 'static` instead of `impl Trait`. let has_static_predicate = { let predicates_of = infcx . tcx . predicates_of ( * did ) ; let bounds = predicates_of . instantiate ( infcx . tcx , substs ) ; let mut found = false ; for predicate in bounds . predicates { if let ty :: Predicate :: TypeOutlives ( binder ) = predicate { if let ty :: OutlivesPredicate ( _ , ty :: RegionKind :: ReStatic ) = binder . skip_binder ( ) { found = true ; break ; } } } found } ; debug ! ( " " has_static_predicate ) ; let static_str = kw :: StaticLifetime ; // If there is a static predicate, then the only sensible suggestion is to replace // fr with `'static`. if has_static_predicate { diag . help ( & format ! ( " " fr_name static_str ) ) ; } else { // Otherwise, we should suggest adding a constraint on the return type. let span = infcx . tcx . def_span ( * did ) ; if let Ok ( snippet ) = infcx . tcx . sess . source_map ( ) . span_to_snippet ( span ) { let suggestable_fr_name = if fr_name . was_named ( ) { fr_name . to_string ( ) } else { " " . to_string ( ) } ; diag . span_suggestion ( span , & format ! ( " \
 " fr_name suggestable_fr_name ) , format ! ( " " snippet suggestable_fr_name ) , Applicability :: MachineApplicable , ) ; } } } } }
Adds a suggestion to errors where a `impl Trait` is returned.
Adds a suggestion to errors where a ` impl Trait ` is returned .
&self
infcx
&InferCtxt<'_, 'tcx>
diag
&mut DiagnosticBuilder<'_>
fr
RegionVid
fr_name
RegionName
outlived_fr
RegionVid