]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/diagnostics/var_name.rs
Rollup merge of #105172 - alexs-sh:issue-98861-fix-next, r=scottmcm
[rust.git] / compiler / rustc_borrowck / src / diagnostics / var_name.rs
1 #![deny(rustc::untranslatable_diagnostic)]
2 #![deny(rustc::diagnostic_outside_of_impl)]
3
4 use crate::Upvar;
5 use crate::{nll::ToRegionVid, region_infer::RegionInferenceContext};
6 use rustc_index::vec::{Idx, IndexVec};
7 use rustc_middle::mir::{Body, Local};
8 use rustc_middle::ty::{RegionVid, TyCtxt};
9 use rustc_span::source_map::Span;
10 use rustc_span::symbol::Symbol;
11
12 impl<'tcx> RegionInferenceContext<'tcx> {
13     pub(crate) fn get_var_name_and_span_for_region(
14         &self,
15         tcx: TyCtxt<'tcx>,
16         body: &Body<'tcx>,
17         local_names: &IndexVec<Local, Option<Symbol>>,
18         upvars: &[Upvar<'tcx>],
19         fr: RegionVid,
20     ) -> Option<(Option<Symbol>, Span)> {
21         debug!("get_var_name_and_span_for_region(fr={fr:?})");
22         assert!(self.universal_regions().is_universal_region(fr));
23
24         debug!("get_var_name_and_span_for_region: attempting upvar");
25         self.get_upvar_index_for_region(tcx, fr)
26             .map(|index| {
27                 // FIXME(project-rfc-2229#8): Use place span for diagnostics
28                 let (name, span) = self.get_upvar_name_and_span_for_region(tcx, upvars, index);
29                 (Some(name), span)
30             })
31             .or_else(|| {
32                 debug!("get_var_name_and_span_for_region: attempting argument");
33                 self.get_argument_index_for_region(tcx, fr).map(|index| {
34                     self.get_argument_name_and_span_for_region(body, local_names, index)
35                 })
36             })
37     }
38
39     /// Search the upvars (if any) to find one that references fr. Return its index.
40     pub(crate) fn get_upvar_index_for_region(
41         &self,
42         tcx: TyCtxt<'tcx>,
43         fr: RegionVid,
44     ) -> Option<usize> {
45         let upvar_index =
46             self.universal_regions().defining_ty.upvar_tys().position(|upvar_ty| {
47                 debug!("get_upvar_index_for_region: upvar_ty={upvar_ty:?}");
48                 tcx.any_free_region_meets(&upvar_ty, |r| {
49                     let r = r.to_region_vid();
50                     debug!("get_upvar_index_for_region: r={r:?} fr={fr:?}");
51                     r == fr
52                 })
53             })?;
54
55         let upvar_ty = self.universal_regions().defining_ty.upvar_tys().nth(upvar_index);
56
57         debug!(
58             "get_upvar_index_for_region: found {fr:?} in upvar {upvar_index} which has type {upvar_ty:?}",
59         );
60
61         Some(upvar_index)
62     }
63
64     /// Given the index of an upvar, finds its name and the span from where it was
65     /// declared.
66     pub(crate) fn get_upvar_name_and_span_for_region(
67         &self,
68         tcx: TyCtxt<'tcx>,
69         upvars: &[Upvar<'tcx>],
70         upvar_index: usize,
71     ) -> (Symbol, Span) {
72         let upvar_hir_id = upvars[upvar_index].place.get_root_variable();
73         debug!("get_upvar_name_and_span_for_region: upvar_hir_id={upvar_hir_id:?}");
74
75         let upvar_name = tcx.hir().name(upvar_hir_id);
76         let upvar_span = tcx.hir().span(upvar_hir_id);
77         debug!(
78             "get_upvar_name_and_span_for_region: upvar_name={upvar_name:?} upvar_span={upvar_span:?}",
79         );
80
81         (upvar_name, upvar_span)
82     }
83
84     /// Search the argument types for one that references fr (which should be a free region).
85     /// Returns Some(_) with the index of the input if one is found.
86     ///
87     /// N.B., in the case of a closure, the index is indexing into the signature as seen by the
88     /// user - in particular, index 0 is not the implicit self parameter.
89     pub(crate) fn get_argument_index_for_region(
90         &self,
91         tcx: TyCtxt<'tcx>,
92         fr: RegionVid,
93     ) -> Option<usize> {
94         let implicit_inputs = self.universal_regions().defining_ty.implicit_inputs();
95         let argument_index =
96             self.universal_regions().unnormalized_input_tys.iter().skip(implicit_inputs).position(
97                 |arg_ty| {
98                     debug!("get_argument_index_for_region: arg_ty = {arg_ty:?}");
99                     tcx.any_free_region_meets(arg_ty, |r| r.to_region_vid() == fr)
100                 },
101             )?;
102
103         debug!(
104             "get_argument_index_for_region: found {fr:?} in argument {argument_index} which has type {:?}",
105             self.universal_regions().unnormalized_input_tys[argument_index],
106         );
107
108         Some(argument_index)
109     }
110
111     /// Given the index of an argument, finds its name (if any) and the span from where it was
112     /// declared.
113     pub(crate) fn get_argument_name_and_span_for_region(
114         &self,
115         body: &Body<'tcx>,
116         local_names: &IndexVec<Local, Option<Symbol>>,
117         argument_index: usize,
118     ) -> (Option<Symbol>, Span) {
119         let implicit_inputs = self.universal_regions().defining_ty.implicit_inputs();
120         let argument_local = Local::new(implicit_inputs + argument_index + 1);
121         debug!("get_argument_name_and_span_for_region: argument_local={argument_local:?}");
122
123         let argument_name = local_names[argument_local];
124         let argument_span = body.local_decls[argument_local].source_info.span;
125         debug!(
126             "get_argument_name_and_span_for_region: argument_name={argument_name:?} argument_span={argument_span:?}",
127         );
128
129         (argument_name, argument_span)
130     }
131 }