]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/infer/error_reporting/nice_region_error/find_anon_type.rs
Do not suggest using a const parameter when there are bounds on an unused type parameter
[rust.git] / compiler / rustc_infer / src / infer / error_reporting / nice_region_error / find_anon_type.rs
1 use rustc_hir as hir;
2 use rustc_hir::intravisit::{self, Visitor};
3 use rustc_middle::hir::map::Map;
4 use rustc_middle::hir::nested_filter;
5 use rustc_middle::middle::resolve_lifetime as rl;
6 use rustc_middle::ty::{self, Region, TyCtxt};
7
8 /// This function calls the `visit_ty` method for the parameters
9 /// corresponding to the anonymous regions. The `nested_visitor.found_type`
10 /// contains the anonymous type.
11 ///
12 /// # Arguments
13 /// region - the anonymous region corresponding to the anon_anon conflict
14 /// br - the bound region corresponding to the above region which is of type `BrAnon(_)`
15 ///
16 /// # Example
17 /// ```
18 /// fn foo(x: &mut Vec<&u8>, y: &u8)
19 ///    { x.push(y); }
20 /// ```
21 /// The function returns the nested type corresponding to the anonymous region
22 /// for e.g., `&u8` and `Vec<&u8>`.
23 pub(crate) fn find_anon_type<'tcx>(
24     tcx: TyCtxt<'tcx>,
25     region: Region<'tcx>,
26     br: &ty::BoundRegionKind,
27 ) -> Option<(&'tcx hir::Ty<'tcx>, &'tcx hir::FnSig<'tcx>)> {
28     if let Some(anon_reg) = tcx.is_suitable_region(region) {
29         let hir_id = tcx.hir().local_def_id_to_hir_id(anon_reg.def_id);
30         let Some(fn_sig) = tcx.hir().get(hir_id).fn_sig() else {
31             return None
32         };
33
34         fn_sig
35             .decl
36             .inputs
37             .iter()
38             .find_map(|arg| find_component_for_bound_region(tcx, arg, br))
39             .map(|ty| (ty, fn_sig))
40     } else {
41         None
42     }
43 }
44
45 // This method creates a FindNestedTypeVisitor which returns the type corresponding
46 // to the anonymous region.
47 fn find_component_for_bound_region<'tcx>(
48     tcx: TyCtxt<'tcx>,
49     arg: &'tcx hir::Ty<'tcx>,
50     br: &ty::BoundRegionKind,
51 ) -> Option<&'tcx hir::Ty<'tcx>> {
52     let mut nested_visitor = FindNestedTypeVisitor {
53         tcx,
54         bound_region: *br,
55         found_type: None,
56         current_index: ty::INNERMOST,
57     };
58     nested_visitor.visit_ty(arg);
59     nested_visitor.found_type
60 }
61
62 // The FindNestedTypeVisitor captures the corresponding `hir::Ty` of the
63 // anonymous region. The example above would lead to a conflict between
64 // the two anonymous lifetimes for &u8 in x and y respectively. This visitor
65 // would be invoked twice, once for each lifetime, and would
66 // walk the types like &mut Vec<&u8> and &u8 looking for the HIR
67 // where that lifetime appears. This allows us to highlight the
68 // specific part of the type in the error message.
69 struct FindNestedTypeVisitor<'tcx> {
70     tcx: TyCtxt<'tcx>,
71     // The bound_region corresponding to the Refree(freeregion)
72     // associated with the anonymous region we are looking for.
73     bound_region: ty::BoundRegionKind,
74     // The type where the anonymous lifetime appears
75     // for e.g., Vec<`&u8`> and <`&u8`>
76     found_type: Option<&'tcx hir::Ty<'tcx>>,
77     current_index: ty::DebruijnIndex,
78 }
79
80 impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
81     type NestedFilter = nested_filter::OnlyBodies;
82
83     fn nested_visit_map(&mut self) -> Self::Map {
84         self.tcx.hir()
85     }
86
87     fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
88         match arg.kind {
89             hir::TyKind::BareFn(_) => {
90                 self.current_index.shift_in(1);
91                 intravisit::walk_ty(self, arg);
92                 self.current_index.shift_out(1);
93                 return;
94             }
95
96             hir::TyKind::TraitObject(bounds, ..) => {
97                 for bound in bounds {
98                     self.current_index.shift_in(1);
99                     self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
100                     self.current_index.shift_out(1);
101                 }
102             }
103
104             hir::TyKind::Rptr(ref lifetime, _) => {
105                 // the lifetime of the TyRptr
106                 let hir_id = lifetime.hir_id;
107                 match (self.tcx.named_region(hir_id), self.bound_region) {
108                     // Find the index of the anonymous region that was part of the
109                     // error. We will then search the function parameters for a bound
110                     // region at the right depth with the same index
111                     (
112                         Some(rl::Region::LateBoundAnon(debruijn_index, _, anon_index)),
113                         ty::BrAnon(br_index),
114                     ) => {
115                         debug!(
116                             "LateBoundAnon depth = {:?} anon_index = {:?} br_index={:?}",
117                             debruijn_index, anon_index, br_index
118                         );
119                         if debruijn_index == self.current_index && anon_index == br_index {
120                             self.found_type = Some(arg);
121                             return; // we can stop visiting now
122                         }
123                     }
124
125                     // Find the index of the named region that was part of the
126                     // error. We will then search the function parameters for a bound
127                     // region at the right depth with the same index
128                     (Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
129                         debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
130                         if id == def_id {
131                             self.found_type = Some(arg);
132                             return; // we can stop visiting now
133                         }
134                     }
135
136                     // Find the index of the named region that was part of the
137                     // error. We will then search the function parameters for a bound
138                     // region at the right depth with the same index
139                     (
140                         Some(rl::Region::LateBound(debruijn_index, _, id, _)),
141                         ty::BrNamed(def_id, _),
142                     ) => {
143                         debug!(
144                             "FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}",
145                             debruijn_index
146                         );
147                         debug!("LateBound id={:?} def_id={:?}", id, def_id);
148                         if debruijn_index == self.current_index && id == def_id {
149                             self.found_type = Some(arg);
150                             return; // we can stop visiting now
151                         }
152                     }
153
154                     (
155                         Some(
156                             rl::Region::Static
157                             | rl::Region::Free(_, _)
158                             | rl::Region::EarlyBound(_, _, _)
159                             | rl::Region::LateBound(_, _, _, _)
160                             | rl::Region::LateBoundAnon(_, _, _),
161                         )
162                         | None,
163                         _,
164                     ) => {
165                         debug!("no arg found");
166                     }
167                 }
168             }
169             // Checks if it is of type `hir::TyKind::Path` which corresponds to a struct.
170             hir::TyKind::Path(_) => {
171                 let subvisitor = &mut TyPathVisitor {
172                     tcx: self.tcx,
173                     found_it: false,
174                     bound_region: self.bound_region,
175                     current_index: self.current_index,
176                 };
177                 intravisit::walk_ty(subvisitor, arg); // call walk_ty; as visit_ty is empty,
178                 // this will visit only outermost type
179                 if subvisitor.found_it {
180                     self.found_type = Some(arg);
181                 }
182             }
183             _ => {}
184         }
185         // walk the embedded contents: e.g., if we are visiting `Vec<&Foo>`,
186         // go on to visit `&Foo`
187         intravisit::walk_ty(self, arg);
188     }
189 }
190
191 // The visitor captures the corresponding `hir::Ty` of the anonymous region
192 // in the case of structs ie. `hir::TyKind::Path`.
193 // This visitor would be invoked for each lifetime corresponding to a struct,
194 // and would walk the types like Vec<Ref> in the above example and Ref looking for the HIR
195 // where that lifetime appears. This allows us to highlight the
196 // specific part of the type in the error message.
197 struct TyPathVisitor<'tcx> {
198     tcx: TyCtxt<'tcx>,
199     found_it: bool,
200     bound_region: ty::BoundRegionKind,
201     current_index: ty::DebruijnIndex,
202 }
203
204 impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
205     type NestedFilter = nested_filter::OnlyBodies;
206
207     fn nested_visit_map(&mut self) -> Map<'tcx> {
208         self.tcx.hir()
209     }
210
211     fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
212         match (self.tcx.named_region(lifetime.hir_id), self.bound_region) {
213             // the lifetime of the TyPath!
214             (
215                 Some(rl::Region::LateBoundAnon(debruijn_index, _, anon_index)),
216                 ty::BrAnon(br_index),
217             ) => {
218                 if debruijn_index == self.current_index && anon_index == br_index {
219                     self.found_it = true;
220                     return;
221                 }
222             }
223
224             (Some(rl::Region::EarlyBound(_, id, _)), ty::BrNamed(def_id, _)) => {
225                 debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
226                 if id == def_id {
227                     self.found_it = true;
228                     return; // we can stop visiting now
229                 }
230             }
231
232             (Some(rl::Region::LateBound(debruijn_index, _, id, _)), ty::BrNamed(def_id, _)) => {
233                 debug!("FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}", debruijn_index,);
234                 debug!("id={:?}", id);
235                 debug!("def_id={:?}", def_id);
236                 if debruijn_index == self.current_index && id == def_id {
237                     self.found_it = true;
238                     return; // we can stop visiting now
239                 }
240             }
241
242             (
243                 Some(
244                     rl::Region::Static
245                     | rl::Region::EarlyBound(_, _, _)
246                     | rl::Region::LateBound(_, _, _, _)
247                     | rl::Region::LateBoundAnon(_, _, _)
248                     | rl::Region::Free(_, _),
249                 )
250                 | None,
251                 _,
252             ) => {
253                 debug!("no arg found");
254             }
255         }
256     }
257
258     fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
259         // ignore nested types
260         //
261         // If you have a type like `Foo<'a, &Ty>` we
262         // are only interested in the immediate lifetimes ('a).
263         //
264         // Making `visit_ty` empty will ignore the `&Ty` embedded
265         // inside, it will get reached by the outer visitor.
266         debug!("`Ty` corresponding to a struct is {:?}", arg);
267     }
268 }