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, opaque_types, NLLRegionVariableOrigin,
9 use rustc_span::symbol::kw;
12 use crate::util::borrowck_errors;
14 use crate::borrow_check::{
15 nll::ConstraintDescription,
16 region_infer::{values::RegionElement, TypeTest},
17 universal_regions::DefiningTy,
21 use super::{OutlivesSuggestionBuilder, RegionName, RegionNameSource};
23 impl ConstraintDescription for ConstraintCategory {
24 fn description(&self) -> &'static str {
25 // Must end with a space. Allows for empty names to be provided.
27 ConstraintCategory::Assignment => "assignment ",
28 ConstraintCategory::Return => "returning this value ",
29 ConstraintCategory::Yield => "yielding this value ",
30 ConstraintCategory::UseAsConst => "using this value as a constant ",
31 ConstraintCategory::UseAsStatic => "using this value as a static ",
32 ConstraintCategory::Cast => "cast ",
33 ConstraintCategory::CallArgument => "argument ",
34 ConstraintCategory::TypeAnnotation => "type annotation ",
35 ConstraintCategory::ClosureBounds => "closure body ",
36 ConstraintCategory::SizedBound => "proving this value is `Sized` ",
37 ConstraintCategory::CopyBound => "copying this value ",
38 ConstraintCategory::OpaqueType => "opaque type ",
39 ConstraintCategory::Boring
40 | ConstraintCategory::BoringNoLocation
41 | ConstraintCategory::Internal => "",
46 /// A collection of errors encountered during region inference. This is needed to efficiently
47 /// report errors after borrow checking.
49 /// Usually we expect this to either be empty or contain a small number of items, so we can avoid
50 /// allocation most of the time.
51 crate type RegionErrors<'tcx> = Vec<RegionErrorKind<'tcx>>;
53 #[derive(Clone, Debug)]
54 crate enum RegionErrorKind<'tcx> {
55 /// A generic bound failure for a type test (`T: 'a`).
56 TypeTestError { type_test: TypeTest<'tcx> },
58 /// An unexpected hidden region for an opaque type.
59 UnexpectedHiddenRegion {
60 /// The span for the member constraint.
64 /// The unexpected region.
65 member_region: ty::Region<'tcx>,
68 /// Higher-ranked subtyping error.
69 BoundUniversalRegionError {
70 /// The placeholder free region.
72 /// The region element that erroneously must be outlived by `longer_fr`.
73 error_element: RegionElement,
74 /// The origin of the placeholder region.
75 fr_origin: NLLRegionVariableOrigin,
78 /// Any other lifetime error.
80 /// The origin of the region.
81 fr_origin: NLLRegionVariableOrigin,
82 /// The region that should outlive `shorter_fr`.
84 /// The region that should be shorter, but we can't prove it.
85 shorter_fr: RegionVid,
86 /// Indicates whether this is a reported error. We currently only report the first error
87 /// encountered and leave the rest unreported so as not to overwhelm the user.
92 /// Information about the various region constraints involved in a borrow checker error.
93 #[derive(Clone, Debug)]
94 pub struct ErrorConstraintInfo {
96 pub(super) fr: RegionVid,
97 pub(super) fr_is_local: bool,
98 pub(super) outlived_fr: RegionVid,
99 pub(super) outlived_fr_is_local: bool,
101 // Category and span for best blame constraint
102 pub(super) category: ConstraintCategory,
103 pub(super) span: Span,
106 impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
107 /// Converts a region inference variable into a `ty::Region` that
108 /// we can use for error reporting. If `r` is universally bound,
109 /// then we use the name that we have on record for it. If `r` is
110 /// existentially bound, then we check its inferred value and try
111 /// to find a good name from that. Returns `None` if we can't find
112 /// one (e.g., this is just some random part of the CFG).
113 pub(super) fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
114 self.to_error_region_vid(r).and_then(|r| self.regioncx.region_definition(r).external_name)
117 /// Returns the `RegionVid` corresponding to the region returned by
118 /// `to_error_region`.
119 pub(super) fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
120 if self.regioncx.universal_regions().is_universal_region(r) {
123 let upper_bound = self.regioncx.universal_upper_bound(r);
125 if self.regioncx.upper_bound_in_region_scc(r, upper_bound) {
126 self.to_error_region_vid(upper_bound)
133 /// Returns `true` if a closure is inferred to be an `FnMut` closure.
134 fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
135 if let Some(ty::ReFree(free_region)) = self.to_error_region(fr) {
136 if let ty::BoundRegion::BrEnv = free_region.bound_region {
137 if let DefiningTy::Closure(def_id, substs) =
138 self.regioncx.universal_regions().defining_ty
140 return substs.as_closure().kind(def_id, self.infcx.tcx)
141 == ty::ClosureKind::FnMut;
149 /// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`.
150 pub(in crate::borrow_check) fn report_region_errors(&mut self, nll_errors: RegionErrors<'tcx>) {
151 // Iterate through all the errors, producing a diagnostic for each one. The diagnostics are
152 // buffered in the `MirBorrowckCtxt`.
154 let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
156 for nll_error in nll_errors.into_iter() {
158 RegionErrorKind::TypeTestError { type_test } => {
159 // Try to convert the lower-bound region into something named we can print for the user.
160 let lower_bound_region = self.to_error_region(type_test.lower_bound);
162 let type_test_span = type_test.locations.span(&self.body);
164 if let Some(lower_bound_region) = lower_bound_region {
165 let region_scope_tree = &self.infcx.tcx.region_scope_tree(self.mir_def_id);
167 .construct_generic_bound_failure(
171 type_test.generic_kind,
174 .buffer(&mut self.errors_buffer);
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 univesal 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.
190 &format!("`{}` does not live long enough", type_test.generic_kind),
192 .buffer(&mut self.errors_buffer);
196 RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, member_region } => {
197 let region_scope_tree = &self.infcx.tcx.region_scope_tree(self.mir_def_id);
198 let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
199 let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
200 opaque_types::unexpected_hidden_region_diagnostic(
202 Some(region_scope_tree),
207 .buffer(&mut self.errors_buffer);
210 RegionErrorKind::BoundUniversalRegionError {
215 let error_region = self.regioncx.region_from_element(longer_fr, error_element);
217 // Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
218 let (_, span) = self.regioncx.find_outlives_blame_span(
225 // FIXME: improve this error message
229 .struct_span_err(span, "higher-ranked subtype error")
230 .buffer(&mut self.errors_buffer);
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(in crate::borrow_check) 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 (category, _, span) =
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, span);
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 tables = self.infcx.tcx.typeck_tables_of(self.mir_def_id);
287 let nice = NiceRegionError::new_from_span(self.infcx, span, o, f, Some(tables));
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,