fn add_static_impl_trait_suggestion(
        &self,
        diag: &mut DiagnosticBuilder<'tcx>,
        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::Opaque(did, substs)) = self
                .infcx
                .tcx
                .is_suitable_region(f)
                .map(|r| r.def_id)
                .and_then(|id| self.infcx.tcx.return_type_impl_trait(id))
                .map(|(ty, _)| ty.kind())
            {
                // 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 bounds = self.infcx.tcx.explicit_item_bounds(did);

                    let mut found = false;
                    for (bound, _) in bounds {
                        if let ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_, r)) =
                            bound.kind().skip_binder()
                        {
                            let r = r.subst(self.infcx.tcx, substs);
                            if let ty::RegionKind::ReStatic = r {
                                found = true;
                                break;
                            } else {
                                // If there's already a lifetime bound, don't
                                // suggest anything.
                                return;
                            }
                        }
                    }

                    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 = self.infcx.tcx.def_span(did);
                    if let Ok(snippet) = self.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()
                        };
                        let span = if snippet.ends_with(';') {
                            // `type X = impl Trait;`
                            span.with_hi(span.hi() - BytePos(1))
                        } else {
                            span
                        };
                        let suggestion = format!(" + {}", suggestable_fr_name);
                        let span = span.shrink_to_hi();
                        diag.span_suggestion(
                            span,
                            &format!(
                                "to allow this `impl Trait` to capture borrowed data with lifetime \
                                 `{}`, add `{}` as a bound",
                                fr_name, suggestable_fr_name,
                            ),
                            suggestion,
                            Applicability::MachineApplicable,
                        );
                    }
                }
            }
        }
    }
/// Adds a suggestion to errors where an `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 an `impl Trait` is returned.
Adds a suggestion to errors where an ` impl Trait ` is returned .
fn add_static_impl_trait_suggestion(
        &self,
        diag: &mut DiagnosticBuilder<'tcx>,
        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::Opaque(did, substs)) = self
                .infcx
                .tcx
                .is_suitable_region(f)
                .map(|r| r.def_id)
                .and_then(|id| self.infcx.tcx.return_type_impl_trait(id))
                .map(|(ty, _)| ty.kind())
            {
                // 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 bounds = self.infcx.tcx.explicit_item_bounds(did);
                    let mut found = false;
                    for (bound, _) in bounds {
                        if let ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_, r)) =
                            bound.kind().skip_binder()
                        {
                            let r = r.subst(self.infcx.tcx, substs);
                            if let ty::RegionKind::ReStatic = r {
                                found = true;
                                break;
                            } else {
                                // If there's already a lifetime bound, don't
                                // suggest anything.
                                return;
                            }
                        }
                    }
                    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 = self.infcx.tcx.def_span(did);
                    if let Ok(snippet) = self.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()
                        };
                        let span = if snippet.ends_with(';') {
                            // `type X = impl Trait;`
                            span.with_hi(span.hi() - BytePos(1))
                        } else {
                            span
                        };
                        let suggestion = format!(" + {}", suggestable_fr_name);
                        let span = span.shrink_to_hi();
                        diag.span_suggestion(
                            span,
                            &format!(
                                "to allow this `impl Trait` to capture borrowed data with lifetime \
                                 `{}`, add `{}` as a bound",
                                fr_name, suggestable_fr_name,
                            ),
                            suggestion,
                            Applicability::MachineApplicable,
                        );
                    }
                }
            }
        }
    }
fn add_static_impl_trait_suggestion ( & self , diag : & mut DiagnosticBuilder < ' tcx > , 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 :: Opaque ( did , substs ) ) = self . infcx . tcx . is_suitable_region ( f ) . map ( | r | r . def_id ) . and_then ( | id | self . infcx . tcx . return_type_impl_trait ( id ) ) . map ( | ( ty , _ ) | ty . kind ( ) ) { // 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 bounds = self . infcx . tcx . explicit_item_bounds ( did ) ; let mut found = false ; for ( bound , _ ) in bounds { if let ty :: PredicateKind :: TypeOutlives ( ty :: OutlivesPredicate ( _ , r ) ) = bound . kind ( ) . skip_binder ( ) { let r = r . subst ( self . infcx . tcx , substs ) ; if let ty :: RegionKind :: ReStatic = r { found = true ; break ; } else { // If there's already a lifetime bound, don't // suggest anything. return ; } } } 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 = self . infcx . tcx . def_span ( did ) ; if let Ok ( snippet ) = self . 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 ( ) } ; let span = if snippet . ends_with ( ';' ) { // `type X = impl Trait;` span . with_hi ( span . hi ( ) - BytePos ( 1 ) ) } else { span } ; let suggestion = format ! ( " " suggestable_fr_name ) ; let span = span . shrink_to_hi ( ) ; diag . span_suggestion ( span , & format ! ( " \
 " fr_name suggestable_fr_name ) , suggestion , Applicability :: MachineApplicable , ) ; } } } } }
Adds a suggestion to errors where an `impl Trait` is returned.
Adds a suggestion to errors where an ` impl Trait ` is returned .
&self
diag
&mut DiagnosticBuilder<'tcx>
fr
RegionVid
fr_name
RegionName
outlived_fr
RegionVid
&self
diag
&mut DiagnosticBuilder<'tcx>
fr
RegionVid
fr_name
RegionName
outlived_fr
RegionVid