Skip to main content

rustc_trait_selection/error_reporting/infer/nice_region_error/
named_anon_conflict.rs

1//! Error Reporting for Anonymous Region Lifetime Errors
2//! where one region is named and the other is anonymous.
3
4use rustc_errors::Diag;
5use rustc_middle::ty;
6use rustc_middle::ty::RegionUtilitiesExt;
7use tracing::debug;
8
9use crate::diagnostics::ExplicitLifetimeRequired;
10use crate::error_reporting::infer::nice_region_error::NiceRegionError;
11use crate::error_reporting::infer::nice_region_error::find_anon_type::find_anon_type;
12
13impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
14    /// When given a `ConcreteFailure` for a function with parameters containing a named region and
15    /// an anonymous region, emit an descriptive diagnostic error.
16    pub(super) fn try_report_named_anon_conflict(&self) -> Option<Diag<'tcx>> {
17        let (span, sub, sup) = self.regions()?;
18
19        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs:19",
                        "rustc_trait_selection::error_reporting::infer::nice_region_error::named_anon_conflict",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs"),
                        ::tracing_core::__macro_support::Option::Some(19u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::nice_region_error::named_anon_conflict"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("try_report_named_anon_conflict(sub={0:?}, sup={1:?}, error={2:?})",
                                                    sub, sup, self.error) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!(
20            "try_report_named_anon_conflict(sub={:?}, sup={:?}, error={:?})",
21            sub, sup, self.error,
22        );
23
24        // Determine whether the sub and sup consist of one named region ('a)
25        // and one anonymous (elided) region. If so, find the parameter arg
26        // where the anonymous region appears (there must always be one; we
27        // only introduced anonymous regions in parameters) as well as a
28        // version new_ty of its type where the anonymous region is replaced
29        // with the named one.
30        let (named, anon, anon_param_info, region_info) = if sub.is_named(self.tcx())
31            && let Some(region_info) = self.tcx().is_suitable_region(self.generic_param_scope, sup)
32            && let Some(anon_param_info) = self.find_param_with_region(sup, sub)
33        {
34            (sub, sup, anon_param_info, region_info)
35        } else if sup.is_named(self.tcx())
36            && let Some(region_info) = self.tcx().is_suitable_region(self.generic_param_scope, sub)
37            && let Some(anon_param_info) = self.find_param_with_region(sub, sup)
38        {
39            (sup, sub, anon_param_info, region_info)
40        } else {
41            return None; // inapplicable
42        };
43
44        // Suggesting to add a `'static` lifetime to a parameter is nearly always incorrect,
45        // and can steer users down the wrong path.
46        if named.is_static() {
47            return None;
48        }
49
50        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs:50",
                        "rustc_trait_selection::error_reporting::infer::nice_region_error::named_anon_conflict",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs"),
                        ::tracing_core::__macro_support::Option::Some(50u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::nice_region_error::named_anon_conflict"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("try_report_named_anon_conflict: named = {0:?}",
                                                    named) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("try_report_named_anon_conflict: named = {:?}", named);
51        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs:51",
                        "rustc_trait_selection::error_reporting::infer::nice_region_error::named_anon_conflict",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs"),
                        ::tracing_core::__macro_support::Option::Some(51u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::nice_region_error::named_anon_conflict"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("try_report_named_anon_conflict: anon_param_info = {0:?}",
                                                    anon_param_info) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("try_report_named_anon_conflict: anon_param_info = {:?}", anon_param_info);
52        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs:52",
                        "rustc_trait_selection::error_reporting::infer::nice_region_error::named_anon_conflict",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs"),
                        ::tracing_core::__macro_support::Option::Some(52u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::nice_region_error::named_anon_conflict"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("try_report_named_anon_conflict: region_info = {0:?}",
                                                    region_info) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("try_report_named_anon_conflict: region_info = {:?}", region_info);
53
54        let param = anon_param_info.param;
55        let new_ty = anon_param_info.param_ty;
56        let new_ty_span = anon_param_info.param_ty_span;
57        let is_first = anon_param_info.is_first;
58        let scope_def_id = region_info.scope;
59        let is_impl_item = region_info.is_impl_item;
60
61        if anon_param_info.kind.is_named(self.tcx()) {
62            /* not an anonymous region */
63            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs:63",
                        "rustc_trait_selection::error_reporting::infer::nice_region_error::named_anon_conflict",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs"),
                        ::tracing_core::__macro_support::Option::Some(63u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::nice_region_error::named_anon_conflict"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("try_report_named_anon_conflict: not an anonymous region")
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("try_report_named_anon_conflict: not an anonymous region");
64            return None;
65        }
66
67        if is_impl_item {
68            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs:68",
                        "rustc_trait_selection::error_reporting::infer::nice_region_error::named_anon_conflict",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/named_anon_conflict.rs"),
                        ::tracing_core::__macro_support::Option::Some(68u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::nice_region_error::named_anon_conflict"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                __CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("try_report_named_anon_conflict: impl item, bail out")
                                            as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("try_report_named_anon_conflict: impl item, bail out");
69            return None;
70        }
71
72        if find_anon_type(self.tcx(), self.generic_param_scope, anon).is_some()
73            && self.is_self_anon(is_first, scope_def_id)
74        {
75            return None;
76        }
77        let orig_ty = anon_param_info.orig_param_ty;
78        // Don't suggest naming the outer lifetime when it already appears in the pointee
79        // (e.g. `&'a mut Buffer<'a>`); point at borrow splitting instead.
80        let suggestion_would_alias_lifetime =
81            if let ty::Ref(_, orig_inner_ty, ty::Mutability::Mut) = orig_ty.kind() {
82                self.tcx().any_free_region_meets(orig_inner_ty, |r| r == named)
83            } else {
84                false
85            };
86
87        let named = named.to_string();
88        let new_ty_span = if suggestion_would_alias_lifetime { None } else { Some(new_ty_span) };
89        let err = match param.pat.simple_ident() {
90            Some(simple_ident) => ExplicitLifetimeRequired::WithIdent {
91                span,
92                simple_ident,
93                named,
94                new_ty_span,
95                new_ty,
96                link_nomicon: suggestion_would_alias_lifetime,
97            },
98            None => ExplicitLifetimeRequired::WithParamType {
99                span,
100                named,
101                new_ty_span,
102                new_ty,
103                link_nomicon: suggestion_would_alias_lifetime,
104            },
105        };
106        Some(self.tcx().sess.dcx().create_err(err))
107    }
108}