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