1 use std::fmt::{self, Display};
3 use rustc_errors::DiagnosticBuilder;
5 use rustc_hir::def::{DefKind, Res};
6 use rustc_middle::ty::print::RegionHighlightMode;
7 use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
8 use rustc_middle::ty::{self, RegionVid, Ty};
9 use rustc_span::symbol::kw;
10 use rustc_span::{symbol::Symbol, Span, DUMMY_SP};
12 use crate::borrow_check::{nll::ToRegionVid, universal_regions::DefiningTy, MirBorrowckCtxt};
14 /// A name for a particular region used in emitting diagnostics. This name could be a generated
15 /// name like `'1`, a name used by the user like `'a`, or a name like `'static`.
16 #[derive(Debug, Clone)]
17 crate struct RegionName {
18 /// The name of the region (interned).
20 /// Where the region comes from.
21 crate source: RegionNameSource,
24 /// Denotes the source of a region that is named by a `RegionName`. For example, a free region that
25 /// was named by the user would get `NamedFreeRegion` and `'static` lifetime would get `Static`.
26 /// This helps to print the right kinds of diagnostics.
27 #[derive(Debug, Clone)]
28 crate enum RegionNameSource {
29 /// A bound (not free) region that was substituted at the def site (not an HRTB).
30 NamedEarlyBoundRegion(Span),
31 /// A free region that the user has a name (`'a`) for.
32 NamedFreeRegion(Span),
33 /// The `'static` region.
35 /// The free region corresponding to the environment of a closure.
36 SynthesizedFreeEnvRegion(Span, String),
37 /// The region name corresponds to a region where the type annotation is completely missing
38 /// from the code, e.g. in a closure arguments `|x| { ... }`, where `x` is a reference.
39 CannotMatchHirTy(Span, String),
40 /// The region name corresponds a reference that was found by traversing the type in the HIR.
42 /// A region name from the generics list of a struct/enum/union.
43 MatchedAdtAndSegment(Span),
44 /// The region corresponding to a closure upvar.
45 AnonRegionFromUpvar(Span, String),
46 /// The region corresponding to the return type of a closure.
47 AnonRegionFromOutput(Span, String, String),
48 /// The region from a type yielded by a generator.
49 AnonRegionFromYieldTy(Span, String),
50 /// An anonymous region from an async fn.
51 AnonRegionFromAsyncFn(Span),
55 crate fn was_named(&self) -> bool {
57 RegionNameSource::NamedEarlyBoundRegion(..)
58 | RegionNameSource::NamedFreeRegion(..)
59 | RegionNameSource::Static => true,
60 RegionNameSource::SynthesizedFreeEnvRegion(..)
61 | RegionNameSource::CannotMatchHirTy(..)
62 | RegionNameSource::MatchedHirTy(..)
63 | RegionNameSource::MatchedAdtAndSegment(..)
64 | RegionNameSource::AnonRegionFromUpvar(..)
65 | RegionNameSource::AnonRegionFromOutput(..)
66 | RegionNameSource::AnonRegionFromYieldTy(..)
67 | RegionNameSource::AnonRegionFromAsyncFn(..) => false,
71 crate fn highlight_region_name(&self, diag: &mut DiagnosticBuilder<'_>) {
73 RegionNameSource::NamedFreeRegion(span)
74 | RegionNameSource::NamedEarlyBoundRegion(span) => {
75 diag.span_label(*span, format!("lifetime `{}` defined here", self));
77 RegionNameSource::SynthesizedFreeEnvRegion(span, note) => {
80 format!("lifetime `{}` represents this closure's body", self),
84 RegionNameSource::CannotMatchHirTy(span, type_name) => {
85 diag.span_label(*span, format!("has type `{}`", type_name));
87 RegionNameSource::MatchedHirTy(span)
88 | RegionNameSource::AnonRegionFromAsyncFn(span) => {
91 format!("let's call the lifetime of this reference `{}`", self),
94 RegionNameSource::MatchedAdtAndSegment(span) => {
95 diag.span_label(*span, format!("let's call this `{}`", self));
97 RegionNameSource::AnonRegionFromUpvar(span, upvar_name) => {
100 format!("lifetime `{}` appears in the type of `{}`", self, upvar_name),
103 RegionNameSource::AnonRegionFromOutput(span, mir_description, type_name) => {
104 diag.span_label(*span, format!("return type{} is {}", mir_description, type_name));
106 RegionNameSource::AnonRegionFromYieldTy(span, type_name) => {
107 diag.span_label(*span, format!("yield type is {}", type_name));
109 RegionNameSource::Static => {}
114 impl Display for RegionName {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 write!(f, "{}", self.name)
120 impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
121 /// Generate a synthetic region named `'N`, where `N` is the next value of the counter. Then,
122 /// increment the counter.
124 /// This is _not_ idempotent. Call `give_region_a_name` when possible.
125 fn synthesize_region_name(&self) -> Symbol {
126 let c = self.next_region_name.replace_with(|counter| *counter + 1);
127 Symbol::intern(&format!("'{:?}", c))
130 /// Maps from an internal MIR region vid to something that we can
131 /// report to the user. In some cases, the region vids will map
132 /// directly to lifetimes that the user has a name for (e.g.,
133 /// `'static`). But frequently they will not, in which case we
134 /// have to find some way to identify the lifetime to the user. To
135 /// that end, this function takes a "diagnostic" so that it can
136 /// create auxiliary notes as needed.
138 /// The names are memoized, so this is both cheap to recompute and idempotent.
140 /// Example (function arguments):
142 /// Suppose we are trying to give a name to the lifetime of the
146 /// fn foo(x: &u32) { .. }
149 /// This function would create a label like this:
152 /// | fn foo(x: &u32) { .. }
153 /// ------- fully elaborated type of `x` is `&'1 u32`
156 /// and then return the name `'1` for us to use.
157 crate fn give_region_a_name(&self, fr: RegionVid) -> Option<RegionName> {
159 "give_region_a_name(fr={:?}, counter={:?})",
161 self.next_region_name.try_borrow().unwrap()
164 assert!(self.regioncx.universal_regions().is_universal_region(fr));
166 if let Some(value) = self.region_names.try_borrow_mut().unwrap().get(&fr) {
167 return Some(value.clone());
171 .give_name_from_error_region(fr)
172 .or_else(|| self.give_name_if_anonymous_region_appears_in_arguments(fr))
173 .or_else(|| self.give_name_if_anonymous_region_appears_in_upvars(fr))
174 .or_else(|| self.give_name_if_anonymous_region_appears_in_output(fr))
175 .or_else(|| self.give_name_if_anonymous_region_appears_in_yield_ty(fr));
177 if let Some(ref value) = value {
178 self.region_names.try_borrow_mut().unwrap().insert(fr, value.clone());
181 debug!("give_region_a_name: gave name {:?}", value);
185 /// Checks for the case where `fr` maps to something that the
186 /// *user* has a name for. In that case, we'll be able to map
187 /// `fr` to a `Region<'tcx>`, and that region will be one of
189 fn give_name_from_error_region(&self, fr: RegionVid) -> Option<RegionName> {
190 let error_region = self.to_error_region(fr)?;
192 let tcx = self.infcx.tcx;
194 debug!("give_region_a_name: error_region = {:?}", error_region);
196 ty::ReEarlyBound(ebr) => {
198 let span = tcx.hir().span_if_local(ebr.def_id).unwrap_or(DUMMY_SP);
201 source: RegionNameSource::NamedEarlyBoundRegion(span),
209 Some(RegionName { name: kw::StaticLifetime, source: RegionNameSource::Static })
212 ty::ReFree(free_region) => match free_region.bound_region {
213 ty::BoundRegion::BrNamed(region_def_id, name) => {
214 // Get the span to point to, even if we don't use the name.
215 let span = tcx.hir().span_if_local(region_def_id).unwrap_or(DUMMY_SP);
217 "bound region named: {:?}, is_named: {:?}",
219 free_region.bound_region.is_named()
222 if free_region.bound_region.is_named() {
223 // A named region that is actually named.
224 Some(RegionName { name, source: RegionNameSource::NamedFreeRegion(span) })
226 // If we spuriously thought that the region is named, we should let the
227 // system generate a true name for error messages. Currently this can
228 // happen if we have an elided name in an async fn for example: the
229 // compiler will generate a region named `'_`, but reporting such a name is
230 // not actually useful, so we synthesize a name for it instead.
231 let name = self.synthesize_region_name();
234 source: RegionNameSource::AnonRegionFromAsyncFn(span),
239 ty::BoundRegion::BrEnv => {
240 let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id);
241 let def_ty = self.regioncx.universal_regions().defining_ty;
243 if let DefiningTy::Closure(_, substs) = def_ty {
244 let args_span = if let hir::ExprKind::Closure(_, _, _, span, _) =
245 tcx.hir().expect_expr(mir_hir_id).kind
249 bug!("Closure is not defined by a closure expr");
251 let region_name = self.synthesize_region_name();
253 let closure_kind_ty = substs.as_closure().kind_ty();
254 let note = match closure_kind_ty.to_opt_closure_kind() {
255 Some(ty::ClosureKind::Fn) => {
256 "closure implements `Fn`, so references to captured variables \
257 can't escape the closure"
259 Some(ty::ClosureKind::FnMut) => {
260 "closure implements `FnMut`, so references to captured variables \
261 can't escape the closure"
263 Some(ty::ClosureKind::FnOnce) => {
264 bug!("BrEnv in a `FnOnce` closure");
266 None => bug!("Closure kind not inferred in borrow check"),
271 source: RegionNameSource::SynthesizedFreeEnvRegion(
277 // Can't have BrEnv in functions, constants or generators.
278 bug!("BrEnv outside of closure.");
282 ty::BoundRegion::BrAnon(_) => None,
287 | ty::RePlaceholder(..)
289 | ty::ReErased => None,
293 /// Finds an argument that contains `fr` and label it with a fully
294 /// elaborated type, returning something like `'1`. Result looks
298 /// | fn foo(x: &u32) { .. }
299 /// ------- fully elaborated type of `x` is `&'1 u32`
301 fn give_name_if_anonymous_region_appears_in_arguments(
304 ) -> Option<RegionName> {
305 let implicit_inputs = self.regioncx.universal_regions().defining_ty.implicit_inputs();
306 let argument_index = self.regioncx.get_argument_index_for_region(self.infcx.tcx, fr)?;
308 let arg_ty = self.regioncx.universal_regions().unnormalized_input_tys
309 [implicit_inputs + argument_index];
310 if let Some(region_name) =
311 self.give_name_if_we_can_match_hir_ty_from_argument(fr, arg_ty, argument_index)
313 return Some(region_name);
316 self.give_name_if_we_cannot_match_hir_ty(fr, arg_ty)
319 fn give_name_if_we_can_match_hir_ty_from_argument(
321 needle_fr: RegionVid,
322 argument_ty: Ty<'tcx>,
323 argument_index: usize,
324 ) -> Option<RegionName> {
325 let mir_hir_id = self.infcx.tcx.hir().as_local_hir_id(self.mir_def_id);
326 let fn_decl = self.infcx.tcx.hir().fn_decl_by_hir_id(mir_hir_id)?;
327 let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(argument_index)?;
328 match argument_hir_ty.kind {
329 // This indicates a variable with no type annotation, like
330 // `|x|`... in that case, we can't highlight the type but
331 // must highlight the variable.
332 // NOTE(eddyb) this is handled in/by the sole caller
333 // (`give_name_if_anonymous_region_appears_in_arguments`).
334 hir::TyKind::Infer => None,
336 _ => self.give_name_if_we_can_match_hir_ty(needle_fr, argument_ty, argument_hir_ty),
340 /// Attempts to highlight the specific part of a type in an argument
341 /// that has no type annotation.
342 /// For example, we might produce an annotation like this:
348 /// | | has type `&'1 u32`
349 /// | has type `&'2 u32`
351 fn give_name_if_we_cannot_match_hir_ty(
353 needle_fr: RegionVid,
354 argument_ty: Ty<'tcx>,
355 ) -> Option<RegionName> {
356 let counter = *self.next_region_name.try_borrow().unwrap();
357 let mut highlight = RegionHighlightMode::default();
358 highlight.highlighting_region_vid(needle_fr, counter);
359 let type_name = self.infcx.extract_type_name(&argument_ty, Some(highlight)).0;
362 "give_name_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}",
365 let assigned_region_name = if type_name.find(&format!("'{}", counter)).is_some() {
366 // Only add a label if we can confirm that a region was labelled.
368 self.regioncx.get_argument_index_for_region(self.infcx.tcx, needle_fr)?;
369 let (_, span) = self.regioncx.get_argument_name_and_span_for_region(
376 // This counter value will already have been used, so this function will increment
377 // it so the next value will be used next and return the region name that would
379 name: self.synthesize_region_name(),
380 source: RegionNameSource::CannotMatchHirTy(span, type_name),
389 /// Attempts to highlight the specific part of a type annotation
390 /// that contains the anonymous reference we want to give a name
391 /// to. For example, we might produce an annotation like this:
394 /// | fn a<T>(items: &[T]) -> Box<dyn Iterator<Item = &T>> {
395 /// | - let's call the lifetime of this reference `'1`
398 /// the way this works is that we match up `argument_ty`, which is
399 /// a `Ty<'tcx>` (the internal form of the type) with
400 /// `argument_hir_ty`, a `hir::Ty` (the syntax of the type
401 /// annotation). We are descending through the types stepwise,
402 /// looking in to find the region `needle_fr` in the internal
403 /// type. Once we find that, we can use the span of the `hir::Ty`
404 /// to add the highlight.
406 /// This is a somewhat imperfect process, so along the way we also
407 /// keep track of the **closest** type we've found. If we fail to
408 /// find the exact `&` or `'_` to highlight, then we may fall back
409 /// to highlighting that closest type instead.
410 fn give_name_if_we_can_match_hir_ty(
412 needle_fr: RegionVid,
413 argument_ty: Ty<'tcx>,
414 argument_hir_ty: &hir::Ty<'_>,
415 ) -> Option<RegionName> {
416 let search_stack: &mut Vec<(Ty<'tcx>, &hir::Ty<'_>)> =
417 &mut vec![(argument_ty, argument_hir_ty)];
419 while let Some((ty, hir_ty)) = search_stack.pop() {
420 match (&ty.kind, &hir_ty.kind) {
421 // Check if the `argument_ty` is `&'X ..` where `'X`
422 // is the region we are looking for -- if so, and we have a `&T`
423 // on the RHS, then we want to highlight the `&` like so:
426 // - let's call the lifetime of this reference `'1`
428 ty::Ref(region, referent_ty, _),
429 hir::TyKind::Rptr(_lifetime, referent_hir_ty),
431 if region.to_region_vid() == needle_fr {
432 let region_name = self.synthesize_region_name();
434 // Just grab the first character, the `&`.
435 let source_map = self.infcx.tcx.sess.source_map();
436 let ampersand_span = source_map.start_point(hir_ty.span);
438 return Some(RegionName {
440 source: RegionNameSource::MatchedHirTy(ampersand_span),
444 // Otherwise, let's descend into the referent types.
445 search_stack.push((referent_ty, &referent_hir_ty.ty));
448 // Match up something like `Foo<'1>`
450 ty::Adt(_adt_def, substs),
451 hir::TyKind::Path(hir::QPath::Resolved(None, path)),
454 // Type parameters of the type alias have no reason to
455 // be the same as those of the ADT.
456 // FIXME: We should be able to do something similar to
457 // match_adt_and_segment in this case.
458 Res::Def(DefKind::TyAlias, _) => (),
460 if let Some(last_segment) = path.segments.last() {
461 if let Some(name) = self.match_adt_and_segment(
474 // The following cases don't have lifetimes, so we
475 // just worry about trying to match up the rustc type
476 // with the HIR types:
477 (ty::Tuple(elem_tys), hir::TyKind::Tup(elem_hir_tys)) => {
478 search_stack.extend(elem_tys.iter().map(|k| k.expect_ty()).zip(*elem_hir_tys));
481 (ty::Slice(elem_ty), hir::TyKind::Slice(elem_hir_ty))
482 | (ty::Array(elem_ty, _), hir::TyKind::Array(elem_hir_ty, _)) => {
483 search_stack.push((elem_ty, elem_hir_ty));
486 (ty::RawPtr(mut_ty), hir::TyKind::Ptr(mut_hir_ty)) => {
487 search_stack.push((mut_ty.ty, &mut_hir_ty.ty));
491 // FIXME there are other cases that we could trace
499 /// We've found an enum/struct/union type with the substitutions
500 /// `substs` and -- in the HIR -- a path type with the final
501 /// segment `last_segment`. Try to find a `'_` to highlight in
502 /// the generic args (or, if not, to produce new zipped pairs of
503 /// types+hir to search through).
504 fn match_adt_and_segment<'hir>(
506 substs: SubstsRef<'tcx>,
507 needle_fr: RegionVid,
508 last_segment: &'hir hir::PathSegment<'hir>,
509 search_stack: &mut Vec<(Ty<'tcx>, &'hir hir::Ty<'hir>)>,
510 ) -> Option<RegionName> {
511 // Did the user give explicit arguments? (e.g., `Foo<..>`)
512 let args = last_segment.args.as_ref()?;
514 self.try_match_adt_and_generic_args(substs, needle_fr, args, search_stack)?;
515 match lifetime.name {
516 hir::LifetimeName::Param(_)
517 | hir::LifetimeName::Error
518 | hir::LifetimeName::Static
519 | hir::LifetimeName::Underscore => {
520 let region_name = self.synthesize_region_name();
521 let ampersand_span = lifetime.span;
524 source: RegionNameSource::MatchedAdtAndSegment(ampersand_span),
528 hir::LifetimeName::ImplicitObjectLifetimeDefault | hir::LifetimeName::Implicit => {
529 // In this case, the user left off the lifetime; so
530 // they wrote something like:
536 // where the fully elaborated form is `Foo<'_, '1,
537 // T>`. We don't consider this a match; instead we let
538 // the "fully elaborated" type fallback above handle
545 /// We've found an enum/struct/union type with the substitutions
546 /// `substs` and -- in the HIR -- a path with the generic
547 /// arguments `args`. If `needle_fr` appears in the args, return
548 /// the `hir::Lifetime` that corresponds to it. If not, push onto
549 /// `search_stack` the types+hir to search through.
550 fn try_match_adt_and_generic_args<'hir>(
552 substs: SubstsRef<'tcx>,
553 needle_fr: RegionVid,
554 args: &'hir hir::GenericArgs<'hir>,
555 search_stack: &mut Vec<(Ty<'tcx>, &'hir hir::Ty<'hir>)>,
556 ) -> Option<&'hir hir::Lifetime> {
557 for (kind, hir_arg) in substs.iter().zip(args.args) {
558 match (kind.unpack(), hir_arg) {
559 (GenericArgKind::Lifetime(r), hir::GenericArg::Lifetime(lt)) => {
560 if r.to_region_vid() == needle_fr {
565 (GenericArgKind::Type(ty), hir::GenericArg::Type(hir_ty)) => {
566 search_stack.push((ty, hir_ty));
569 (GenericArgKind::Const(_ct), hir::GenericArg::Const(_hir_ct)) => {
570 // Lifetimes cannot be found in consts, so we don't need
571 // to search anything here.
575 GenericArgKind::Lifetime(_)
576 | GenericArgKind::Type(_)
577 | GenericArgKind::Const(_),
580 // I *think* that HIR lowering should ensure this
581 // doesn't happen, even in erroneous
582 // programs. Else we should use delay-span-bug.
585 "unmatched subst and hir arg: found {:?} vs {:?}",
596 /// Finds a closure upvar that contains `fr` and label it with a
597 /// fully elaborated type, returning something like `'1`. Result
601 /// | let x = Some(&22);
602 /// - fully elaborated type of `x` is `Option<&'1 u32>`
604 fn give_name_if_anonymous_region_appears_in_upvars(&self, fr: RegionVid) -> Option<RegionName> {
605 let upvar_index = self.regioncx.get_upvar_index_for_region(self.infcx.tcx, fr)?;
606 let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
611 let region_name = self.synthesize_region_name();
615 source: RegionNameSource::AnonRegionFromUpvar(upvar_span, upvar_name.to_string()),
619 /// Checks for arguments appearing in the (closure) return type. It
620 /// must be a closure since, in a free fn, such an argument would
621 /// have to either also appear in an argument (if using elision)
622 /// or be early bound (named, not in argument).
623 fn give_name_if_anonymous_region_appears_in_output(&self, fr: RegionVid) -> Option<RegionName> {
624 let tcx = self.infcx.tcx;
626 let return_ty = self.regioncx.universal_regions().unnormalized_output_ty;
627 debug!("give_name_if_anonymous_region_appears_in_output: return_ty = {:?}", return_ty);
628 if !tcx.any_free_region_meets(&return_ty, |r| r.to_region_vid() == fr) {
632 let mut highlight = RegionHighlightMode::default();
633 highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap());
634 let type_name = self.infcx.extract_type_name(&return_ty, Some(highlight)).0;
636 let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id);
638 let (return_span, mir_description) = match tcx.hir().get(mir_hir_id) {
639 hir::Node::Expr(hir::Expr {
640 kind: hir::ExprKind::Closure(_, return_ty, _, span, gen_move),
643 match return_ty.output {
644 hir::FnRetTy::DefaultReturn(_) => tcx.sess.source_map().end_point(*span),
645 hir::FnRetTy::Return(_) => return_ty.output.span(),
647 if gen_move.is_some() { " of generator" } else { " of closure" },
649 hir::Node::ImplItem(hir::ImplItem {
650 kind: hir::ImplItemKind::Fn(method_sig, _),
652 }) => (method_sig.decl.output.span(), ""),
653 _ => (self.body.span, ""),
657 // This counter value will already have been used, so this function will increment it
658 // so the next value will be used next and return the region name that would have been
660 name: self.synthesize_region_name(),
661 source: RegionNameSource::AnonRegionFromOutput(
663 mir_description.to_string(),
669 fn give_name_if_anonymous_region_appears_in_yield_ty(
672 ) -> Option<RegionName> {
673 // Note: generators from `async fn` yield `()`, so we don't have to
674 // worry about them here.
675 let yield_ty = self.regioncx.universal_regions().yield_ty?;
676 debug!("give_name_if_anonymous_region_appears_in_yield_ty: yield_ty = {:?}", yield_ty,);
678 let tcx = self.infcx.tcx;
680 if !tcx.any_free_region_meets(&yield_ty, |r| r.to_region_vid() == fr) {
684 let mut highlight = RegionHighlightMode::default();
685 highlight.highlighting_region_vid(fr, *self.next_region_name.try_borrow().unwrap());
686 let type_name = self.infcx.extract_type_name(&yield_ty, Some(highlight)).0;
688 let mir_hir_id = tcx.hir().as_local_hir_id(self.mir_def_id);
690 let yield_span = match tcx.hir().get(mir_hir_id) {
691 hir::Node::Expr(hir::Expr {
692 kind: hir::ExprKind::Closure(_, _, _, span, _), ..
693 }) => (tcx.sess.source_map().end_point(*span)),
698 "give_name_if_anonymous_region_appears_in_yield_ty: \
699 type_name = {:?}, yield_span = {:?}",
700 yield_span, type_name,
704 name: self.synthesize_region_name(),
705 source: RegionNameSource::AnonRegionFromYieldTy(yield_span, type_name),