]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/errors/note_and_explain.rs
Auto merge of #106235 - compiler-errors:rework-bounds-collection, r=davidtwco
[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, span) => {
93                             me.kind = "anon_num_here";
94                             me.num_arg = idx+1;
95                             me.span = match span {
96                                 Some(_) => span,
97                                 None => Some(tcx.def_span(scope)),
98                             }
99                         },
100                         _ => {
101                             me.kind = "defined_here_reg";
102                             me.arg = region.to_string();
103                             me.span = Some(tcx.def_span(scope));
104                         },
105                     }
106                 }
107             }
108             _ => bug!(),
109         }
110         Some(me)
111     }
112
113     fn add_to(self, diag: &mut rustc_errors::Diagnostic) {
114         diag.set_arg("desc_kind", self.kind);
115         diag.set_arg("desc_arg", self.arg);
116         diag.set_arg("desc_num_arg", self.num_arg);
117     }
118 }
119
120 pub enum PrefixKind {
121     Empty,
122 }
123
124 pub enum SuffixKind {
125     Continues,
126 }
127
128 impl IntoDiagnosticArg for PrefixKind {
129     fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
130         let kind = match self {
131             Self::Empty => "empty",
132         }
133         .into();
134         rustc_errors::DiagnosticArgValue::Str(kind)
135     }
136 }
137
138 impl IntoDiagnosticArg for SuffixKind {
139     fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> {
140         let kind = match self {
141             Self::Continues => "continues",
142         }
143         .into();
144         rustc_errors::DiagnosticArgValue::Str(kind)
145     }
146 }
147
148 pub struct RegionExplanation<'a> {
149     desc: DescriptionCtx<'a>,
150     prefix: PrefixKind,
151     suffix: SuffixKind,
152 }
153
154 impl RegionExplanation<'_> {
155     pub fn new<'tcx>(
156         tcx: TyCtxt<'tcx>,
157         region: ty::Region<'tcx>,
158         alt_span: Option<Span>,
159         prefix: PrefixKind,
160         suffix: SuffixKind,
161     ) -> Option<Self> {
162         Some(Self { desc: DescriptionCtx::new(tcx, region, alt_span)?, prefix, suffix })
163     }
164 }
165
166 impl AddToDiagnostic for RegionExplanation<'_> {
167     fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
168     where
169         F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
170     {
171         if let Some(span) = self.desc.span {
172             diag.span_note(span, fluent::infer_region_explanation);
173         } else {
174             diag.note(fluent::infer_region_explanation);
175         }
176         self.desc.add_to(diag);
177         diag.set_arg("pref_kind", self.prefix);
178         diag.set_arg("suff_kind", self.suffix);
179     }
180 }