]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_borrowck/src/diagnostics/region_name.rs
Auto merge of #105145 - Ayush1325:sequential-remote-server, r=Mark-Simulacrum
[rust.git] / compiler / rustc_borrowck / src / diagnostics / region_name.rs
1 use std::fmt::{self, Display};
2 use std::iter;
3
4 use rustc_errors::Diagnostic;
5 use rustc_hir as hir;
6 use rustc_hir::def::{DefKind, Res};
7 use rustc_middle::ty::print::RegionHighlightMode;
8 use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
9 use rustc_middle::ty::{self, DefIdTree, RegionVid, Ty};
10 use rustc_span::symbol::{kw, sym, Ident, Symbol};
11 use rustc_span::{Span, DUMMY_SP};
12
13 use crate::{nll::ToRegionVid, universal_regions::DefiningTy, MirBorrowckCtxt};
14
15 /// A name for a particular region used in emitting diagnostics. This name could be a generated
16 /// name like `'1`, a name used by the user like `'a`, or a name like `'static`.
17 #[derive(Debug, Clone)]
18 pub(crate) struct RegionName {
19     /// The name of the region (interned).
20     pub(crate) name: Symbol,
21     /// Where the region comes from.
22     pub(crate) source: RegionNameSource,
23 }
24
25 /// Denotes the source of a region that is named by a `RegionName`. For example, a free region that
26 /// was named by the user would get `NamedFreeRegion` and `'static` lifetime would get `Static`.
27 /// This helps to print the right kinds of diagnostics.
28 #[derive(Debug, Clone)]
29 pub(crate) enum RegionNameSource {
30     /// A bound (not free) region that was substituted at the def site (not an HRTB).
31     NamedEarlyBoundRegion(Span),
32     /// A free region that the user has a name (`'a`) for.
33     NamedFreeRegion(Span),
34     /// The `'static` region.
35     Static,
36     /// The free region corresponding to the environment of a closure.
37     SynthesizedFreeEnvRegion(Span, &'static str),
38     /// The region corresponding to an argument.
39     AnonRegionFromArgument(RegionNameHighlight),
40     /// The region corresponding to a closure upvar.
41     AnonRegionFromUpvar(Span, Symbol),
42     /// The region corresponding to the return type of a closure.
43     AnonRegionFromOutput(RegionNameHighlight, &'static str),
44     /// The region from a type yielded by a generator.
45     AnonRegionFromYieldTy(Span, String),
46     /// An anonymous region from an async fn.
47     AnonRegionFromAsyncFn(Span),
48     /// An anonymous region from an impl self type or trait
49     AnonRegionFromImplSignature(Span, &'static str),
50 }
51
52 /// Describes what to highlight to explain to the user that we're giving an anonymous region a
53 /// synthesized name, and how to highlight it.
54 #[derive(Debug, Clone)]
55 pub(crate) enum RegionNameHighlight {
56     /// The anonymous region corresponds to a reference that was found by traversing the type in the HIR.
57     MatchedHirTy(Span),
58     /// The anonymous region corresponds to a `'_` in the generics list of a struct/enum/union.
59     MatchedAdtAndSegment(Span),
60     /// The anonymous region corresponds to a region where the type annotation is completely missing
61     /// from the code, e.g. in a closure arguments `|x| { ... }`, where `x` is a reference.
62     CannotMatchHirTy(Span, String),
63     /// The anonymous region corresponds to a region where the type annotation is completely missing
64     /// from the code, and *even if* we print out the full name of the type, the region name won't
65     /// be included. This currently occurs for opaque types like `impl Future`.
66     Occluded(Span, String),
67 }
68
69 impl RegionName {
70     pub(crate) fn was_named(&self) -> bool {
71         match self.source {
72             RegionNameSource::NamedEarlyBoundRegion(..)
73             | RegionNameSource::NamedFreeRegion(..)
74             | RegionNameSource::Static => true,
75             RegionNameSource::SynthesizedFreeEnvRegion(..)
76             | RegionNameSource::AnonRegionFromArgument(..)
77             | RegionNameSource::AnonRegionFromUpvar(..)
78             | RegionNameSource::AnonRegionFromOutput(..)
79             | RegionNameSource::AnonRegionFromYieldTy(..)
80             | RegionNameSource::AnonRegionFromAsyncFn(..)
81             | RegionNameSource::AnonRegionFromImplSignature(..) => false,
82         }
83     }
84
85     pub(crate) fn span(&self) -> Option<Span> {
86         match self.source {
87             RegionNameSource::Static => None,
88             RegionNameSource::NamedEarlyBoundRegion(span)
89             | RegionNameSource::NamedFreeRegion(span)
90             | RegionNameSource::SynthesizedFreeEnvRegion(span, _)
91             | RegionNameSource::AnonRegionFromUpvar(span, _)
92             | RegionNameSource::AnonRegionFromYieldTy(span, _)
93             | RegionNameSource::AnonRegionFromAsyncFn(span)
94             | RegionNameSource::AnonRegionFromImplSignature(span, _) => Some(span),
95             RegionNameSource::AnonRegionFromArgument(ref highlight)
96             | RegionNameSource::AnonRegionFromOutput(ref highlight, _) => match *highlight {
97                 RegionNameHighlight::MatchedHirTy(span)
98                 | RegionNameHighlight::MatchedAdtAndSegment(span)
99                 | RegionNameHighlight::CannotMatchHirTy(span, _)
100                 | RegionNameHighlight::Occluded(span, _) => Some(span),
101             },
102         }
103     }
104
105     pub(crate) fn highlight_region_name(&self, diag: &mut Diagnostic) {
106         match &self.source {
107             RegionNameSource::NamedFreeRegion(span)
108             | RegionNameSource::NamedEarlyBoundRegion(span) => {
109                 diag.span_label(*span, format!("lifetime `{self}` defined here"));
110             }
111             RegionNameSource::SynthesizedFreeEnvRegion(span, note) => {
112                 diag.span_label(*span, format!("lifetime `{self}` represents this closure's body"));
113                 diag.note(*note);
114             }
115             RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::CannotMatchHirTy(
116                 span,
117                 type_name,
118             )) => {
119                 diag.span_label(*span, format!("has type `{type_name}`"));
120             }
121             RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::MatchedHirTy(span))
122             | RegionNameSource::AnonRegionFromOutput(RegionNameHighlight::MatchedHirTy(span), _)
123             | RegionNameSource::AnonRegionFromAsyncFn(span) => {
124                 diag.span_label(
125                     *span,
126                     format!("let's call the lifetime of this reference `{self}`"),
127                 );
128             }
129             RegionNameSource::AnonRegionFromArgument(
130                 RegionNameHighlight::MatchedAdtAndSegment(span),
131             )
132             | RegionNameSource::AnonRegionFromOutput(
133                 RegionNameHighlight::MatchedAdtAndSegment(span),
134                 _,
135             ) => {
136                 diag.span_label(*span, format!("let's call this `{self}`"));
137             }
138             RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::Occluded(
139                 span,
140                 type_name,
141             )) => {
142                 diag.span_label(
143                     *span,
144                     format!("lifetime `{self}` appears in the type {type_name}"),
145                 );
146             }
147             RegionNameSource::AnonRegionFromOutput(
148                 RegionNameHighlight::Occluded(span, type_name),
149                 mir_description,
150             ) => {
151                 diag.span_label(
152                     *span,
153                     format!(
154                         "return type{mir_description} `{type_name}` contains a lifetime `{self}`"
155                     ),
156                 );
157             }
158             RegionNameSource::AnonRegionFromUpvar(span, upvar_name) => {
159                 diag.span_label(
160                     *span,
161                     format!("lifetime `{self}` appears in the type of `{upvar_name}`"),
162                 );
163             }
164             RegionNameSource::AnonRegionFromOutput(
165                 RegionNameHighlight::CannotMatchHirTy(span, type_name),
166                 mir_description,
167             ) => {
168                 diag.span_label(*span, format!("return type{mir_description} is {type_name}"));
169             }
170             RegionNameSource::AnonRegionFromYieldTy(span, type_name) => {
171                 diag.span_label(*span, format!("yield type is {type_name}"));
172             }
173             RegionNameSource::AnonRegionFromImplSignature(span, location) => {
174                 diag.span_label(
175                     *span,
176                     format!("lifetime `{self}` appears in the `impl`'s {location}"),
177                 );
178             }
179             RegionNameSource::Static => {}
180         }
181     }
182 }
183
184 impl Display for RegionName {
185     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
186         write!(f, "{}", self.name)
187     }
188 }
189
190 impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
191     pub(crate) fn mir_def_id(&self) -> hir::def_id::LocalDefId {
192         self.body.source.def_id().expect_local()
193     }
194
195     pub(crate) fn mir_hir_id(&self) -> hir::HirId {
196         self.infcx.tcx.hir().local_def_id_to_hir_id(self.mir_def_id())
197     }
198
199     /// Generate a synthetic region named `'N`, where `N` is the next value of the counter. Then,
200     /// increment the counter.
201     ///
202     /// This is _not_ idempotent. Call `give_region_a_name` when possible.
203     fn synthesize_region_name(&self) -> Symbol {
204         let c = self.next_region_name.replace_with(|counter| *counter + 1);
205         Symbol::intern(&format!("'{:?}", c))
206     }
207
208     /// Maps from an internal MIR region vid to something that we can
209     /// report to the user. In some cases, the region vids will map
210     /// directly to lifetimes that the user has a name for (e.g.,
211     /// `'static`). But frequently they will not, in which case we
212     /// have to find some way to identify the lifetime to the user. To
213     /// that end, this function takes a "diagnostic" so that it can
214     /// create auxiliary notes as needed.
215     ///
216     /// The names are memoized, so this is both cheap to recompute and idempotent.
217     ///
218     /// Example (function arguments):
219     ///
220     /// Suppose we are trying to give a name to the lifetime of the
221     /// reference `x`:
222     ///
223     /// ```ignore (pseudo-rust)
224     /// fn foo(x: &u32) { .. }
225     /// ```
226     ///
227     /// This function would create a label like this:
228     ///
229     /// ```text
230     ///  | fn foo(x: &u32) { .. }
231     ///           ------- fully elaborated type of `x` is `&'1 u32`
232     /// ```
233     ///
234     /// and then return the name `'1` for us to use.
235     pub(crate) fn give_region_a_name(&self, fr: RegionVid) -> Option<RegionName> {
236         debug!(
237             "give_region_a_name(fr={:?}, counter={:?})",
238             fr,
239             self.next_region_name.try_borrow().unwrap()
240         );
241
242         assert!(self.regioncx.universal_regions().is_universal_region(fr));
243
244         if let Some(value) = self.region_names.try_borrow_mut().unwrap().get(&fr) {
245             return Some(value.clone());
246         }
247
248         let value = self
249             .give_name_from_error_region(fr)
250             .or_else(|| self.give_name_if_anonymous_region_appears_in_arguments(fr))
251             .or_else(|| self.give_name_if_anonymous_region_appears_in_upvars(fr))
252             .or_else(|| self.give_name_if_anonymous_region_appears_in_output(fr))
253             .or_else(|| self.give_name_if_anonymous_region_appears_in_yield_ty(fr))
254             .or_else(|| self.give_name_if_anonymous_region_appears_in_impl_signature(fr))
255             .or_else(|| self.give_name_if_anonymous_region_appears_in_arg_position_impl_trait(fr));
256
257         if let Some(value) = &value {
258             self.region_names.try_borrow_mut().unwrap().insert(fr, value.clone());
259         }
260
261         debug!("give_region_a_name: gave name {:?}", value);
262         value
263     }
264
265     /// Checks for the case where `fr` maps to something that the
266     /// *user* has a name for. In that case, we'll be able to map
267     /// `fr` to a `Region<'tcx>`, and that region will be one of
268     /// named variants.
269     #[instrument(level = "trace", skip(self))]
270     fn give_name_from_error_region(&self, fr: RegionVid) -> Option<RegionName> {
271         let error_region = self.to_error_region(fr)?;
272
273         let tcx = self.infcx.tcx;
274
275         debug!("give_region_a_name: error_region = {:?}", error_region);
276         match *error_region {
277             ty::ReEarlyBound(ebr) => {
278                 if ebr.has_name() {
279                     let span = tcx.hir().span_if_local(ebr.def_id).unwrap_or(DUMMY_SP);
280                     Some(RegionName {
281                         name: ebr.name,
282                         source: RegionNameSource::NamedEarlyBoundRegion(span),
283                     })
284                 } else {
285                     None
286                 }
287             }
288
289             ty::ReStatic => {
290                 Some(RegionName { name: kw::StaticLifetime, source: RegionNameSource::Static })
291             }
292
293             ty::ReFree(free_region) => match free_region.bound_region {
294                 ty::BoundRegionKind::BrNamed(region_def_id, name) => {
295                     // Get the span to point to, even if we don't use the name.
296                     let span = tcx.hir().span_if_local(region_def_id).unwrap_or(DUMMY_SP);
297                     debug!(
298                         "bound region named: {:?}, is_named: {:?}",
299                         name,
300                         free_region.bound_region.is_named()
301                     );
302
303                     if free_region.bound_region.is_named() {
304                         // A named region that is actually named.
305                         Some(RegionName { name, source: RegionNameSource::NamedFreeRegion(span) })
306                     } else if let hir::IsAsync::Async = tcx.asyncness(self.mir_hir_id().owner) {
307                         // If we spuriously thought that the region is named, we should let the
308                         // system generate a true name for error messages. Currently this can
309                         // happen if we have an elided name in an async fn for example: the
310                         // compiler will generate a region named `'_`, but reporting such a name is
311                         // not actually useful, so we synthesize a name for it instead.
312                         let name = self.synthesize_region_name();
313                         Some(RegionName {
314                             name,
315                             source: RegionNameSource::AnonRegionFromAsyncFn(span),
316                         })
317                     } else {
318                         None
319                     }
320                 }
321
322                 ty::BoundRegionKind::BrEnv => {
323                     let def_ty = self.regioncx.universal_regions().defining_ty;
324
325                     let DefiningTy::Closure(_, substs) = def_ty else {
326                         // Can't have BrEnv in functions, constants or generators.
327                         bug!("BrEnv outside of closure.");
328                     };
329                     let hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. })
330                         = tcx.hir().expect_expr(self.mir_hir_id()).kind
331                     else {
332                         bug!("Closure is not defined by a closure expr");
333                     };
334                     let region_name = self.synthesize_region_name();
335
336                     let closure_kind_ty = substs.as_closure().kind_ty();
337                     let note = match closure_kind_ty.to_opt_closure_kind() {
338                         Some(ty::ClosureKind::Fn) => {
339                             "closure implements `Fn`, so references to captured variables \
340                                 can't escape the closure"
341                         }
342                         Some(ty::ClosureKind::FnMut) => {
343                             "closure implements `FnMut`, so references to captured variables \
344                                 can't escape the closure"
345                         }
346                         Some(ty::ClosureKind::FnOnce) => {
347                             bug!("BrEnv in a `FnOnce` closure");
348                         }
349                         None => bug!("Closure kind not inferred in borrow check"),
350                     };
351
352                     Some(RegionName {
353                         name: region_name,
354                         source: RegionNameSource::SynthesizedFreeEnvRegion(fn_decl_span, note),
355                     })
356                 }
357
358                 ty::BoundRegionKind::BrAnon(..) => None,
359             },
360
361             ty::ReLateBound(..) | ty::ReVar(..) | ty::RePlaceholder(..) | ty::ReErased => None,
362         }
363     }
364
365     /// Finds an argument that contains `fr` and label it with a fully
366     /// elaborated type, returning something like `'1`. Result looks
367     /// like:
368     ///
369     /// ```text
370     ///  | fn foo(x: &u32) { .. }
371     ///           ------- fully elaborated type of `x` is `&'1 u32`
372     /// ```
373     #[instrument(level = "trace", skip(self))]
374     fn give_name_if_anonymous_region_appears_in_arguments(
375         &self,
376         fr: RegionVid,
377     ) -> Option<RegionName> {
378         let implicit_inputs = self.regioncx.universal_regions().defining_ty.implicit_inputs();
379         let argument_index = self.regioncx.get_argument_index_for_region(self.infcx.tcx, fr)?;
380
381         let arg_ty = self.regioncx.universal_regions().unnormalized_input_tys
382             [implicit_inputs + argument_index];
383         let (_, span) = self.regioncx.get_argument_name_and_span_for_region(
384             &self.body,
385             &self.local_names,
386             argument_index,
387         );
388
389         let highlight = self
390             .get_argument_hir_ty_for_highlighting(argument_index)
391             .and_then(|arg_hir_ty| self.highlight_if_we_can_match_hir_ty(fr, arg_ty, arg_hir_ty))
392             .unwrap_or_else(|| {
393                 // `highlight_if_we_cannot_match_hir_ty` needs to know the number we will give to
394                 // the anonymous region. If it succeeds, the `synthesize_region_name` call below
395                 // will increment the counter, "reserving" the number we just used.
396                 let counter = *self.next_region_name.try_borrow().unwrap();
397                 self.highlight_if_we_cannot_match_hir_ty(fr, arg_ty, span, counter)
398             });
399
400         Some(RegionName {
401             name: self.synthesize_region_name(),
402             source: RegionNameSource::AnonRegionFromArgument(highlight),
403         })
404     }
405
406     fn get_argument_hir_ty_for_highlighting(
407         &self,
408         argument_index: usize,
409     ) -> Option<&hir::Ty<'tcx>> {
410         let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(self.mir_hir_id())?;
411         let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(argument_index)?;
412         match argument_hir_ty.kind {
413             // This indicates a variable with no type annotation, like
414             // `|x|`... in that case, we can't highlight the type but
415             // must highlight the variable.
416             // NOTE(eddyb) this is handled in/by the sole caller
417             // (`give_name_if_anonymous_region_appears_in_arguments`).
418             hir::TyKind::Infer => None,
419
420             _ => Some(argument_hir_ty),
421         }
422     }
423
424     /// Attempts to highlight the specific part of a type in an argument
425     /// that has no type annotation.
426     /// For example, we might produce an annotation like this:
427     ///
428     /// ```text
429     ///  |     foo(|a, b| b)
430     ///  |          -  -
431     ///  |          |  |
432     ///  |          |  has type `&'1 u32`
433     ///  |          has type `&'2 u32`
434     /// ```
435     fn highlight_if_we_cannot_match_hir_ty(
436         &self,
437         needle_fr: RegionVid,
438         ty: Ty<'tcx>,
439         span: Span,
440         counter: usize,
441     ) -> RegionNameHighlight {
442         let mut highlight = RegionHighlightMode::new(self.infcx.tcx);
443         highlight.highlighting_region_vid(needle_fr, counter);
444         let type_name =
445             self.infcx.extract_inference_diagnostics_data(ty.into(), Some(highlight)).name;
446
447         debug!(
448             "highlight_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}",
449             type_name, needle_fr
450         );
451         if type_name.contains(&format!("'{counter}")) {
452             // Only add a label if we can confirm that a region was labelled.
453             RegionNameHighlight::CannotMatchHirTy(span, type_name)
454         } else {
455             RegionNameHighlight::Occluded(span, type_name)
456         }
457     }
458
459     /// Attempts to highlight the specific part of a type annotation
460     /// that contains the anonymous reference we want to give a name
461     /// to. For example, we might produce an annotation like this:
462     ///
463     /// ```text
464     ///  | fn a<T>(items: &[T]) -> Box<dyn Iterator<Item = &T>> {
465     ///  |                - let's call the lifetime of this reference `'1`
466     /// ```
467     ///
468     /// the way this works is that we match up `ty`, which is
469     /// a `Ty<'tcx>` (the internal form of the type) with
470     /// `hir_ty`, a `hir::Ty` (the syntax of the type
471     /// annotation). We are descending through the types stepwise,
472     /// looking in to find the region `needle_fr` in the internal
473     /// type. Once we find that, we can use the span of the `hir::Ty`
474     /// to add the highlight.
475     ///
476     /// This is a somewhat imperfect process, so along the way we also
477     /// keep track of the **closest** type we've found. If we fail to
478     /// find the exact `&` or `'_` to highlight, then we may fall back
479     /// to highlighting that closest type instead.
480     fn highlight_if_we_can_match_hir_ty(
481         &self,
482         needle_fr: RegionVid,
483         ty: Ty<'tcx>,
484         hir_ty: &hir::Ty<'_>,
485     ) -> Option<RegionNameHighlight> {
486         let search_stack: &mut Vec<(Ty<'tcx>, &hir::Ty<'_>)> = &mut vec![(ty, hir_ty)];
487
488         while let Some((ty, hir_ty)) = search_stack.pop() {
489             match (ty.kind(), &hir_ty.kind) {
490                 // Check if the `ty` is `&'X ..` where `'X`
491                 // is the region we are looking for -- if so, and we have a `&T`
492                 // on the RHS, then we want to highlight the `&` like so:
493                 //
494                 //     &
495                 //     - let's call the lifetime of this reference `'1`
496                 (
497                     ty::Ref(region, referent_ty, _),
498                     hir::TyKind::Rptr(_lifetime, referent_hir_ty),
499                 ) => {
500                     if region.to_region_vid() == needle_fr {
501                         // Just grab the first character, the `&`.
502                         let source_map = self.infcx.tcx.sess.source_map();
503                         let ampersand_span = source_map.start_point(hir_ty.span);
504
505                         return Some(RegionNameHighlight::MatchedHirTy(ampersand_span));
506                     }
507
508                     // Otherwise, let's descend into the referent types.
509                     search_stack.push((*referent_ty, &referent_hir_ty.ty));
510                 }
511
512                 // Match up something like `Foo<'1>`
513                 (
514                     ty::Adt(_adt_def, substs),
515                     hir::TyKind::Path(hir::QPath::Resolved(None, path)),
516                 ) => {
517                     match path.res {
518                         // Type parameters of the type alias have no reason to
519                         // be the same as those of the ADT.
520                         // FIXME: We should be able to do something similar to
521                         // match_adt_and_segment in this case.
522                         Res::Def(DefKind::TyAlias, _) => (),
523                         _ => {
524                             if let Some(last_segment) = path.segments.last() {
525                                 if let Some(highlight) = self.match_adt_and_segment(
526                                     substs,
527                                     needle_fr,
528                                     last_segment,
529                                     search_stack,
530                                 ) {
531                                     return Some(highlight);
532                                 }
533                             }
534                         }
535                     }
536                 }
537
538                 // The following cases don't have lifetimes, so we
539                 // just worry about trying to match up the rustc type
540                 // with the HIR types:
541                 (&ty::Tuple(elem_tys), hir::TyKind::Tup(elem_hir_tys)) => {
542                     search_stack.extend(iter::zip(elem_tys, *elem_hir_tys));
543                 }
544
545                 (ty::Slice(elem_ty), hir::TyKind::Slice(elem_hir_ty))
546                 | (ty::Array(elem_ty, _), hir::TyKind::Array(elem_hir_ty, _)) => {
547                     search_stack.push((*elem_ty, elem_hir_ty));
548                 }
549
550                 (ty::RawPtr(mut_ty), hir::TyKind::Ptr(mut_hir_ty)) => {
551                     search_stack.push((mut_ty.ty, &mut_hir_ty.ty));
552                 }
553
554                 _ => {
555                     // FIXME there are other cases that we could trace
556                 }
557             }
558         }
559
560         None
561     }
562
563     /// We've found an enum/struct/union type with the substitutions
564     /// `substs` and -- in the HIR -- a path type with the final
565     /// segment `last_segment`. Try to find a `'_` to highlight in
566     /// the generic args (or, if not, to produce new zipped pairs of
567     /// types+hir to search through).
568     fn match_adt_and_segment<'hir>(
569         &self,
570         substs: SubstsRef<'tcx>,
571         needle_fr: RegionVid,
572         last_segment: &'hir hir::PathSegment<'hir>,
573         search_stack: &mut Vec<(Ty<'tcx>, &'hir hir::Ty<'hir>)>,
574     ) -> Option<RegionNameHighlight> {
575         // Did the user give explicit arguments? (e.g., `Foo<..>`)
576         let args = last_segment.args.as_ref()?;
577         let lifetime =
578             self.try_match_adt_and_generic_args(substs, needle_fr, args, search_stack)?;
579         if lifetime.is_anonymous() {
580             None
581         } else {
582             Some(RegionNameHighlight::MatchedAdtAndSegment(lifetime.ident.span))
583         }
584     }
585
586     /// We've found an enum/struct/union type with the substitutions
587     /// `substs` and -- in the HIR -- a path with the generic
588     /// arguments `args`. If `needle_fr` appears in the args, return
589     /// the `hir::Lifetime` that corresponds to it. If not, push onto
590     /// `search_stack` the types+hir to search through.
591     fn try_match_adt_and_generic_args<'hir>(
592         &self,
593         substs: SubstsRef<'tcx>,
594         needle_fr: RegionVid,
595         args: &'hir hir::GenericArgs<'hir>,
596         search_stack: &mut Vec<(Ty<'tcx>, &'hir hir::Ty<'hir>)>,
597     ) -> Option<&'hir hir::Lifetime> {
598         for (kind, hir_arg) in iter::zip(substs, args.args) {
599             match (kind.unpack(), hir_arg) {
600                 (GenericArgKind::Lifetime(r), hir::GenericArg::Lifetime(lt)) => {
601                     if r.to_region_vid() == needle_fr {
602                         return Some(lt);
603                     }
604                 }
605
606                 (GenericArgKind::Type(ty), hir::GenericArg::Type(hir_ty)) => {
607                     search_stack.push((ty, hir_ty));
608                 }
609
610                 (GenericArgKind::Const(_ct), hir::GenericArg::Const(_hir_ct)) => {
611                     // Lifetimes cannot be found in consts, so we don't need
612                     // to search anything here.
613                 }
614
615                 (
616                     GenericArgKind::Lifetime(_)
617                     | GenericArgKind::Type(_)
618                     | GenericArgKind::Const(_),
619                     _,
620                 ) => {
621                     // HIR lowering sometimes doesn't catch this in erroneous
622                     // programs, so we need to use delay_span_bug here. See #82126.
623                     self.infcx.tcx.sess.delay_span_bug(
624                         hir_arg.span(),
625                         &format!("unmatched subst and hir arg: found {:?} vs {:?}", kind, hir_arg),
626                     );
627                 }
628             }
629         }
630
631         None
632     }
633
634     /// Finds a closure upvar that contains `fr` and label it with a
635     /// fully elaborated type, returning something like `'1`. Result
636     /// looks like:
637     ///
638     /// ```text
639     ///  | let x = Some(&22);
640     ///        - fully elaborated type of `x` is `Option<&'1 u32>`
641     /// ```
642     #[instrument(level = "trace", skip(self))]
643     fn give_name_if_anonymous_region_appears_in_upvars(&self, fr: RegionVid) -> Option<RegionName> {
644         let upvar_index = self.regioncx.get_upvar_index_for_region(self.infcx.tcx, fr)?;
645         let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
646             self.infcx.tcx,
647             &self.upvars,
648             upvar_index,
649         );
650         let region_name = self.synthesize_region_name();
651
652         Some(RegionName {
653             name: region_name,
654             source: RegionNameSource::AnonRegionFromUpvar(upvar_span, upvar_name),
655         })
656     }
657
658     /// Checks for arguments appearing in the (closure) return type. It
659     /// must be a closure since, in a free fn, such an argument would
660     /// have to either also appear in an argument (if using elision)
661     /// or be early bound (named, not in argument).
662     #[instrument(level = "trace", skip(self))]
663     fn give_name_if_anonymous_region_appears_in_output(&self, fr: RegionVid) -> Option<RegionName> {
664         let tcx = self.infcx.tcx;
665         let hir = tcx.hir();
666
667         let return_ty = self.regioncx.universal_regions().unnormalized_output_ty;
668         debug!("give_name_if_anonymous_region_appears_in_output: return_ty = {:?}", return_ty);
669         if !tcx.any_free_region_meets(&return_ty, |r| r.to_region_vid() == fr) {
670             return None;
671         }
672
673         let mir_hir_id = self.mir_hir_id();
674
675         let (return_span, mir_description, hir_ty) = match hir.get(mir_hir_id) {
676             hir::Node::Expr(hir::Expr {
677                 kind: hir::ExprKind::Closure(&hir::Closure { fn_decl, body, fn_decl_span, .. }),
678                 ..
679             }) => {
680                 let (mut span, mut hir_ty) = match fn_decl.output {
681                     hir::FnRetTy::DefaultReturn(_) => {
682                         (tcx.sess.source_map().end_point(fn_decl_span), None)
683                     }
684                     hir::FnRetTy::Return(hir_ty) => (fn_decl.output.span(), Some(hir_ty)),
685                 };
686                 let mir_description = match hir.body(body).generator_kind {
687                     Some(hir::GeneratorKind::Async(gen)) => match gen {
688                         hir::AsyncGeneratorKind::Block => " of async block",
689                         hir::AsyncGeneratorKind::Closure => " of async closure",
690                         hir::AsyncGeneratorKind::Fn => {
691                             let parent_item =
692                                 hir.get_by_def_id(hir.get_parent_item(mir_hir_id).def_id);
693                             let output = &parent_item
694                                 .fn_decl()
695                                 .expect("generator lowered from async fn should be in fn")
696                                 .output;
697                             span = output.span();
698                             if let hir::FnRetTy::Return(ret) = output {
699                                 hir_ty = Some(self.get_future_inner_return_ty(*ret));
700                             }
701                             " of async function"
702                         }
703                     },
704                     Some(hir::GeneratorKind::Gen) => " of generator",
705                     None => " of closure",
706                 };
707                 (span, mir_description, hir_ty)
708             }
709             node => match node.fn_decl() {
710                 Some(fn_decl) => {
711                     let hir_ty = match fn_decl.output {
712                         hir::FnRetTy::DefaultReturn(_) => None,
713                         hir::FnRetTy::Return(ty) => Some(ty),
714                     };
715                     (fn_decl.output.span(), "", hir_ty)
716                 }
717                 None => (self.body.span, "", None),
718             },
719         };
720
721         let highlight = hir_ty
722             .and_then(|hir_ty| self.highlight_if_we_can_match_hir_ty(fr, return_ty, hir_ty))
723             .unwrap_or_else(|| {
724                 // `highlight_if_we_cannot_match_hir_ty` needs to know the number we will give to
725                 // the anonymous region. If it succeeds, the `synthesize_region_name` call below
726                 // will increment the counter, "reserving" the number we just used.
727                 let counter = *self.next_region_name.try_borrow().unwrap();
728                 self.highlight_if_we_cannot_match_hir_ty(fr, return_ty, return_span, counter)
729             });
730
731         Some(RegionName {
732             name: self.synthesize_region_name(),
733             source: RegionNameSource::AnonRegionFromOutput(highlight, mir_description),
734         })
735     }
736
737     /// From the [`hir::Ty`] of an async function's lowered return type,
738     /// retrieve the `hir::Ty` representing the type the user originally wrote.
739     ///
740     /// e.g. given the function:
741     ///
742     /// ```
743     /// async fn foo() -> i32 { 2 }
744     /// ```
745     ///
746     /// this function, given the lowered return type of `foo`, an [`OpaqueDef`] that implements `Future<Output=i32>`,
747     /// returns the `i32`.
748     ///
749     /// [`OpaqueDef`]: hir::TyKind::OpaqueDef
750     fn get_future_inner_return_ty(&self, hir_ty: &'tcx hir::Ty<'tcx>) -> &'tcx hir::Ty<'tcx> {
751         let hir = self.infcx.tcx.hir();
752
753         let hir::TyKind::OpaqueDef(id, _, _) = hir_ty.kind else {
754             span_bug!(
755                 hir_ty.span,
756                 "lowered return type of async fn is not OpaqueDef: {:?}",
757                 hir_ty
758             );
759         };
760         let opaque_ty = hir.item(id);
761         if let hir::ItemKind::OpaqueTy(hir::OpaqueTy {
762             bounds:
763                 [
764                     hir::GenericBound::LangItemTrait(
765                         hir::LangItem::Future,
766                         _,
767                         _,
768                         hir::GenericArgs {
769                             bindings:
770                                 [
771                                     hir::TypeBinding {
772                                         ident: Ident { name: sym::Output, .. },
773                                         kind:
774                                             hir::TypeBindingKind::Equality { term: hir::Term::Ty(ty) },
775                                         ..
776                                     },
777                                 ],
778                             ..
779                         },
780                     ),
781                 ],
782             ..
783         }) = opaque_ty.kind
784         {
785             ty
786         } else {
787             span_bug!(
788                 hir_ty.span,
789                 "bounds from lowered return type of async fn did not match expected format: {:?}",
790                 opaque_ty
791             );
792         }
793     }
794
795     #[instrument(level = "trace", skip(self))]
796     fn give_name_if_anonymous_region_appears_in_yield_ty(
797         &self,
798         fr: RegionVid,
799     ) -> Option<RegionName> {
800         // Note: generators from `async fn` yield `()`, so we don't have to
801         // worry about them here.
802         let yield_ty = self.regioncx.universal_regions().yield_ty?;
803         debug!("give_name_if_anonymous_region_appears_in_yield_ty: yield_ty = {:?}", yield_ty);
804
805         let tcx = self.infcx.tcx;
806
807         if !tcx.any_free_region_meets(&yield_ty, |r| r.to_region_vid() == fr) {
808             return None;
809         }
810
811         let mut highlight = RegionHighlightMode::new(tcx);
812         highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap());
813         let type_name =
814             self.infcx.extract_inference_diagnostics_data(yield_ty.into(), Some(highlight)).name;
815
816         let yield_span = match tcx.hir().get(self.mir_hir_id()) {
817             hir::Node::Expr(hir::Expr {
818                 kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }),
819                 ..
820             }) => tcx.sess.source_map().end_point(fn_decl_span),
821             _ => self.body.span,
822         };
823
824         debug!(
825             "give_name_if_anonymous_region_appears_in_yield_ty: \
826              type_name = {:?}, yield_span = {:?}",
827             yield_span, type_name,
828         );
829
830         Some(RegionName {
831             name: self.synthesize_region_name(),
832             source: RegionNameSource::AnonRegionFromYieldTy(yield_span, type_name),
833         })
834     }
835
836     fn give_name_if_anonymous_region_appears_in_impl_signature(
837         &self,
838         fr: RegionVid,
839     ) -> Option<RegionName> {
840         let ty::ReEarlyBound(region) = *self.to_error_region(fr)? else {
841             return None;
842         };
843         if region.has_name() {
844             return None;
845         };
846
847         let tcx = self.infcx.tcx;
848         let region_parent = tcx.parent(region.def_id);
849         if tcx.def_kind(region_parent) != DefKind::Impl {
850             return None;
851         }
852
853         let found = tcx
854             .any_free_region_meets(&tcx.type_of(region_parent), |r| *r == ty::ReEarlyBound(region));
855
856         Some(RegionName {
857             name: self.synthesize_region_name(),
858             source: RegionNameSource::AnonRegionFromImplSignature(
859                 tcx.def_span(region.def_id),
860                 // FIXME(compiler-errors): Does this ever actually show up
861                 // anywhere other than the self type? I couldn't create an
862                 // example of a `'_` in the impl's trait being referenceable.
863                 if found { "self type" } else { "header" },
864             ),
865         })
866     }
867
868     fn give_name_if_anonymous_region_appears_in_arg_position_impl_trait(
869         &self,
870         fr: RegionVid,
871     ) -> Option<RegionName> {
872         let ty::ReEarlyBound(region) = *self.to_error_region(fr)? else {
873             return None;
874         };
875         if region.has_name() {
876             return None;
877         };
878
879         let predicates = self
880             .infcx
881             .tcx
882             .predicates_of(self.body.source.def_id())
883             .instantiate_identity(self.infcx.tcx)
884             .predicates;
885
886         if let Some(upvar_index) = self
887             .regioncx
888             .universal_regions()
889             .defining_ty
890             .upvar_tys()
891             .position(|ty| self.any_param_predicate_mentions(&predicates, ty, region))
892         {
893             let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
894                 self.infcx.tcx,
895                 &self.upvars,
896                 upvar_index,
897             );
898             let region_name = self.synthesize_region_name();
899
900             Some(RegionName {
901                 name: region_name,
902                 source: RegionNameSource::AnonRegionFromUpvar(upvar_span, upvar_name),
903             })
904         } else if let Some(arg_index) = self
905             .regioncx
906             .universal_regions()
907             .unnormalized_input_tys
908             .iter()
909             .position(|ty| self.any_param_predicate_mentions(&predicates, *ty, region))
910         {
911             let (arg_name, arg_span) = self.regioncx.get_argument_name_and_span_for_region(
912                 self.body,
913                 &self.local_names,
914                 arg_index,
915             );
916             let region_name = self.synthesize_region_name();
917
918             Some(RegionName {
919                 name: region_name,
920                 source: RegionNameSource::AnonRegionFromArgument(
921                     RegionNameHighlight::CannotMatchHirTy(arg_span, arg_name?.to_string()),
922                 ),
923             })
924         } else {
925             None
926         }
927     }
928
929     fn any_param_predicate_mentions(
930         &self,
931         predicates: &[ty::Predicate<'tcx>],
932         ty: Ty<'tcx>,
933         region: ty::EarlyBoundRegion,
934     ) -> bool {
935         let tcx = self.infcx.tcx;
936         ty.walk().any(|arg| {
937             if let ty::GenericArgKind::Type(ty) = arg.unpack()
938                 && let ty::Param(_) = ty.kind()
939             {
940                 predicates.iter().any(|pred| {
941                     match pred.kind().skip_binder() {
942                         ty::PredicateKind::Clause(ty::Clause::Trait(data)) if data.self_ty() == ty => {}
943                         ty::PredicateKind::Clause(ty::Clause::Projection(data)) if data.projection_ty.self_ty() == ty => {}
944                         _ => return false,
945                     }
946                     tcx.any_free_region_meets(pred, |r| {
947                         *r == ty::ReEarlyBound(region)
948                     })
949                 })
950             } else {
951                 false
952             }
953         })
954     }
955 }