|
1 | 1 | //! Error Reporting for Anonymous Region Lifetime Errors
|
2 | 2 | //! where both the regions are anonymous.
|
3 | 3 |
|
| 4 | +use crate::hir::Node; |
| 5 | +use crate::hir::{Expr, ExprKind::Closure}; |
4 | 6 | use crate::infer::error_reporting::nice_region_error::NiceRegionError;
|
| 7 | +use crate::infer::lexical_region_resolve::RegionResolutionError::SubSupConflict; |
5 | 8 | use crate::infer::SubregionOrigin;
|
6 | 9 | use crate::ty::RegionKind;
|
7 |
| -use crate::hir::{Expr, ExprKind::Closure}; |
8 |
| -use crate::hir::Node; |
9 | 10 | use crate::util::common::ErrorReported;
|
10 |
| -use crate::infer::lexical_region_resolve::RegionResolutionError::SubSupConflict; |
11 | 11 |
|
12 | 12 | impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
13 | 13 | /// Print the error message for lifetime errors when binding escapes a closure.
|
@@ -36,69 +36,75 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
|
36 | 36 | /// ...because it cannot outlive this closure
|
37 | 37 | /// ```
|
38 | 38 | pub(super) fn try_report_outlives_closure(&self) -> Option<ErrorReported> {
|
39 |
| - if let Some(SubSupConflict(_, |
40 |
| - origin, |
41 |
| - ref sub_origin, |
42 |
| - _, |
43 |
| - ref sup_origin, |
44 |
| - sup_region)) = self.error { |
45 |
| - |
| 39 | + if let Some(SubSupConflict(_, origin, ref sub_origin, _, ref sup_origin, sup_region)) = |
| 40 | + self.error |
| 41 | + { |
46 | 42 | // #45983: when trying to assign the contents of an argument to a binding outside of a
|
47 | 43 | // closure, provide a specific message pointing this out.
|
48 |
| - if let (&SubregionOrigin::BindingTypeIsNotValidAtDecl(ref external_span), |
49 |
| - &RegionKind::ReFree(ref free_region)) = (&sub_origin, sup_region) { |
| 44 | + if let ( |
| 45 | + &SubregionOrigin::BindingTypeIsNotValidAtDecl(ref external_span), |
| 46 | + &RegionKind::ReFree(ref free_region), |
| 47 | + ) = (&sub_origin, sup_region) |
| 48 | + { |
50 | 49 | let hir = &self.tcx().hir();
|
51 | 50 | if let Some(hir_id) = hir.as_local_hir_id(free_region.scope) {
|
52 |
| - if let Node::Expr(Expr { |
53 |
| - kind: Closure(_, _, _, closure_span, None), |
54 |
| - .. |
55 |
| - }) = hir.get(hir_id) { |
| 51 | + if let Node::Expr(Expr { kind: Closure(_, _, _, closure_span, None), .. }) = |
| 52 | + hir.get(hir_id) |
| 53 | + { |
56 | 54 | let sup_sp = sup_origin.span();
|
57 | 55 | let origin_sp = origin.span();
|
58 | 56 | let mut err = self.tcx().sess.struct_span_err(
|
59 | 57 | sup_sp,
|
60 |
| - "borrowed data cannot be stored outside of its closure"); |
| 58 | + "borrowed data cannot be stored outside of its closure", |
| 59 | + ); |
61 | 60 | err.span_label(sup_sp, "cannot be stored outside of its closure");
|
62 | 61 | if origin_sp == sup_sp || origin_sp.contains(sup_sp) {
|
63 |
| -// // sup_sp == origin.span(): |
64 |
| -// |
65 |
| -// let mut x = None; |
66 |
| -// ----- borrowed data cannot be stored into here... |
67 |
| -// with_int(|y| x = Some(y)); |
68 |
| -// --- ^ cannot be stored outside of its closure |
69 |
| -// | |
70 |
| -// ...because it cannot outlive this closure |
71 |
| -// |
72 |
| -// // origin.contains(&sup_sp): |
73 |
| -// |
74 |
| -// let mut f: Option<&u32> = None; |
75 |
| -// ----- borrowed data cannot be stored into here... |
76 |
| -// closure_expecting_bound(|x: &'x u32| { |
77 |
| -// ------------ ... because it cannot outlive this closure |
78 |
| -// f = Some(x); |
79 |
| -// ^ cannot be stored outside of its closure |
80 |
| - err.span_label(*external_span, |
81 |
| - "borrowed data cannot be stored into here..."); |
82 |
| - err.span_label(*closure_span, |
83 |
| - "...because it cannot outlive this closure"); |
| 62 | + // // sup_sp == origin.span(): |
| 63 | + // |
| 64 | + // let mut x = None; |
| 65 | + // ----- borrowed data cannot be stored into here... |
| 66 | + // with_int(|y| x = Some(y)); |
| 67 | + // --- ^ cannot be stored outside of its closure |
| 68 | + // | |
| 69 | + // ...because it cannot outlive this closure |
| 70 | + // |
| 71 | + // // origin.contains(&sup_sp): |
| 72 | + // |
| 73 | + // let mut f: Option<&u32> = None; |
| 74 | + // ----- borrowed data cannot be stored into here... |
| 75 | + // closure_expecting_bound(|x: &'x u32| { |
| 76 | + // ------------ ... because it cannot outlive this closure |
| 77 | + // f = Some(x); |
| 78 | + // ^ cannot be stored outside of its closure |
| 79 | + err.span_label( |
| 80 | + *external_span, |
| 81 | + "borrowed data cannot be stored into here...", |
| 82 | + ); |
| 83 | + err.span_label( |
| 84 | + *closure_span, |
| 85 | + "...because it cannot outlive this closure", |
| 86 | + ); |
84 | 87 | } else {
|
85 |
| -// FIXME: the wording for this case could be much improved |
86 |
| -// |
87 |
| -// let mut lines_to_use: Vec<&CrateId> = Vec::new(); |
88 |
| -// - cannot infer an appropriate lifetime... |
89 |
| -// let push_id = |installed_id: &CrateId| { |
90 |
| -// ------- ------------------------ borrowed data cannot outlive this closure |
91 |
| -// | |
92 |
| -// ...so that variable is valid at time of its declaration |
93 |
| -// lines_to_use.push(installed_id); |
94 |
| -// ^^^^^^^^^^^^ cannot be stored outside of its closure |
95 |
| - err.span_label(origin_sp, |
96 |
| - "cannot infer an appropriate lifetime..."); |
97 |
| - err.span_label(*external_span, |
98 |
| - "...so that variable is valid at time of its \ |
99 |
| - declaration"); |
100 |
| - err.span_label(*closure_span, |
101 |
| - "borrowed data cannot outlive this closure"); |
| 88 | + // FIXME: the wording for this case could be much improved |
| 89 | + // |
| 90 | + // let mut lines_to_use: Vec<&CrateId> = Vec::new(); |
| 91 | + // - cannot infer an appropriate lifetime... |
| 92 | + // let push_id = |installed_id: &CrateId| { |
| 93 | + // ------- ------------------------ borrowed data cannot outlive this closure |
| 94 | + // | |
| 95 | + // ...so that variable is valid at time of its declaration |
| 96 | + // lines_to_use.push(installed_id); |
| 97 | + // ^^^^^^^^^^^^ cannot be stored outside of its closure |
| 98 | + err.span_label(origin_sp, "cannot infer an appropriate lifetime..."); |
| 99 | + err.span_label( |
| 100 | + *external_span, |
| 101 | + "...so that variable is valid at time of its \ |
| 102 | + declaration", |
| 103 | + ); |
| 104 | + err.span_label( |
| 105 | + *closure_span, |
| 106 | + "borrowed data cannot outlive this closure", |
| 107 | + ); |
102 | 108 | }
|
103 | 109 | err.emit();
|
104 | 110 | return Some(ErrorReported);
|
|
0 commit comments