]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/borrow_check/diagnostics/region_errors.rs
Rollup merge of #70038 - DutchGhost:const-forget-tests, r=RalfJung
[rust.git] / src / librustc_mir / borrow_check / diagnostics / region_errors.rs
1 //! Error reporting machinery for lifetime errors.
2
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,
9 };
10 use rustc_span::symbol::kw;
11 use rustc_span::Span;
12
13 use crate::util::borrowck_errors;
14
15 use crate::borrow_check::{
16     nll::ConstraintDescription,
17     region_infer::{values::RegionElement, TypeTest},
18     universal_regions::DefiningTy,
19     MirBorrowckCtxt,
20 };
21
22 use super::{OutlivesSuggestionBuilder, RegionName, RegionNameSource};
23
24 impl ConstraintDescription for ConstraintCategory {
25     fn description(&self) -> &'static str {
26         // Must end with a space. Allows for empty names to be provided.
27         match self {
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 => "",
43         }
44     }
45 }
46
47 /// A collection of errors encountered during region inference. This is needed to efficiently
48 /// report errors after borrow checking.
49 ///
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>>;
53
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> },
58
59     /// An unexpected hidden region for an opaque type.
60     UnexpectedHiddenRegion {
61         /// The span for the member constraint.
62         span: Span,
63         /// The hidden type.
64         hidden_ty: Ty<'tcx>,
65         /// The unexpected region.
66         member_region: ty::Region<'tcx>,
67     },
68
69     /// Higher-ranked subtyping error.
70     BoundUniversalRegionError {
71         /// The placeholder free region.
72         longer_fr: RegionVid,
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,
77     },
78
79     /// Any other lifetime error.
80     RegionError {
81         /// The origin of the region.
82         fr_origin: NLLRegionVariableOrigin,
83         /// The region that should outlive `shorter_fr`.
84         longer_fr: RegionVid,
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.
89         is_reported: bool,
90     },
91 }
92
93 /// Information about the various region constraints involved in a borrow checker error.
94 #[derive(Clone, Debug)]
95 pub struct ErrorConstraintInfo {
96     // fr: outlived_fr
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,
101
102     // Category and span for best blame constraint
103     pub(super) category: ConstraintCategory,
104     pub(super) span: Span,
105 }
106
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)
116     }
117
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) {
122             Some(r)
123         } else {
124             let upper_bound = self.regioncx.universal_upper_bound(r);
125
126             if self.regioncx.upper_bound_in_region_scc(r, upper_bound) {
127                 self.to_error_region_vid(upper_bound)
128             } else {
129                 None
130             }
131         }
132     }
133
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
140                 {
141                     return substs.as_closure().kind(def_id, self.infcx.tcx)
142                         == ty::ClosureKind::FnMut;
143                 }
144             }
145         }
146
147         false
148     }
149
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`.
154
155         let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
156
157         for nll_error in nll_errors.into_iter() {
158             match nll_error {
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);
162
163                     let type_test_span = type_test.locations.span(&self.body);
164
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);
167                         self.infcx
168                             .construct_generic_bound_failure(
169                                 region_scope_tree,
170                                 type_test_span,
171                                 None,
172                                 type_test.generic_kind,
173                                 lower_bound_region,
174                             )
175                             .buffer(&mut self.errors_buffer);
176                     } else {
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.
186                         self.infcx
187                             .tcx
188                             .sess
189                             .struct_span_err(
190                                 type_test_span,
191                                 &format!("`{}` does not live long enough", type_test.generic_kind),
192                             )
193                             .buffer(&mut self.errors_buffer);
194                     }
195                 }
196
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(
202                         self.infcx.tcx,
203                         Some(region_scope_tree),
204                         span,
205                         named_ty,
206                         named_region,
207                     )
208                     .buffer(&mut self.errors_buffer);
209                 }
210
211                 RegionErrorKind::BoundUniversalRegionError {
212                     longer_fr,
213                     fr_origin,
214                     error_element,
215                 } => {
216                     let error_region = self.regioncx.region_from_element(longer_fr, error_element);
217
218                     // Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
219                     let (_, span) = self.regioncx.find_outlives_blame_span(
220                         &self.body,
221                         longer_fr,
222                         fr_origin,
223                         error_region,
224                     );
225
226                     // FIXME: improve this error message
227                     self.infcx
228                         .tcx
229                         .sess
230                         .struct_span_err(span, "higher-ranked subtype error")
231                         .buffer(&mut self.errors_buffer);
232                 }
233
234                 RegionErrorKind::RegionError { fr_origin, longer_fr, shorter_fr, is_reported } => {
235                     if is_reported {
236                         self.report_region_error(
237                             longer_fr,
238                             fr_origin,
239                             shorter_fr,
240                             &mut outlives_suggestion,
241                         );
242                     } else {
243                         // We only report the first error, so as not to overwhelm the user. See
244                         // `RegRegionErrorKind` docs.
245                         //
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?
249                         debug!(
250                             "Unreported region error: can't prove that {:?}: {:?}",
251                             longer_fr, shorter_fr
252                         );
253                     }
254                 }
255             }
256         }
257
258         // Emit one outlives suggestions for each MIR def we borrowck
259         outlives_suggestion.add_suggestion(self);
260     }
261
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:
264     ///
265     /// ```
266     /// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
267     /// ```
268     ///
269     /// Here we would be invoked with `fr = 'a` and `outlived_fr = `'b`.
270     pub(in crate::borrow_check) fn report_region_error(
271         &mut self,
272         fr: RegionVid,
273         fr_origin: NLLRegionVariableOrigin,
274         outlived_fr: RegionVid,
275         outlives_suggestion: &mut OutlivesSuggestionBuilder,
276     ) {
277         debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
278
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)
282             });
283
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);
290                 return;
291             }
292         }
293
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),
297         );
298
299         debug!(
300             "report_region_error: fr_is_local={:?} outlived_fr_is_local={:?} category={:?}",
301             fr_is_local, outlived_fr_is_local, category
302         );
303
304         let errci = ErrorConstraintInfo {
305             fr,
306             outlived_fr,
307             fr_is_local,
308             outlived_fr_is_local,
309             category,
310             span,
311         };
312
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)
316             }
317             (ConstraintCategory::Assignment, true, false)
318             | (ConstraintCategory::CallArgument, true, false) => {
319                 let mut db = self.report_escaping_data_error(&errci);
320
321                 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
322                 outlives_suggestion.collect_constraint(fr, outlived_fr);
323
324                 db
325             }
326             _ => {
327                 let mut db = self.report_general_error(&errci);
328
329                 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
330                 outlives_suggestion.collect_constraint(fr, outlived_fr);
331
332                 db
333             }
334         };
335
336         diag.buffer(&mut self.errors_buffer);
337     }
338
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.
341     ///
342     /// ```text
343     /// error: captured variable cannot escape `FnMut` closure body
344     ///   --> $DIR/issue-53040.rs:15:8
345     ///    |
346     /// LL |     || &mut v;
347     ///    |     -- ^^^^^^ creates a reference to a captured variable which escapes the closure body
348     ///    |     |
349     ///    |     inferred to be a `FnMut` closure
350     ///    |
351     ///    = note: `FnMut` closures only have access to their captured variables while they are
352     ///            executing...
353     ///    = note: ...therefore, returned references to captured variables will escape the closure
354     /// ```
355     fn report_fnmut_error(&self, errci: &ErrorConstraintInfo) -> DiagnosticBuilder<'tcx> {
356         let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
357
358         let mut diag = self
359             .infcx
360             .tcx
361             .sess
362             .struct_span_err(*span, "captured variable cannot escape `FnMut` closure body");
363
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"
371         } else {
372             "returns a reference to a captured variable which escapes the closure body"
373         };
374
375         diag.span_label(*span, message);
376
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");
387             }
388             _ => {}
389         }
390
391         diag.note(
392             "`FnMut` closures only have access to their captured variables while they are \
393              executing...",
394         );
395         diag.note("...therefore, they cannot allow references to captured variables to escape");
396
397         diag
398     }
399
400     /// Reports a error specifically for when data is escaping a closure.
401     ///
402     /// ```text
403     /// error: borrowed data escapes outside of function
404     ///   --> $DIR/lifetime-bound-will-change-warning.rs:44:5
405     ///    |
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.
409     /// LL |     ref_obj(x)
410     ///    |     ^^^^^^^^^^ `x` escapes the function body here
411     /// ```
412     fn report_escaping_data_error(&self, errci: &ErrorConstraintInfo) -> DiagnosticBuilder<'tcx> {
413         let ErrorConstraintInfo { span, category, .. } = errci;
414
415         let fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
416             self.infcx.tcx,
417             &self.body,
418             &self.local_names,
419             &self.upvars,
420             errci.fr,
421         );
422         let outlived_fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
423             self.infcx.tcx,
424             &self.body,
425             &self.local_names,
426             &self.upvars,
427             errci.outlived_fr,
428         );
429
430         let (_, escapes_from) = self
431             .infcx
432             .tcx
433             .article_and_description(self.regioncx.universal_regions().defining_ty.def_id());
434
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()
441         {
442             return self.report_general_error(&ErrorConstraintInfo {
443                 fr_is_local: true,
444                 outlived_fr_is_local: false,
445                 ..*errci
446             });
447         }
448
449         let mut diag =
450             borrowck_errors::borrowed_data_escapes_closure(self.infcx.tcx, *span, escapes_from);
451
452         if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span {
453             diag.span_label(
454                 outlived_fr_span,
455                 format!(
456                     "`{}` declared here, outside of the {} body",
457                     outlived_fr_name, escapes_from
458                 ),
459             );
460         }
461
462         if let Some((Some(fr_name), fr_span)) = fr_name_and_span {
463             diag.span_label(
464                 fr_span,
465                 format!(
466                     "`{}` is a reference that is only valid in the {} body",
467                     fr_name, escapes_from
468                 ),
469             );
470
471             diag.span_label(*span, format!("`{}` escapes the {} body here", fr_name, escapes_from));
472         }
473
474         diag
475     }
476
477     /// Reports a region inference error for the general case with named/synthesized lifetimes to
478     /// explain what is happening.
479     ///
480     /// ```text
481     /// error: unsatisfied lifetime constraints
482     ///   --> $DIR/regions-creating-enums3.rs:17:5
483     ///    |
484     /// LL | fn mk_add_bad1<'a,'b>(x: &'a ast<'a>, y: &'b ast<'b>) -> ast<'a> {
485     ///    |                -- -- lifetime `'b` defined here
486     ///    |                |
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`
491     /// ```
492     fn report_general_error(&self, errci: &ErrorConstraintInfo) -> DiagnosticBuilder<'tcx> {
493         let ErrorConstraintInfo {
494             fr,
495             fr_is_local,
496             outlived_fr,
497             outlived_fr_is_local,
498             span,
499             category,
500             ..
501         } = errci;
502
503         let mut diag =
504             self.infcx.tcx.sess.struct_span_err(*span, "lifetime may not live long enough");
505
506         let (_, mir_def_name) = self.infcx.tcx.article_and_description(self.mir_def_id);
507
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);
512
513         match (category, outlived_fr_is_local, fr_is_local) {
514             (ConstraintCategory::Return, true, _) => {
515                 diag.span_label(
516                     *span,
517                     format!(
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
521                     ),
522                 );
523             }
524             _ => {
525                 diag.span_label(
526                     *span,
527                     format!(
528                         "{}requires that `{}` must outlive `{}`",
529                         category.description(),
530                         fr_name,
531                         outlived_fr_name,
532                     ),
533                 );
534             }
535         }
536
537         self.add_static_impl_trait_suggestion(&mut diag, *fr, fr_name, *outlived_fr);
538
539         diag
540     }
541
542     /// Adds a suggestion to errors where a `impl Trait` is returned.
543     ///
544     /// ```text
545     /// help: to allow this `impl Trait` to capture borrowed data with lifetime `'1`, add `'_` as
546     ///       a constraint
547     ///    |
548     /// LL |     fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
549     ///    |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
550     /// ```
551     fn add_static_impl_trait_suggestion(
552         &self,
553         diag: &mut DiagnosticBuilder<'tcx>,
554         fr: RegionVid,
555         // We need to pass `fr_name` - computing it again will label it twice.
556         fr_name: RegionName,
557         outlived_fr: RegionVid,
558     ) {
559         if let (Some(f), Some(ty::RegionKind::ReStatic)) =
560             (self.to_error_region(fr), self.to_error_region(outlived_fr))
561         {
562             if let Some((ty::TyS { kind: ty::Opaque(did, substs), .. }, _)) = self
563                 .infcx
564                 .tcx
565                 .is_suitable_region(f)
566                 .map(|r| r.def_id)
567                 .map(|id| self.infcx.tcx.return_type_impl_trait(id))
568                 .unwrap_or(None)
569             {
570                 // Check whether or not the impl trait return type is intended to capture
571                 // data with the static lifetime.
572                 //
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);
577
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) =
582                                 binder.skip_binder()
583                             {
584                                 found = true;
585                                 break;
586                             } else {
587                                 // If there's already a lifetime bound, don't
588                                 // suggest anything.
589                                 return;
590                             }
591                         }
592                     }
593
594                     found
595                 };
596
597                 debug!(
598                     "add_static_impl_trait_suggestion: has_static_predicate={:?}",
599                     has_static_predicate
600                 );
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));
606                 } else {
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() {
611                             fr_name.to_string()
612                         } else {
613                             "'_".to_string()
614                         };
615                         let suggestion = if snippet.ends_with(';') {
616                             // `type X = impl Trait;`
617                             format!("{} + {};", &snippet[..snippet.len() - 1], suggestable_fr_name)
618                         } else {
619                             format!("{} + {}", snippet, suggestable_fr_name)
620                         };
621                         diag.span_suggestion(
622                             span,
623                             &format!(
624                                 "to allow this `impl Trait` to capture borrowed data with lifetime \
625                                  `{}`, add `{}` as a bound",
626                                 fr_name, suggestable_fr_name,
627                             ),
628                             suggestion,
629                             Applicability::MachineApplicable,
630                         );
631                     }
632                 }
633             }
634         }
635     }
636 }