rustc_trait_selection/errors/
note_and_explain.rs

1use rustc_errors::{Diag, EmissionGuarantee, IntoDiagArg, SubdiagMessageOp, Subdiagnostic};
2use rustc_hir::def_id::LocalDefId;
3use rustc_middle::bug;
4use rustc_middle::ty::{self, TyCtxt};
5use rustc_span::{Span, kw};
6
7use crate::error_reporting::infer::nice_region_error::find_anon_type;
8use crate::fluent_generated as fluent;
9
10struct DescriptionCtx<'a> {
11    span: Option<Span>,
12    kind: &'a str,
13    arg: String,
14}
15
16impl<'a> DescriptionCtx<'a> {
17    fn new<'tcx>(
18        tcx: TyCtxt<'tcx>,
19        generic_param_scope: LocalDefId,
20        region: ty::Region<'tcx>,
21        alt_span: Option<Span>,
22    ) -> Option<Self> {
23        let (span, kind, arg) = match *region {
24            ty::ReEarlyParam(br) => {
25                let scope = tcx
26                    .parent(tcx.generics_of(generic_param_scope).region_param(br, tcx).def_id)
27                    .expect_local();
28                let span = if let Some(param) =
29                    tcx.hir_get_generics(scope).and_then(|generics| generics.get_named(br.name))
30                {
31                    param.span
32                } else {
33                    tcx.def_span(scope)
34                };
35                if br.has_name() {
36                    (Some(span), "as_defined", br.name.to_string())
37                } else {
38                    (Some(span), "as_defined_anon", String::new())
39                }
40            }
41            ty::ReLateParam(ref fr) => {
42                if !fr.kind.is_named()
43                    && let Some((ty, _)) = find_anon_type(tcx, generic_param_scope, region)
44                {
45                    (Some(ty.span), "defined_here", String::new())
46                } else {
47                    let scope = fr.scope.expect_local();
48                    match fr.kind {
49                        ty::LateParamRegionKind::Named(_, name) => {
50                            let span = if let Some(param) = tcx
51                                .hir_get_generics(scope)
52                                .and_then(|generics| generics.get_named(name))
53                            {
54                                param.span
55                            } else {
56                                tcx.def_span(scope)
57                            };
58                            if name == kw::UnderscoreLifetime {
59                                (Some(span), "as_defined_anon", String::new())
60                            } else {
61                                (Some(span), "as_defined", name.to_string())
62                            }
63                        }
64                        ty::LateParamRegionKind::Anon(_) => {
65                            let span = Some(tcx.def_span(scope));
66                            (span, "defined_here", String::new())
67                        }
68                        _ => (Some(tcx.def_span(scope)), "defined_here_reg", region.to_string()),
69                    }
70                }
71            }
72
73            ty::ReStatic => (alt_span, "restatic", String::new()),
74
75            ty::RePlaceholder(_) | ty::ReError(_) => return None,
76
77            ty::ReVar(_) | ty::ReBound(..) | ty::ReErased => {
78                bug!("unexpected region for DescriptionCtx: {:?}", region);
79            }
80        };
81        Some(DescriptionCtx { span, kind, arg })
82    }
83}
84
85pub enum PrefixKind {
86    Empty,
87    RefValidFor,
88    ContentValidFor,
89    TypeObjValidFor,
90    SourcePointerValidFor,
91    TypeSatisfy,
92    TypeOutlive,
93    LfParamInstantiatedWith,
94    LfParamMustOutlive,
95    LfInstantiatedWith,
96    LfMustOutlive,
97    PointerValidFor,
98    DataValidFor,
99}
100
101pub enum SuffixKind {
102    Empty,
103    Continues,
104    ReqByBinding,
105}
106
107impl IntoDiagArg for PrefixKind {
108    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
109        let kind = match self {
110            Self::Empty => "empty",
111            Self::RefValidFor => "ref_valid_for",
112            Self::ContentValidFor => "content_valid_for",
113            Self::TypeObjValidFor => "type_obj_valid_for",
114            Self::SourcePointerValidFor => "source_pointer_valid_for",
115            Self::TypeSatisfy => "type_satisfy",
116            Self::TypeOutlive => "type_outlive",
117            Self::LfParamInstantiatedWith => "lf_param_instantiated_with",
118            Self::LfParamMustOutlive => "lf_param_must_outlive",
119            Self::LfInstantiatedWith => "lf_instantiated_with",
120            Self::LfMustOutlive => "lf_must_outlive",
121            Self::PointerValidFor => "pointer_valid_for",
122            Self::DataValidFor => "data_valid_for",
123        }
124        .into();
125        rustc_errors::DiagArgValue::Str(kind)
126    }
127}
128
129impl IntoDiagArg for SuffixKind {
130    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
131        let kind = match self {
132            Self::Empty => "empty",
133            Self::Continues => "continues",
134            Self::ReqByBinding => "req_by_binding",
135        }
136        .into();
137        rustc_errors::DiagArgValue::Str(kind)
138    }
139}
140
141pub struct RegionExplanation<'a> {
142    desc: DescriptionCtx<'a>,
143    prefix: PrefixKind,
144    suffix: SuffixKind,
145}
146
147impl RegionExplanation<'_> {
148    pub fn new<'tcx>(
149        tcx: TyCtxt<'tcx>,
150        generic_param_scope: LocalDefId,
151        region: ty::Region<'tcx>,
152        alt_span: Option<Span>,
153        prefix: PrefixKind,
154        suffix: SuffixKind,
155    ) -> Option<Self> {
156        Some(Self {
157            desc: DescriptionCtx::new(tcx, generic_param_scope, region, alt_span)?,
158            prefix,
159            suffix,
160        })
161    }
162}
163
164impl Subdiagnostic for RegionExplanation<'_> {
165    fn add_to_diag_with<G: EmissionGuarantee, F: SubdiagMessageOp<G>>(
166        self,
167        diag: &mut Diag<'_, G>,
168        f: &F,
169    ) {
170        diag.arg("pref_kind", self.prefix);
171        diag.arg("suff_kind", self.suffix);
172        diag.arg("desc_kind", self.desc.kind);
173        diag.arg("desc_arg", self.desc.arg);
174
175        let msg = f(diag, fluent::trait_selection_region_explanation.into());
176        if let Some(span) = self.desc.span {
177            diag.span_note(span, msg);
178        } else {
179            diag.note(msg);
180        }
181    }
182}