]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/errors/note_and_explain.rs
Rollup merge of #102454 - chenyukang:fix-102396-missing-parentheses, r=lcnr
[rust.git] / compiler / rustc_infer / src / errors / note_and_explain.rs
1 use crate::infer::error_reporting::nice_region_error::find_anon_type;
2 use rustc_errors::{
3     self, fluent, AddToDiagnostic, Diagnostic, IntoDiagnosticArg, SubdiagnosticMessage,
4 };
5 use rustc_middle::ty::{self, TyCtxt};
6 use rustc_span::{symbol::kw, Span};
7
8 #[derive(Default)]
9 struct DescriptionCtx<'a> {
10     span: Option<Span>,
11     kind: &'a str,
12     arg: String,
13     num_arg: u32,
14 }
15
16 impl<'a> DescriptionCtx<'a> {
17     fn new<'tcx>(
18         tcx: TyCtxt<'tcx>,
19         region: ty::Region<'tcx>,
20         alt_span: Option<Span>,
21     ) -> Option<Self> {
22         let mut me = DescriptionCtx::default();
23         me.span = alt_span;
24         match *region {
25             ty::ReEarlyBound(_) | ty::ReFree(_) => {
26                 return Self::from_early_bound_and_free_regions(tcx, region);
27             }
28             ty::ReStatic => {
29                 me.kind = "restatic";
30             }
31
32             ty::RePlaceholder(_) => return None,
33
34             // FIXME(#13998) RePlaceholder should probably print like
35             // ReFree rather than dumping Debug output on the user.
36             //
37             // We shouldn't really be having unification failures with ReVar
38             // and ReLateBound though.
39             ty::ReVar(_) | ty::ReLateBound(..) | ty::ReErased => {
40                 me.kind = "revar";
41                 me.arg = format!("{:?}", region);
42             }
43         };
44         Some(me)
45     }
46
47     fn from_early_bound_and_free_regions<'tcx>(
48         tcx: TyCtxt<'tcx>,
49         region: ty::Region<'tcx>,
50     ) -> Option<Self> {
51         let mut me = DescriptionCtx::default();
52         let scope = region.free_region_binding_scope(tcx).expect_local();
53         match *region {
54             ty::ReEarlyBound(ref br) => {
55                 let mut sp = tcx.def_span(scope);
56                 if let Some(param) =
57                     tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
58                 {
59                     sp = param.span;
60                 }
61                 if br.has_name() {
62                     me.kind = "as_defined";
63                     me.arg = br.name.to_string();
64                 } else {
65                     me.kind = "as_defined_anon";
66                 };
67                 me.span = Some(sp)
68             }
69             ty::ReFree(ref fr) => {
70                 if !fr.bound_region.is_named()
71                     && let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region)
72                 {
73                     me.kind = "defined_here";
74                     me.span = Some(ty.span);
75                 } else {
76                     match fr.bound_region {
77                         ty::BoundRegionKind::BrNamed(_, name) => {
78                             let mut sp = tcx.def_span(scope);
79                             if let Some(param) =
80                                 tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
81                             {
82                                 sp = param.span;
83                             }
84                             if name == kw::UnderscoreLifetime {
85                                 me.kind = "as_defined_anon";
86                             } else {
87                                 me.kind = "as_defined";
88                                 me.arg = name.to_string();
89                             };
90                             me.span = Some(sp);
91                         }
92                         ty::BrAnon(idx) => {
93                             me.kind = "anon_num_here";
94                             me.num_arg = idx+1;
95                             me.span = Some(tcx.def_span(scope));
96                         },
97                         _ => {
98                             me.kind = "defined_here_reg";
99                             me.arg = region.to_string();
100                             me.span = Some(tcx.def_span(scope));
101                         },
102                     }
103                 }
104             }
105             _ => bug!(),
106         }
107         Some(me)
108     }
109
110     fn add_to(self, diag: &mut rustc_errors::Diagnostic) {
111         diag.set_arg("desc_kind", self.kind);
112         diag.set_arg("desc_arg", self.arg);
113         diag.set_arg("desc_num_arg", self.num_arg);
114     }
115 }
116
117 pub enum PrefixKind {
118     Empty,
119 }
120
121 pub enum SuffixKind {
122     Continues,
123 }
124
125 impl IntoDiagnosticArg for PrefixKind {
126     fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
127         let kind = match self {
128             Self::Empty => "empty",
129         }
130         .into();
131         rustc_errors::DiagnosticArgValue::Str(kind)
132     }
133 }
134
135 impl IntoDiagnosticArg for SuffixKind {
136     fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
137         let kind = match self {
138             Self::Continues => "continues",
139         }
140         .into();
141         rustc_errors::DiagnosticArgValue::Str(kind)
142     }
143 }
144
145 pub struct RegionExplanation<'a> {
146     desc: DescriptionCtx<'a>,
147     prefix: PrefixKind,
148     suffix: SuffixKind,
149 }
150
151 impl RegionExplanation<'_> {
152     pub fn new<'tcx>(
153         tcx: TyCtxt<'tcx>,
154         region: ty::Region<'tcx>,
155         alt_span: Option<Span>,
156         prefix: PrefixKind,
157         suffix: SuffixKind,
158     ) -> Option<Self> {
159         Some(Self { desc: DescriptionCtx::new(tcx, region, alt_span)?, prefix, suffix })
160     }
161 }
162
163 impl AddToDiagnostic for RegionExplanation<'_> {
164     fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
165     where
166         F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
167     {
168         if let Some(span) = self.desc.span {
169             diag.span_note(span, fluent::infer::region_explanation);
170         } else {
171             diag.note(fluent::infer::region_explanation);
172         }
173         self.desc.add_to(diag);
174         diag.set_arg("pref_kind", self.prefix);
175         diag.set_arg("suff_kind", self.suffix);
176     }
177 }