1 //! Error reporting machinery for lifetime errors.
3 use rustc_errors::{Applicability, DiagnosticBuilder};
4 use rustc_infer::infer::{
5 error_reporting::nice_region_error::NiceRegionError,
6 error_reporting::unexpected_hidden_region_diagnostic, NllRegionVariableOrigin,
8 use rustc_middle::mir::{ConstraintCategory, ReturnConstraint};
9 use rustc_middle::ty::subst::Subst;
10 use rustc_middle::ty::{self, RegionVid, Ty};
11 use rustc_span::symbol::{kw, sym};
12 use rustc_span::{BytePos, Span};
14 use crate::borrowck_errors;
16 use super::{OutlivesSuggestionBuilder, RegionName};
17 use crate::region_infer::BlameConstraint;
19 nll::ConstraintDescription,
20 region_infer::{values::RegionElement, TypeTest},
21 universal_regions::DefiningTy,
25 impl ConstraintDescription for ConstraintCategory {
26 fn description(&self) -> &'static str {
27 // Must end with a space. Allows for empty names to be provided.
29 ConstraintCategory::Assignment => "assignment ",
30 ConstraintCategory::Return(_) => "returning this value ",
31 ConstraintCategory::Yield => "yielding this value ",
32 ConstraintCategory::UseAsConst => "using this value as a constant ",
33 ConstraintCategory::UseAsStatic => "using this value as a static ",
34 ConstraintCategory::Cast => "cast ",
35 ConstraintCategory::CallArgument => "argument ",
36 ConstraintCategory::TypeAnnotation => "type annotation ",
37 ConstraintCategory::ClosureBounds => "closure body ",
38 ConstraintCategory::SizedBound => "proving this value is `Sized` ",
39 ConstraintCategory::CopyBound => "copying this value ",
40 ConstraintCategory::OpaqueType => "opaque type ",
41 ConstraintCategory::ClosureUpvar(_) => "closure capture ",
42 ConstraintCategory::Usage => "this usage ",
43 ConstraintCategory::Predicate(_)
44 | ConstraintCategory::Boring
45 | ConstraintCategory::BoringNoLocation
46 | ConstraintCategory::Internal => "",
51 /// A collection of errors encountered during region inference. This is needed to efficiently
52 /// report errors after borrow checking.
54 /// Usually we expect this to either be empty or contain a small number of items, so we can avoid
55 /// allocation most of the time.
56 crate type RegionErrors<'tcx> = Vec<RegionErrorKind<'tcx>>;
58 #[derive(Clone, Debug)]
59 crate enum RegionErrorKind<'tcx> {
60 /// A generic bound failure for a type test (`T: 'a`).
61 TypeTestError { type_test: TypeTest<'tcx> },
63 /// An unexpected hidden region for an opaque type.
64 UnexpectedHiddenRegion {
65 /// The span for the member constraint.
69 /// The unexpected region.
70 member_region: ty::Region<'tcx>,
73 /// Higher-ranked subtyping error.
74 BoundUniversalRegionError {
75 /// The placeholder free region.
77 /// The region element that erroneously must be outlived by `longer_fr`.
78 error_element: RegionElement,
79 /// The placeholder region.
80 placeholder: ty::PlaceholderRegion,
83 /// Any other lifetime error.
85 /// The origin of the region.
86 fr_origin: NllRegionVariableOrigin,
87 /// The region that should outlive `shorter_fr`.
89 /// The region that should be shorter, but we can't prove it.
90 shorter_fr: RegionVid,
91 /// Indicates whether this is a reported error. We currently only report the first error
92 /// encountered and leave the rest unreported so as not to overwhelm the user.
97 /// Information about the various region constraints involved in a borrow checker error.
98 #[derive(Clone, Debug)]
99 pub struct ErrorConstraintInfo {
101 pub(super) fr: RegionVid,
102 pub(super) fr_is_local: bool,
103 pub(super) outlived_fr: RegionVid,
104 pub(super) outlived_fr_is_local: bool,
106 // Category and span for best blame constraint
107 pub(super) category: ConstraintCategory,
108 pub(super) span: Span,
111 impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
112 /// Converts a region inference variable into a `ty::Region` that
113 /// we can use for error reporting. If `r` is universally bound,
114 /// then we use the name that we have on record for it. If `r` is
115 /// existentially bound, then we check its inferred value and try
116 /// to find a good name from that. Returns `None` if we can't find
117 /// one (e.g., this is just some random part of the CFG).
118 pub(super) fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
119 self.to_error_region_vid(r).and_then(|r| self.regioncx.region_definition(r).external_name)
122 /// Returns the `RegionVid` corresponding to the region returned by
123 /// `to_error_region`.
124 pub(super) fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
125 if self.regioncx.universal_regions().is_universal_region(r) {
128 // We just want something nameable, even if it's not
129 // actually an upper bound.
130 let upper_bound = self.regioncx.approx_universal_upper_bound(r);
132 if self.regioncx.upper_bound_in_region_scc(r, upper_bound) {
133 self.to_error_region_vid(upper_bound)
140 /// Returns `true` if a closure is inferred to be an `FnMut` closure.
141 fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
142 if let Some(ty::ReFree(free_region)) = self.to_error_region(fr) {
143 if let ty::BoundRegionKind::BrEnv = free_region.bound_region {
144 if let DefiningTy::Closure(_, substs) =
145 self.regioncx.universal_regions().defining_ty
147 return substs.as_closure().kind() == ty::ClosureKind::FnMut;
155 /// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`.
156 pub(crate) fn report_region_errors(&mut self, nll_errors: RegionErrors<'tcx>) {
157 // Iterate through all the errors, producing a diagnostic for each one. The diagnostics are
158 // buffered in the `MirBorrowckCtxt`.
160 let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
162 for nll_error in nll_errors.into_iter() {
164 RegionErrorKind::TypeTestError { type_test } => {
165 // Try to convert the lower-bound region into something named we can print for the user.
166 let lower_bound_region = self.to_error_region(type_test.lower_bound);
168 let type_test_span = type_test.locations.span(&self.body);
170 if let Some(lower_bound_region) = lower_bound_region {
172 .construct_generic_bound_failure(
175 type_test.generic_kind,
178 .buffer(&mut self.errors_buffer);
180 // FIXME. We should handle this case better. It
181 // indicates that we have e.g., some region variable
182 // whose value is like `'a+'b` where `'a` and `'b` are
183 // distinct unrelated univesal regions that are not
184 // known to outlive one another. It'd be nice to have
185 // some examples where this arises to decide how best
186 // to report it; we could probably handle it by
187 // iterating over the universal regions and reporting
188 // an error that multiple bounds are required.
194 &format!("`{}` does not live long enough", type_test.generic_kind),
196 .buffer(&mut self.errors_buffer);
200 RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, member_region } => {
201 let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
202 let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
203 unexpected_hidden_region_diagnostic(
209 .buffer(&mut self.errors_buffer);
212 RegionErrorKind::BoundUniversalRegionError {
217 let error_vid = self.regioncx.region_from_element(longer_fr, &error_element);
219 // Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
220 let (_, cause) = self.regioncx.find_outlives_blame_span(
223 NllRegionVariableOrigin::Placeholder(placeholder),
227 let universe = placeholder.universe;
228 let universe_info = self.regioncx.universe_info(universe);
230 universe_info.report_error(self, placeholder, error_element, cause);
233 RegionErrorKind::RegionError { fr_origin, longer_fr, shorter_fr, is_reported } => {
235 self.report_region_error(
239 &mut outlives_suggestion,
242 // We only report the first error, so as not to overwhelm the user. See
243 // `RegRegionErrorKind` docs.
245 // FIXME: currently we do nothing with these, but perhaps we can do better?
246 // FIXME: try collecting these constraints on the outlives suggestion
247 // builder. Does it make the suggestions any better?
249 "Unreported region error: can't prove that {:?}: {:?}",
250 longer_fr, shorter_fr
257 // Emit one outlives suggestions for each MIR def we borrowck
258 outlives_suggestion.add_suggestion(self);
261 /// Report an error because the universal region `fr` was required to outlive
262 /// `outlived_fr` but it is not known to do so. For example:
265 /// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
268 /// Here we would be invoked with `fr = 'a` and `outlived_fr = `'b`.
269 pub(crate) fn report_region_error(
272 fr_origin: NllRegionVariableOrigin,
273 outlived_fr: RegionVid,
274 outlives_suggestion: &mut OutlivesSuggestionBuilder,
276 debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
278 let BlameConstraint { category, cause, variance_info, from_closure: _ } =
279 self.regioncx.best_blame_constraint(&self.body, fr, fr_origin, |r| {
280 self.regioncx.provides_universal_region(r, fr, outlived_fr)
283 debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info);
284 // Check if we can use one of the "nice region errors".
285 if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
286 let nice = NiceRegionError::new_from_span(self.infcx, cause.span, o, f);
287 if let Some(diag) = nice.try_report_from_nll() {
288 diag.buffer(&mut self.errors_buffer);
293 let (fr_is_local, outlived_fr_is_local): (bool, bool) = (
294 self.regioncx.universal_regions().is_local_free_region(fr),
295 self.regioncx.universal_regions().is_local_free_region(outlived_fr),
299 "report_region_error: fr_is_local={:?} outlived_fr_is_local={:?} category={:?}",
300 fr_is_local, outlived_fr_is_local, category
303 let errci = ErrorConstraintInfo {
307 outlived_fr_is_local,
312 let mut diag = match (category, fr_is_local, outlived_fr_is_local) {
313 (ConstraintCategory::Return(kind), true, false) if self.is_closure_fn_mut(fr) => {
314 self.report_fnmut_error(&errci, kind)
316 (ConstraintCategory::Assignment, true, false)
317 | (ConstraintCategory::CallArgument, true, false) => {
318 let mut db = self.report_escaping_data_error(&errci);
320 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
321 outlives_suggestion.collect_constraint(fr, outlived_fr);
326 let mut db = self.report_general_error(&errci);
328 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
329 outlives_suggestion.collect_constraint(fr, outlived_fr);
335 match variance_info {
336 ty::VarianceDiagInfo::None => {}
337 ty::VarianceDiagInfo::Mut { kind, ty } => {
338 let kind_name = match kind {
339 ty::VarianceDiagMutKind::Ref => "reference",
340 ty::VarianceDiagMutKind::RawPtr => "pointer",
342 diag.note(&format!("requirement occurs because of a mutable {kind_name} to {ty}",));
343 diag.note(&format!("mutable {kind_name}s are invariant over their type parameter"));
344 diag.help("see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance");
348 diag.buffer(&mut self.errors_buffer);
351 /// Report a specialized error when `FnMut` closures return a reference to a captured variable.
352 /// This function expects `fr` to be local and `outlived_fr` to not be local.
355 /// error: captured variable cannot escape `FnMut` closure body
356 /// --> $DIR/issue-53040.rs:15:8
359 /// | -- ^^^^^^ creates a reference to a captured variable which escapes the closure body
361 /// | inferred to be a `FnMut` closure
363 /// = note: `FnMut` closures only have access to their captured variables while they are
365 /// = note: ...therefore, returned references to captured variables will escape the closure
367 fn report_fnmut_error(
369 errci: &ErrorConstraintInfo,
370 kind: ReturnConstraint,
371 ) -> DiagnosticBuilder<'tcx> {
372 let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
378 .struct_span_err(*span, "captured variable cannot escape `FnMut` closure body");
380 let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
381 if let ty::Opaque(def_id, _) = *output_ty.kind() {
382 output_ty = self.infcx.tcx.type_of(def_id)
385 debug!("report_fnmut_error: output_ty={:?}", output_ty);
387 let message = match output_ty.kind() {
388 ty::Closure(_, _) => {
389 "returns a closure that contains a reference to a captured variable, which then \
390 escapes the closure body"
392 ty::Adt(def, _) if self.infcx.tcx.is_diagnostic_item(sym::gen_future, def.did) => {
393 "returns an `async` block that contains a reference to a captured variable, which then \
394 escapes the closure body"
396 _ => "returns a reference to a captured variable which escapes the closure body",
399 diag.span_label(*span, message);
401 // FIXME(project-rfc-2229#48): This should store a captured_place not a hir id
402 if let ReturnConstraint::ClosureUpvar(upvar) = kind {
403 let def_id = match self.regioncx.universal_regions().defining_ty {
404 DefiningTy::Closure(def_id, _) => def_id,
405 ty => bug!("unexpected DefiningTy {:?}", ty),
408 let upvar_def_span = self.infcx.tcx.hir().span(upvar);
409 let upvar_span = self.infcx.tcx.upvars_mentioned(def_id).unwrap()[&upvar].span;
410 diag.span_label(upvar_def_span, "variable defined here");
411 diag.span_label(upvar_span, "variable captured here");
414 if let Some(fr_span) = self.give_region_a_name(*outlived_fr).unwrap().span() {
415 diag.span_label(fr_span, "inferred to be a `FnMut` closure");
419 "`FnMut` closures only have access to their captured variables while they are \
422 diag.note("...therefore, they cannot allow references to captured variables to escape");
427 /// Reports an error specifically for when data is escaping a closure.
430 /// error: borrowed data escapes outside of function
431 /// --> $DIR/lifetime-bound-will-change-warning.rs:44:5
433 /// LL | fn test2<'a>(x: &'a Box<Fn()+'a>) {
434 /// | - `x` is a reference that is only valid in the function body
435 /// LL | // but ref_obj will not, so warn.
437 /// | ^^^^^^^^^^ `x` escapes the function body here
439 fn report_escaping_data_error(&self, errci: &ErrorConstraintInfo) -> DiagnosticBuilder<'tcx> {
440 let ErrorConstraintInfo { span, category, .. } = errci;
442 let fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
449 let outlived_fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
457 let (_, escapes_from) = self
460 .article_and_description(self.regioncx.universal_regions().defining_ty.def_id());
462 // Revert to the normal error in these cases.
463 // Assignments aren't "escapes" in function items.
464 if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
465 || (*category == ConstraintCategory::Assignment
466 && self.regioncx.universal_regions().defining_ty.is_fn_def())
467 || self.regioncx.universal_regions().defining_ty.is_const()
469 return self.report_general_error(&ErrorConstraintInfo {
471 outlived_fr_is_local: false,
477 borrowck_errors::borrowed_data_escapes_closure(self.infcx.tcx, *span, escapes_from);
479 if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span {
483 "`{}` declared here, outside of the {} body",
484 outlived_fr_name, escapes_from
489 if let Some((Some(fr_name), fr_span)) = fr_name_and_span {
493 "`{}` is a reference that is only valid in the {} body",
494 fr_name, escapes_from
498 diag.span_label(*span, format!("`{}` escapes the {} body here", fr_name, escapes_from));
501 // Only show an extra note if we can find an 'error region' for both of the region
502 // variables. This avoids showing a noisy note that just mentions 'synthetic' regions
503 // that don't help the user understand the error.
504 if self.to_error_region(errci.fr).is_some()
505 && self.to_error_region(errci.outlived_fr).is_some()
507 let fr_region_name = self.give_region_a_name(errci.fr).unwrap();
508 fr_region_name.highlight_region_name(&mut diag);
509 let outlived_fr_region_name = self.give_region_a_name(errci.outlived_fr).unwrap();
510 outlived_fr_region_name.highlight_region_name(&mut diag);
515 "{}requires that `{}` must outlive `{}`",
516 category.description(),
518 outlived_fr_region_name,
525 /// Reports a region inference error for the general case with named/synthesized lifetimes to
526 /// explain what is happening.
529 /// error: unsatisfied lifetime constraints
530 /// --> $DIR/regions-creating-enums3.rs:17:5
532 /// LL | fn mk_add_bad1<'a,'b>(x: &'a ast<'a>, y: &'b ast<'b>) -> ast<'a> {
533 /// | -- -- lifetime `'b` defined here
535 /// | lifetime `'a` defined here
536 /// LL | ast::add(x, y)
537 /// | ^^^^^^^^^^^^^^ function was supposed to return data with lifetime `'a` but it
538 /// | is returning data with lifetime `'b`
540 fn report_general_error(&self, errci: &ErrorConstraintInfo) -> DiagnosticBuilder<'tcx> {
541 let ErrorConstraintInfo {
545 outlived_fr_is_local,
552 self.infcx.tcx.sess.struct_span_err(*span, "lifetime may not live long enough");
554 let (_, mir_def_name) =
555 self.infcx.tcx.article_and_description(self.mir_def_id().to_def_id());
557 let fr_name = self.give_region_a_name(*fr).unwrap();
558 fr_name.highlight_region_name(&mut diag);
559 let outlived_fr_name = self.give_region_a_name(*outlived_fr).unwrap();
560 outlived_fr_name.highlight_region_name(&mut diag);
562 match (category, outlived_fr_is_local, fr_is_local) {
563 (ConstraintCategory::Return(_), true, _) => {
567 "{} was supposed to return data with lifetime `{}` but it is returning \
568 data with lifetime `{}`",
569 mir_def_name, outlived_fr_name, fr_name
577 "{}requires that `{}` must outlive `{}`",
578 category.description(),
586 self.add_static_impl_trait_suggestion(&mut diag, *fr, fr_name, *outlived_fr);
591 /// Adds a suggestion to errors where an `impl Trait` is returned.
594 /// help: to allow this `impl Trait` to capture borrowed data with lifetime `'1`, add `'_` as
597 /// LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
598 /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
600 fn add_static_impl_trait_suggestion(
602 diag: &mut DiagnosticBuilder<'tcx>,
604 // We need to pass `fr_name` - computing it again will label it twice.
606 outlived_fr: RegionVid,
608 if let (Some(f), Some(ty::RegionKind::ReStatic)) =
609 (self.to_error_region(fr), self.to_error_region(outlived_fr))
611 if let Some(&ty::Opaque(did, substs)) = self
614 .is_suitable_region(f)
616 .and_then(|id| self.infcx.tcx.return_type_impl_trait(id))
617 .map(|(ty, _)| ty.kind())
619 // Check whether or not the impl trait return type is intended to capture
620 // data with the static lifetime.
622 // eg. check for `impl Trait + 'static` instead of `impl Trait`.
623 let has_static_predicate = {
624 let bounds = self.infcx.tcx.explicit_item_bounds(did);
626 let mut found = false;
627 for (bound, _) in bounds {
628 if let ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(_, r)) =
629 bound.kind().skip_binder()
631 let r = r.subst(self.infcx.tcx, substs);
632 if let ty::RegionKind::ReStatic = r {
636 // If there's already a lifetime bound, don't
647 "add_static_impl_trait_suggestion: has_static_predicate={:?}",
650 let static_str = kw::StaticLifetime;
651 // If there is a static predicate, then the only sensible suggestion is to replace
652 // fr with `'static`.
653 if has_static_predicate {
654 diag.help(&format!("consider replacing `{}` with `{}`", fr_name, static_str));
656 // Otherwise, we should suggest adding a constraint on the return type.
657 let span = self.infcx.tcx.def_span(did);
658 if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
659 let suggestable_fr_name = if fr_name.was_named() {
664 let span = if snippet.ends_with(';') {
665 // `type X = impl Trait;`
666 span.with_hi(span.hi() - BytePos(1))
670 let suggestion = format!(" + {}", suggestable_fr_name);
671 let span = span.shrink_to_hi();
672 diag.span_suggestion(
675 "to allow this `impl Trait` to capture borrowed data with lifetime \
676 `{}`, add `{}` as a bound",
677 fr_name, suggestable_fr_name,
680 Applicability::MachineApplicable,