1 //! Error reporting machinery for lifetime errors.
3 use rustc::mir::ConstraintCategory;
4 use rustc::ty::{self, RegionVid, Ty};
5 use rustc_errors::{Applicability, DiagnosticBuilder};
6 use rustc_infer::infer::{
7 error_reporting::nice_region_error::NiceRegionError,
8 error_reporting::unexpected_hidden_region_diagnostic, NLLRegionVariableOrigin,
10 use rustc_span::symbol::kw;
13 use crate::util::borrowck_errors;
15 use crate::borrow_check::{
16 nll::ConstraintDescription,
17 region_infer::{values::RegionElement, TypeTest},
18 universal_regions::DefiningTy,
22 use super::{OutlivesSuggestionBuilder, RegionName, RegionNameSource};
24 impl ConstraintDescription for ConstraintCategory {
25 fn description(&self) -> &'static str {
26 // Must end with a space. Allows for empty names to be provided.
28 ConstraintCategory::Assignment => "assignment ",
29 ConstraintCategory::Return => "returning this value ",
30 ConstraintCategory::Yield => "yielding this value ",
31 ConstraintCategory::UseAsConst => "using this value as a constant ",
32 ConstraintCategory::UseAsStatic => "using this value as a static ",
33 ConstraintCategory::Cast => "cast ",
34 ConstraintCategory::CallArgument => "argument ",
35 ConstraintCategory::TypeAnnotation => "type annotation ",
36 ConstraintCategory::ClosureBounds => "closure body ",
37 ConstraintCategory::SizedBound => "proving this value is `Sized` ",
38 ConstraintCategory::CopyBound => "copying this value ",
39 ConstraintCategory::OpaqueType => "opaque type ",
40 ConstraintCategory::Boring
41 | ConstraintCategory::BoringNoLocation
42 | ConstraintCategory::Internal => "",
47 /// A collection of errors encountered during region inference. This is needed to efficiently
48 /// report errors after borrow checking.
50 /// Usually we expect this to either be empty or contain a small number of items, so we can avoid
51 /// allocation most of the time.
52 crate type RegionErrors<'tcx> = Vec<RegionErrorKind<'tcx>>;
54 #[derive(Clone, Debug)]
55 crate enum RegionErrorKind<'tcx> {
56 /// A generic bound failure for a type test (`T: 'a`).
57 TypeTestError { type_test: TypeTest<'tcx> },
59 /// An unexpected hidden region for an opaque type.
60 UnexpectedHiddenRegion {
61 /// The span for the member constraint.
65 /// The unexpected region.
66 member_region: ty::Region<'tcx>,
69 /// Higher-ranked subtyping error.
70 BoundUniversalRegionError {
71 /// The placeholder free region.
73 /// The region element that erroneously must be outlived by `longer_fr`.
74 error_element: RegionElement,
75 /// The origin of the placeholder region.
76 fr_origin: NLLRegionVariableOrigin,
79 /// Any other lifetime error.
81 /// The origin of the region.
82 fr_origin: NLLRegionVariableOrigin,
83 /// The region that should outlive `shorter_fr`.
85 /// The region that should be shorter, but we can't prove it.
86 shorter_fr: RegionVid,
87 /// Indicates whether this is a reported error. We currently only report the first error
88 /// encountered and leave the rest unreported so as not to overwhelm the user.
93 /// Information about the various region constraints involved in a borrow checker error.
94 #[derive(Clone, Debug)]
95 pub struct ErrorConstraintInfo {
97 pub(super) fr: RegionVid,
98 pub(super) fr_is_local: bool,
99 pub(super) outlived_fr: RegionVid,
100 pub(super) outlived_fr_is_local: bool,
102 // Category and span for best blame constraint
103 pub(super) category: ConstraintCategory,
104 pub(super) span: Span,
107 impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
108 /// Converts a region inference variable into a `ty::Region` that
109 /// we can use for error reporting. If `r` is universally bound,
110 /// then we use the name that we have on record for it. If `r` is
111 /// existentially bound, then we check its inferred value and try
112 /// to find a good name from that. Returns `None` if we can't find
113 /// one (e.g., this is just some random part of the CFG).
114 pub(super) fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
115 self.to_error_region_vid(r).and_then(|r| self.regioncx.region_definition(r).external_name)
118 /// Returns the `RegionVid` corresponding to the region returned by
119 /// `to_error_region`.
120 pub(super) fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
121 if self.regioncx.universal_regions().is_universal_region(r) {
124 let upper_bound = self.regioncx.universal_upper_bound(r);
126 if self.regioncx.upper_bound_in_region_scc(r, upper_bound) {
127 self.to_error_region_vid(upper_bound)
134 /// Returns `true` if a closure is inferred to be an `FnMut` closure.
135 fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
136 if let Some(ty::ReFree(free_region)) = self.to_error_region(fr) {
137 if let ty::BoundRegion::BrEnv = free_region.bound_region {
138 if let DefiningTy::Closure(def_id, substs) =
139 self.regioncx.universal_regions().defining_ty
141 return substs.as_closure().kind(def_id, self.infcx.tcx)
142 == ty::ClosureKind::FnMut;
150 /// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`.
151 pub(in crate::borrow_check) fn report_region_errors(&mut self, nll_errors: RegionErrors<'tcx>) {
152 // Iterate through all the errors, producing a diagnostic for each one. The diagnostics are
153 // buffered in the `MirBorrowckCtxt`.
155 let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
157 for nll_error in nll_errors.into_iter() {
159 RegionErrorKind::TypeTestError { type_test } => {
160 // Try to convert the lower-bound region into something named we can print for the user.
161 let lower_bound_region = self.to_error_region(type_test.lower_bound);
163 let type_test_span = type_test.locations.span(&self.body);
165 if let Some(lower_bound_region) = lower_bound_region {
166 let region_scope_tree = &self.infcx.tcx.region_scope_tree(self.mir_def_id);
168 .construct_generic_bound_failure(
172 type_test.generic_kind,
175 .buffer(&mut self.errors_buffer);
177 // FIXME. We should handle this case better. It
178 // indicates that we have e.g., some region variable
179 // whose value is like `'a+'b` where `'a` and `'b` are
180 // distinct unrelated univesal regions that are not
181 // known to outlive one another. It'd be nice to have
182 // some examples where this arises to decide how best
183 // to report it; we could probably handle it by
184 // iterating over the universal regions and reporting
185 // an error that multiple bounds are required.
191 &format!("`{}` does not live long enough", type_test.generic_kind),
193 .buffer(&mut self.errors_buffer);
197 RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, member_region } => {
198 let region_scope_tree = &self.infcx.tcx.region_scope_tree(self.mir_def_id);
199 let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
200 let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
201 unexpected_hidden_region_diagnostic(
203 Some(region_scope_tree),
208 .buffer(&mut self.errors_buffer);
211 RegionErrorKind::BoundUniversalRegionError {
216 let error_region = self.regioncx.region_from_element(longer_fr, error_element);
218 // Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
219 let (_, span) = self.regioncx.find_outlives_blame_span(
226 // FIXME: improve this error message
230 .struct_span_err(span, "higher-ranked subtype error")
231 .buffer(&mut self.errors_buffer);
234 RegionErrorKind::RegionError { fr_origin, longer_fr, shorter_fr, is_reported } => {
236 self.report_region_error(
240 &mut outlives_suggestion,
243 // We only report the first error, so as not to overwhelm the user. See
244 // `RegRegionErrorKind` docs.
246 // FIXME: currently we do nothing with these, but perhaps we can do better?
247 // FIXME: try collecting these constraints on the outlives suggestion
248 // builder. Does it make the suggestions any better?
250 "Unreported region error: can't prove that {:?}: {:?}",
251 longer_fr, shorter_fr
258 // Emit one outlives suggestions for each MIR def we borrowck
259 outlives_suggestion.add_suggestion(self);
262 /// Report an error because the universal region `fr` was required to outlive
263 /// `outlived_fr` but it is not known to do so. For example:
266 /// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
269 /// Here we would be invoked with `fr = 'a` and `outlived_fr = `'b`.
270 pub(in crate::borrow_check) fn report_region_error(
273 fr_origin: NLLRegionVariableOrigin,
274 outlived_fr: RegionVid,
275 outlives_suggestion: &mut OutlivesSuggestionBuilder,
277 debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
279 let (category, _, span) =
280 self.regioncx.best_blame_constraint(&self.body, fr, fr_origin, |r| {
281 self.regioncx.provides_universal_region(r, fr, outlived_fr)
284 debug!("report_region_error: category={:?} {:?}", category, span);
285 // Check if we can use one of the "nice region errors".
286 if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
287 let nice = NiceRegionError::new_from_span(self.infcx, span, o, f);
288 if let Some(diag) = nice.try_report_from_nll() {
289 diag.buffer(&mut self.errors_buffer);
294 let (fr_is_local, outlived_fr_is_local): (bool, bool) = (
295 self.regioncx.universal_regions().is_local_free_region(fr),
296 self.regioncx.universal_regions().is_local_free_region(outlived_fr),
300 "report_region_error: fr_is_local={:?} outlived_fr_is_local={:?} category={:?}",
301 fr_is_local, outlived_fr_is_local, category
304 let errci = ErrorConstraintInfo {
308 outlived_fr_is_local,
313 let diag = match (category, fr_is_local, outlived_fr_is_local) {
314 (ConstraintCategory::Return, true, false) if self.is_closure_fn_mut(fr) => {
315 self.report_fnmut_error(&errci)
317 (ConstraintCategory::Assignment, true, false)
318 | (ConstraintCategory::CallArgument, true, false) => {
319 let mut db = self.report_escaping_data_error(&errci);
321 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
322 outlives_suggestion.collect_constraint(fr, outlived_fr);
327 let mut db = self.report_general_error(&errci);
329 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
330 outlives_suggestion.collect_constraint(fr, outlived_fr);
336 diag.buffer(&mut self.errors_buffer);
339 /// Report a specialized error when `FnMut` closures return a reference to a captured variable.
340 /// This function expects `fr` to be local and `outlived_fr` to not be local.
343 /// error: captured variable cannot escape `FnMut` closure body
344 /// --> $DIR/issue-53040.rs:15:8
347 /// | -- ^^^^^^ creates a reference to a captured variable which escapes the closure body
349 /// | inferred to be a `FnMut` closure
351 /// = note: `FnMut` closures only have access to their captured variables while they are
353 /// = note: ...therefore, returned references to captured variables will escape the closure
355 fn report_fnmut_error(&self, errci: &ErrorConstraintInfo) -> DiagnosticBuilder<'tcx> {
356 let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
362 .struct_span_err(*span, "captured variable cannot escape `FnMut` closure body");
364 // We should check if the return type of this closure is in fact a closure - in that
365 // case, we can special case the error further.
366 let return_type_is_closure =
367 self.regioncx.universal_regions().unnormalized_output_ty.is_closure();
368 let message = if return_type_is_closure {
369 "returns a closure that contains a reference to a captured variable, which then \
370 escapes the closure body"
372 "returns a reference to a captured variable which escapes the closure body"
375 diag.span_label(*span, message);
377 match self.give_region_a_name(*outlived_fr).unwrap().source {
378 RegionNameSource::NamedEarlyBoundRegion(fr_span)
379 | RegionNameSource::NamedFreeRegion(fr_span)
380 | RegionNameSource::SynthesizedFreeEnvRegion(fr_span, _)
381 | RegionNameSource::CannotMatchHirTy(fr_span, _)
382 | RegionNameSource::MatchedHirTy(fr_span)
383 | RegionNameSource::MatchedAdtAndSegment(fr_span)
384 | RegionNameSource::AnonRegionFromUpvar(fr_span, _)
385 | RegionNameSource::AnonRegionFromOutput(fr_span, _, _) => {
386 diag.span_label(fr_span, "inferred to be a `FnMut` closure");
392 "`FnMut` closures only have access to their captured variables while they are \
395 diag.note("...therefore, they cannot allow references to captured variables to escape");
400 /// Reports a error specifically for when data is escaping a closure.
403 /// error: borrowed data escapes outside of function
404 /// --> $DIR/lifetime-bound-will-change-warning.rs:44:5
406 /// LL | fn test2<'a>(x: &'a Box<Fn()+'a>) {
407 /// | - `x` is a reference that is only valid in the function body
408 /// LL | // but ref_obj will not, so warn.
410 /// | ^^^^^^^^^^ `x` escapes the function body here
412 fn report_escaping_data_error(&self, errci: &ErrorConstraintInfo) -> DiagnosticBuilder<'tcx> {
413 let ErrorConstraintInfo { span, category, .. } = errci;
415 let fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
422 let outlived_fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
430 let (_, escapes_from) = self
433 .article_and_description(self.regioncx.universal_regions().defining_ty.def_id());
435 // Revert to the normal error in these cases.
436 // Assignments aren't "escapes" in function items.
437 if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
438 || (*category == ConstraintCategory::Assignment
439 && self.regioncx.universal_regions().defining_ty.is_fn_def())
440 || self.regioncx.universal_regions().defining_ty.is_const()
442 return self.report_general_error(&ErrorConstraintInfo {
444 outlived_fr_is_local: false,
450 borrowck_errors::borrowed_data_escapes_closure(self.infcx.tcx, *span, escapes_from);
452 if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span {
456 "`{}` declared here, outside of the {} body",
457 outlived_fr_name, escapes_from
462 if let Some((Some(fr_name), fr_span)) = fr_name_and_span {
466 "`{}` is a reference that is only valid in the {} body",
467 fr_name, escapes_from
471 diag.span_label(*span, format!("`{}` escapes the {} body here", fr_name, escapes_from));
477 /// Reports a region inference error for the general case with named/synthesized lifetimes to
478 /// explain what is happening.
481 /// error: unsatisfied lifetime constraints
482 /// --> $DIR/regions-creating-enums3.rs:17:5
484 /// LL | fn mk_add_bad1<'a,'b>(x: &'a ast<'a>, y: &'b ast<'b>) -> ast<'a> {
485 /// | -- -- lifetime `'b` defined here
487 /// | lifetime `'a` defined here
488 /// LL | ast::add(x, y)
489 /// | ^^^^^^^^^^^^^^ function was supposed to return data with lifetime `'a` but it
490 /// | is returning data with lifetime `'b`
492 fn report_general_error(&self, errci: &ErrorConstraintInfo) -> DiagnosticBuilder<'tcx> {
493 let ErrorConstraintInfo {
497 outlived_fr_is_local,
504 self.infcx.tcx.sess.struct_span_err(*span, "lifetime may not live long enough");
506 let (_, mir_def_name) = self.infcx.tcx.article_and_description(self.mir_def_id);
508 let fr_name = self.give_region_a_name(*fr).unwrap();
509 fr_name.highlight_region_name(&mut diag);
510 let outlived_fr_name = self.give_region_a_name(*outlived_fr).unwrap();
511 outlived_fr_name.highlight_region_name(&mut diag);
513 match (category, outlived_fr_is_local, fr_is_local) {
514 (ConstraintCategory::Return, true, _) => {
518 "{} was supposed to return data with lifetime `{}` but it is returning \
519 data with lifetime `{}`",
520 mir_def_name, outlived_fr_name, fr_name
528 "{}requires that `{}` must outlive `{}`",
529 category.description(),
537 self.add_static_impl_trait_suggestion(&mut diag, *fr, fr_name, *outlived_fr);
542 /// Adds a suggestion to errors where a `impl Trait` is returned.
545 /// help: to allow this `impl Trait` to capture borrowed data with lifetime `'1`, add `'_` as
548 /// LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
549 /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
551 fn add_static_impl_trait_suggestion(
553 diag: &mut DiagnosticBuilder<'tcx>,
555 // We need to pass `fr_name` - computing it again will label it twice.
557 outlived_fr: RegionVid,
559 if let (Some(f), Some(ty::RegionKind::ReStatic)) =
560 (self.to_error_region(fr), self.to_error_region(outlived_fr))
562 if let Some((ty::TyS { kind: ty::Opaque(did, substs), .. }, _)) = self
565 .is_suitable_region(f)
567 .map(|id| self.infcx.tcx.return_type_impl_trait(id))
570 // Check whether or not the impl trait return type is intended to capture
571 // data with the static lifetime.
573 // eg. check for `impl Trait + 'static` instead of `impl Trait`.
574 let has_static_predicate = {
575 let predicates_of = self.infcx.tcx.predicates_of(*did);
576 let bounds = predicates_of.instantiate(self.infcx.tcx, substs);
578 let mut found = false;
579 for predicate in bounds.predicates {
580 if let ty::Predicate::TypeOutlives(binder) = predicate {
581 if let ty::OutlivesPredicate(_, ty::RegionKind::ReStatic) =
587 // If there's already a lifetime bound, don't
598 "add_static_impl_trait_suggestion: has_static_predicate={:?}",
601 let static_str = kw::StaticLifetime;
602 // If there is a static predicate, then the only sensible suggestion is to replace
603 // fr with `'static`.
604 if has_static_predicate {
605 diag.help(&format!("consider replacing `{}` with `{}`", fr_name, static_str));
607 // Otherwise, we should suggest adding a constraint on the return type.
608 let span = self.infcx.tcx.def_span(*did);
609 if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
610 let suggestable_fr_name = if fr_name.was_named() {
615 let suggestion = if snippet.ends_with(';') {
616 // `type X = impl Trait;`
617 format!("{} + {};", &snippet[..snippet.len() - 1], suggestable_fr_name)
619 format!("{} + {}", snippet, suggestable_fr_name)
621 diag.span_suggestion(
624 "to allow this `impl Trait` to capture borrowed data with lifetime \
625 `{}`, add `{}` as a bound",
626 fr_name, suggestable_fr_name,
629 Applicability::MachineApplicable,