1 //! Error reporting machinery for lifetime errors.
3 use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
4 use rustc_infer::infer::{
5 error_reporting::nice_region_error::NiceRegionError,
6 error_reporting::unexpected_hidden_region_diagnostic, NllRegionVariableOrigin,
8 use rustc_middle::hir::place::PlaceBase;
9 use rustc_middle::mir::{ConstraintCategory, ReturnConstraint};
10 use rustc_middle::ty::subst::{InternalSubsts, Subst};
11 use rustc_middle::ty::{self, RegionVid, Ty};
12 use rustc_span::symbol::{kw, sym};
13 use rustc_span::{BytePos, Span};
15 use crate::borrowck_errors;
17 use super::{OutlivesSuggestionBuilder, RegionName};
18 use crate::region_infer::BlameConstraint;
20 nll::ConstraintDescription,
21 region_infer::{values::RegionElement, TypeTest},
22 universal_regions::DefiningTy,
26 impl ConstraintDescription for ConstraintCategory {
27 fn description(&self) -> &'static str {
28 // Must end with a space. Allows for empty names to be provided.
30 ConstraintCategory::Assignment => "assignment ",
31 ConstraintCategory::Return(_) => "returning this value ",
32 ConstraintCategory::Yield => "yielding this value ",
33 ConstraintCategory::UseAsConst => "using this value as a constant ",
34 ConstraintCategory::UseAsStatic => "using this value as a static ",
35 ConstraintCategory::Cast => "cast ",
36 ConstraintCategory::CallArgument => "argument ",
37 ConstraintCategory::TypeAnnotation => "type annotation ",
38 ConstraintCategory::ClosureBounds => "closure body ",
39 ConstraintCategory::SizedBound => "proving this value is `Sized` ",
40 ConstraintCategory::CopyBound => "copying this value ",
41 ConstraintCategory::OpaqueType => "opaque type ",
42 ConstraintCategory::ClosureUpvar(_) => "closure capture ",
43 ConstraintCategory::Usage => "this usage ",
44 ConstraintCategory::Predicate(_)
45 | ConstraintCategory::Boring
46 | ConstraintCategory::BoringNoLocation
47 | ConstraintCategory::Internal => "",
52 /// A collection of errors encountered during region inference. This is needed to efficiently
53 /// report errors after borrow checking.
55 /// Usually we expect this to either be empty or contain a small number of items, so we can avoid
56 /// allocation most of the time.
57 crate type RegionErrors<'tcx> = Vec<RegionErrorKind<'tcx>>;
59 #[derive(Clone, Debug)]
60 crate enum RegionErrorKind<'tcx> {
61 /// A generic bound failure for a type test (`T: 'a`).
62 TypeTestError { type_test: TypeTest<'tcx> },
64 /// An unexpected hidden region for an opaque type.
65 UnexpectedHiddenRegion {
66 /// The span for the member constraint.
70 /// The unexpected region.
71 member_region: ty::Region<'tcx>,
74 /// Higher-ranked subtyping error.
75 BoundUniversalRegionError {
76 /// The placeholder free region.
78 /// The region element that erroneously must be outlived by `longer_fr`.
79 error_element: RegionElement,
80 /// The placeholder region.
81 placeholder: ty::PlaceholderRegion,
84 /// Any other lifetime error.
86 /// The origin of the region.
87 fr_origin: NllRegionVariableOrigin,
88 /// The region that should outlive `shorter_fr`.
90 /// The region that should be shorter, but we can't prove it.
91 shorter_fr: RegionVid,
92 /// Indicates whether this is a reported error. We currently only report the first error
93 /// encountered and leave the rest unreported so as not to overwhelm the user.
98 /// Information about the various region constraints involved in a borrow checker error.
99 #[derive(Clone, Debug)]
100 pub struct ErrorConstraintInfo {
102 pub(super) fr: RegionVid,
103 pub(super) fr_is_local: bool,
104 pub(super) outlived_fr: RegionVid,
105 pub(super) outlived_fr_is_local: bool,
107 // Category and span for best blame constraint
108 pub(super) category: ConstraintCategory,
109 pub(super) span: Span,
112 impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
113 /// Converts a region inference variable into a `ty::Region` that
114 /// we can use for error reporting. If `r` is universally bound,
115 /// then we use the name that we have on record for it. If `r` is
116 /// existentially bound, then we check its inferred value and try
117 /// to find a good name from that. Returns `None` if we can't find
118 /// one (e.g., this is just some random part of the CFG).
119 pub(super) fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
120 self.to_error_region_vid(r).and_then(|r| self.regioncx.region_definition(r).external_name)
123 /// Returns the `RegionVid` corresponding to the region returned by
124 /// `to_error_region`.
125 pub(super) fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
126 if self.regioncx.universal_regions().is_universal_region(r) {
129 // We just want something nameable, even if it's not
130 // actually an upper bound.
131 let upper_bound = self.regioncx.approx_universal_upper_bound(r);
133 if self.regioncx.upper_bound_in_region_scc(r, upper_bound) {
134 self.to_error_region_vid(upper_bound)
141 /// Returns `true` if a closure is inferred to be an `FnMut` closure.
142 fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
143 if let Some(ty::ReFree(free_region)) = self.to_error_region(fr).as_deref()
144 && let ty::BoundRegionKind::BrEnv = free_region.bound_region
145 && let DefiningTy::Closure(_, substs) = self.regioncx.universal_regions().defining_ty
147 return substs.as_closure().kind() == ty::ClosureKind::FnMut;
153 /// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`.
154 pub(crate) fn report_region_errors(&mut self, nll_errors: RegionErrors<'tcx>) {
155 // Iterate through all the errors, producing a diagnostic for each one. The diagnostics are
156 // buffered in the `MirBorrowckCtxt`.
158 let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
160 for nll_error in nll_errors.into_iter() {
162 RegionErrorKind::TypeTestError { type_test } => {
163 // Try to convert the lower-bound region into something named we can print for the user.
164 let lower_bound_region = self.to_error_region(type_test.lower_bound);
166 let type_test_span = type_test.locations.span(&self.body);
168 if let Some(lower_bound_region) = lower_bound_region {
169 self.buffer_error(self.infcx.construct_generic_bound_failure(
172 type_test.generic_kind,
176 // FIXME. We should handle this case better. It
177 // indicates that we have e.g., some region variable
178 // whose value is like `'a+'b` where `'a` and `'b` are
179 // distinct unrelated universal regions that are not
180 // known to outlive one another. It'd be nice to have
181 // some examples where this arises to decide how best
182 // to report it; we could probably handle it by
183 // iterating over the universal regions and reporting
184 // an error that multiple bounds are required.
185 self.buffer_error(self.infcx.tcx.sess.struct_span_err(
187 &format!("`{}` does not live long enough", type_test.generic_kind),
192 RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, member_region } => {
193 let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
194 let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
195 self.buffer_error(unexpected_hidden_region_diagnostic(
203 RegionErrorKind::BoundUniversalRegionError {
208 let error_vid = self.regioncx.region_from_element(longer_fr, &error_element);
210 // Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
211 let (_, cause) = self.regioncx.find_outlives_blame_span(
214 NllRegionVariableOrigin::Placeholder(placeholder),
218 let universe = placeholder.universe;
219 let universe_info = self.regioncx.universe_info(universe);
221 universe_info.report_error(self, placeholder, error_element, cause);
224 RegionErrorKind::RegionError { fr_origin, longer_fr, shorter_fr, is_reported } => {
226 self.report_region_error(
230 &mut outlives_suggestion,
233 // We only report the first error, so as not to overwhelm the user. See
234 // `RegRegionErrorKind` docs.
236 // FIXME: currently we do nothing with these, but perhaps we can do better?
237 // FIXME: try collecting these constraints on the outlives suggestion
238 // builder. Does it make the suggestions any better?
240 "Unreported region error: can't prove that {:?}: {:?}",
241 longer_fr, shorter_fr
248 // Emit one outlives suggestions for each MIR def we borrowck
249 outlives_suggestion.add_suggestion(self);
252 /// Report an error because the universal region `fr` was required to outlive
253 /// `outlived_fr` but it is not known to do so. For example:
256 /// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
259 /// Here we would be invoked with `fr = 'a` and `outlived_fr = `'b`.
260 pub(crate) fn report_region_error(
263 fr_origin: NllRegionVariableOrigin,
264 outlived_fr: RegionVid,
265 outlives_suggestion: &mut OutlivesSuggestionBuilder,
267 debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
269 let BlameConstraint { category, cause, variance_info, from_closure: _ } =
270 self.regioncx.best_blame_constraint(&self.body, fr, fr_origin, |r| {
271 self.regioncx.provides_universal_region(r, fr, outlived_fr)
274 debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info);
275 // Check if we can use one of the "nice region errors".
276 if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
277 let nice = NiceRegionError::new_from_span(self.infcx, cause.span, o, f);
278 if let Some(diag) = nice.try_report_from_nll() {
279 self.buffer_error(diag);
284 let (fr_is_local, outlived_fr_is_local): (bool, bool) = (
285 self.regioncx.universal_regions().is_local_free_region(fr),
286 self.regioncx.universal_regions().is_local_free_region(outlived_fr),
290 "report_region_error: fr_is_local={:?} outlived_fr_is_local={:?} category={:?}",
291 fr_is_local, outlived_fr_is_local, category
294 let errci = ErrorConstraintInfo {
298 outlived_fr_is_local,
303 let mut diag = match (category, fr_is_local, outlived_fr_is_local) {
304 (ConstraintCategory::Return(kind), true, false) if self.is_closure_fn_mut(fr) => {
305 self.report_fnmut_error(&errci, kind)
307 (ConstraintCategory::Assignment, true, false)
308 | (ConstraintCategory::CallArgument, true, false) => {
309 let mut db = self.report_escaping_data_error(&errci);
311 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
312 outlives_suggestion.collect_constraint(fr, outlived_fr);
317 let mut db = self.report_general_error(&errci);
319 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
320 outlives_suggestion.collect_constraint(fr, outlived_fr);
326 match variance_info {
327 ty::VarianceDiagInfo::None => {}
328 ty::VarianceDiagInfo::Invariant { ty, param_index } => {
329 let (desc, note) = match ty.kind() {
330 ty::RawPtr(ty_mut) => {
331 assert_eq!(ty_mut.mutbl, rustc_hir::Mutability::Mut);
333 format!("a mutable pointer to `{}`", ty_mut.ty),
334 "mutable pointers are invariant over their type parameter".to_string(),
337 ty::Ref(_, inner_ty, mutbl) => {
338 assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
340 format!("a mutable reference to `{inner_ty}`"),
341 "mutable references are invariant over their type parameter"
345 ty::Adt(adt, substs) => {
346 let generic_arg = substs[param_index as usize];
347 let identity_substs =
348 InternalSubsts::identity_for_item(self.infcx.tcx, adt.did());
349 let base_ty = self.infcx.tcx.mk_adt(*adt, identity_substs);
350 let base_generic_arg = identity_substs[param_index as usize];
351 let adt_desc = adt.descr();
354 "the type `{ty}`, which makes the generic argument `{generic_arg}` invariant"
357 "the {adt_desc} `{base_ty}` is invariant over the parameter `{base_generic_arg}`"
361 ty::FnDef(def_id, _) => {
362 let name = self.infcx.tcx.item_name(*def_id);
363 let identity_substs =
364 InternalSubsts::identity_for_item(self.infcx.tcx, *def_id);
365 let desc = format!("a function pointer to `{name}`");
367 "the function `{name}` is invariant over the parameter `{}`",
368 identity_substs[param_index as usize]
372 _ => panic!("Unexpected type {:?}", ty),
374 diag.note(&format!("requirement occurs because of {desc}",));
376 diag.help("see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance");
380 self.buffer_error(diag);
383 /// Report a specialized error when `FnMut` closures return a reference to a captured variable.
384 /// This function expects `fr` to be local and `outlived_fr` to not be local.
387 /// error: captured variable cannot escape `FnMut` closure body
388 /// --> $DIR/issue-53040.rs:15:8
391 /// | -- ^^^^^^ creates a reference to a captured variable which escapes the closure body
393 /// | inferred to be a `FnMut` closure
395 /// = note: `FnMut` closures only have access to their captured variables while they are
397 /// = note: ...therefore, returned references to captured variables will escape the closure
399 fn report_fnmut_error(
401 errci: &ErrorConstraintInfo,
402 kind: ReturnConstraint,
403 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
404 let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
410 .struct_span_err(*span, "captured variable cannot escape `FnMut` closure body");
412 let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
413 if let ty::Opaque(def_id, _) = *output_ty.kind() {
414 output_ty = self.infcx.tcx.type_of(def_id)
417 debug!("report_fnmut_error: output_ty={:?}", output_ty);
419 let message = match output_ty.kind() {
420 ty::Closure(_, _) => {
421 "returns a closure that contains a reference to a captured variable, which then \
422 escapes the closure body"
424 ty::Adt(def, _) if self.infcx.tcx.is_diagnostic_item(sym::gen_future, def.did()) => {
425 "returns an `async` block that contains a reference to a captured variable, which then \
426 escapes the closure body"
428 _ => "returns a reference to a captured variable which escapes the closure body",
431 diag.span_label(*span, message);
433 if let ReturnConstraint::ClosureUpvar(upvar_field) = kind {
434 let def_id = match self.regioncx.universal_regions().defining_ty {
435 DefiningTy::Closure(def_id, _) => def_id,
436 ty => bug!("unexpected DefiningTy {:?}", ty),
439 let captured_place = &self.upvars[upvar_field.index()].place;
440 let defined_hir = match captured_place.place.base {
441 PlaceBase::Local(hirid) => Some(hirid),
442 PlaceBase::Upvar(upvar) => Some(upvar.var_path.hir_id),
446 if defined_hir.is_some() {
447 let upvars_map = self.infcx.tcx.upvars_mentioned(def_id).unwrap();
448 let upvar_def_span = self.infcx.tcx.hir().span(defined_hir.unwrap());
449 let upvar_span = upvars_map.get(&defined_hir.unwrap()).unwrap().span;
450 diag.span_label(upvar_def_span, "variable defined here");
451 diag.span_label(upvar_span, "variable captured here");
455 if let Some(fr_span) = self.give_region_a_name(*outlived_fr).unwrap().span() {
456 diag.span_label(fr_span, "inferred to be a `FnMut` closure");
460 "`FnMut` closures only have access to their captured variables while they are \
463 diag.note("...therefore, they cannot allow references to captured variables to escape");
468 /// Reports an error specifically for when data is escaping a closure.
471 /// error: borrowed data escapes outside of function
472 /// --> $DIR/lifetime-bound-will-change-warning.rs:44:5
474 /// LL | fn test2<'a>(x: &'a Box<Fn()+'a>) {
475 /// | - `x` is a reference that is only valid in the function body
476 /// LL | // but ref_obj will not, so warn.
478 /// | ^^^^^^^^^^ `x` escapes the function body here
480 fn report_escaping_data_error(
482 errci: &ErrorConstraintInfo,
483 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
484 let ErrorConstraintInfo { span, category, .. } = errci;
486 let fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
493 let outlived_fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
501 let (_, escapes_from) = self
504 .article_and_description(self.regioncx.universal_regions().defining_ty.def_id());
506 // Revert to the normal error in these cases.
507 // Assignments aren't "escapes" in function items.
508 if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
509 || (*category == ConstraintCategory::Assignment
510 && self.regioncx.universal_regions().defining_ty.is_fn_def())
511 || self.regioncx.universal_regions().defining_ty.is_const()
513 return self.report_general_error(&ErrorConstraintInfo {
515 outlived_fr_is_local: false,
521 borrowck_errors::borrowed_data_escapes_closure(self.infcx.tcx, *span, escapes_from);
523 if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span {
526 format!("`{outlived_fr_name}` declared here, outside of the {escapes_from} body",),
530 if let Some((Some(fr_name), fr_span)) = fr_name_and_span {
534 "`{fr_name}` is a reference that is only valid in the {escapes_from} body",
538 diag.span_label(*span, format!("`{fr_name}` escapes the {escapes_from} body here"));
541 // Only show an extra note if we can find an 'error region' for both of the region
542 // variables. This avoids showing a noisy note that just mentions 'synthetic' regions
543 // that don't help the user understand the error.
544 if self.to_error_region(errci.fr).is_some()
545 && self.to_error_region(errci.outlived_fr).is_some()
547 let fr_region_name = self.give_region_a_name(errci.fr).unwrap();
548 fr_region_name.highlight_region_name(&mut diag);
549 let outlived_fr_region_name = self.give_region_a_name(errci.outlived_fr).unwrap();
550 outlived_fr_region_name.highlight_region_name(&mut diag);
555 "{}requires that `{}` must outlive `{}`",
556 category.description(),
558 outlived_fr_region_name,
565 /// Reports a region inference error for the general case with named/synthesized lifetimes to
566 /// explain what is happening.
569 /// error: unsatisfied lifetime constraints
570 /// --> $DIR/regions-creating-enums3.rs:17:5
572 /// LL | fn mk_add_bad1<'a,'b>(x: &'a ast<'a>, y: &'b ast<'b>) -> ast<'a> {
573 /// | -- -- lifetime `'b` defined here
575 /// | lifetime `'a` defined here
576 /// LL | ast::add(x, y)
577 /// | ^^^^^^^^^^^^^^ function was supposed to return data with lifetime `'a` but it
578 /// | is returning data with lifetime `'b`
580 fn report_general_error(
582 errci: &ErrorConstraintInfo,
583 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
584 let ErrorConstraintInfo {
588 outlived_fr_is_local,
595 self.infcx.tcx.sess.struct_span_err(*span, "lifetime may not live long enough");
597 let (_, mir_def_name) =
598 self.infcx.tcx.article_and_description(self.mir_def_id().to_def_id());
600 let fr_name = self.give_region_a_name(*fr).unwrap();
601 fr_name.highlight_region_name(&mut diag);
602 let outlived_fr_name = self.give_region_a_name(*outlived_fr).unwrap();
603 outlived_fr_name.highlight_region_name(&mut diag);
605 match (category, outlived_fr_is_local, fr_is_local) {
606 (ConstraintCategory::Return(_), true, _) => {
610 "{mir_def_name} was supposed to return data with lifetime `{outlived_fr_name}` but it is returning \
611 data with lifetime `{fr_name}`",
619 "{}requires that `{}` must outlive `{}`",
620 category.description(),
628 self.add_static_impl_trait_suggestion(&mut diag, *fr, fr_name, *outlived_fr);
633 /// Adds a suggestion to errors where an `impl Trait` is returned.
636 /// help: to allow this `impl Trait` to capture borrowed data with lifetime `'1`, add `'_` as
639 /// LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
640 /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
642 fn add_static_impl_trait_suggestion(
644 diag: &mut Diagnostic,
646 // We need to pass `fr_name` - computing it again will label it twice.
648 outlived_fr: RegionVid,
650 if let (Some(f), Some(ty::ReStatic)) =
651 (self.to_error_region(fr), self.to_error_region(outlived_fr).as_deref())
653 if let Some(&ty::Opaque(did, substs)) = self
656 .is_suitable_region(f)
658 .and_then(|id| self.infcx.tcx.return_type_impl_trait(id))
659 .map(|(ty, _)| ty.kind())
661 // Check whether or not the impl trait return type is intended to capture
662 // data with the static lifetime.
664 // eg. check for `impl Trait + 'static` instead of `impl Trait`.
665 let has_static_predicate = {
666 let bounds = self.infcx.tcx.explicit_item_bounds(did);
668 let mut found = false;
669 for (bound, _) in bounds {
670 if let ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_, r)) =
671 bound.kind().skip_binder()
673 let r = r.subst(self.infcx.tcx, substs);
678 // If there's already a lifetime bound, don't
689 "add_static_impl_trait_suggestion: has_static_predicate={:?}",
692 let static_str = kw::StaticLifetime;
693 // If there is a static predicate, then the only sensible suggestion is to replace
694 // fr with `'static`.
695 if has_static_predicate {
696 diag.help(&format!("consider replacing `{fr_name}` with `{static_str}`"));
698 // Otherwise, we should suggest adding a constraint on the return type.
699 let span = self.infcx.tcx.def_span(did);
700 if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
701 let suggestable_fr_name = if fr_name.was_named() {
706 let span = if snippet.ends_with(';') {
707 // `type X = impl Trait;`
708 span.with_hi(span.hi() - BytePos(1))
712 let suggestion = format!(" + {suggestable_fr_name}");
713 let span = span.shrink_to_hi();
714 diag.span_suggestion(
717 "to allow this `impl Trait` to capture borrowed data with lifetime \
718 `{fr_name}`, add `{suggestable_fr_name}` as a bound",
721 Applicability::MachineApplicable,