|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! Error Reporting for Anonymous Region Lifetime Errors |
| 12 | +//! where both the regions are anonymous. |
| 13 | +
|
| 14 | +use infer::error_reporting::nice_region_error::NiceRegionError; |
| 15 | +use infer::SubregionOrigin; |
| 16 | +use ty::RegionKind; |
| 17 | +use hir::{Expr, ExprClosure}; |
| 18 | +use hir::map::NodeExpr; |
| 19 | +use util::common::ErrorReported; |
| 20 | +use infer::lexical_region_resolve::RegionResolutionError::SubSupConflict; |
| 21 | + |
| 22 | +impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> { |
| 23 | + /// Print the error message for lifetime errors when binding excapes a closure. |
| 24 | + /// |
| 25 | + /// Consider a case where we have |
| 26 | + /// |
| 27 | + /// ```no_run |
| 28 | + /// fn with_int<F>(f: F) where F: FnOnce(&isize) { |
| 29 | + /// let x = 3; |
| 30 | + /// f(&x); |
| 31 | + /// } |
| 32 | + /// fn main() { |
| 33 | + /// let mut x = None; |
| 34 | + /// with_int(|y| x = Some(y)); |
| 35 | + /// } |
| 36 | + /// ``` |
| 37 | + /// |
| 38 | + /// the output will be |
| 39 | + /// |
| 40 | + /// ```text |
| 41 | + /// let mut x = None; |
| 42 | + /// ----- borrowed data cannot be stored into here... |
| 43 | + /// with_int(|y| x = Some(y)); |
| 44 | + /// --- ^ cannot be stored outside of its closure |
| 45 | + /// | |
| 46 | + /// ...because it cannot outlive this closure |
| 47 | + /// ``` |
| 48 | + pub(super) fn try_report_outlives_closure(&self) -> Option<ErrorReported> { |
| 49 | + if let Some(SubSupConflict(origin, |
| 50 | + ref sub_origin, |
| 51 | + _, |
| 52 | + ref sup_origin, |
| 53 | + sup_region)) = self.error { |
| 54 | + |
| 55 | + // #45983: when trying to assign the contents of an argument to a binding outside of a |
| 56 | + // closure, provide a specific message pointing this out. |
| 57 | + if let (&SubregionOrigin::BindingTypeIsNotValidAtDecl(ref external_span), |
| 58 | + &RegionKind::ReFree(ref free_region)) = (&sub_origin, sup_region) { |
| 59 | + let hir = &self.tcx.hir; |
| 60 | + if let Some(node_id) = hir.as_local_node_id(free_region.scope) { |
| 61 | + match hir.get(node_id) { |
| 62 | + NodeExpr(Expr { |
| 63 | + node: ExprClosure(_, _, _, closure_span, false), |
| 64 | + .. |
| 65 | + }) => { |
| 66 | + let sup_sp = sup_origin.span(); |
| 67 | + let origin_sp = origin.span(); |
| 68 | + let mut err = self.tcx.sess.struct_span_err( |
| 69 | + sup_sp, |
| 70 | + "borrowed data cannot be stored outside of its closure"); |
| 71 | + err.span_label(sup_sp, "cannot be stored outside of its closure"); |
| 72 | + if origin_sp == sup_sp || origin_sp.contains(sup_sp) { |
| 73 | +// // sup_sp == origin.span(): |
| 74 | +// |
| 75 | +// let mut x = None; |
| 76 | +// ----- borrowed data cannot be stored into here... |
| 77 | +// with_int(|y| x = Some(y)); |
| 78 | +// --- ^ cannot be stored outside of its closure |
| 79 | +// | |
| 80 | +// ...because it cannot outlive this closure |
| 81 | +// |
| 82 | +// // origin.contains(&sup_sp): |
| 83 | +// |
| 84 | +// let mut f: Option<&u32> = None; |
| 85 | +// ----- borrowed data cannot be stored into here... |
| 86 | +// closure_expecting_bound(|x: &'x u32| { |
| 87 | +// ------------ ... because it cannot outlive this closure |
| 88 | +// f = Some(x); |
| 89 | +// ^ cannot be stored outside of its closure |
| 90 | + err.span_label(*external_span, |
| 91 | + "borrowed data cannot be stored into here..."); |
| 92 | + err.span_label(*closure_span, |
| 93 | + "...because it cannot outlive this closure"); |
| 94 | + } else { |
| 95 | +// FIXME: the wording for this case could be much improved |
| 96 | +// |
| 97 | +// let mut lines_to_use: Vec<&CrateId> = Vec::new(); |
| 98 | +// - cannot infer an appropriate lifetime... |
| 99 | +// let push_id = |installed_id: &CrateId| { |
| 100 | +// ------- ------------------------ borrowed data cannot outlive this closure |
| 101 | +// | |
| 102 | +// ...so that variable is valid at time of its declaration |
| 103 | +// lines_to_use.push(installed_id); |
| 104 | +// ^^^^^^^^^^^^ cannot be stored outside of its closure |
| 105 | + err.span_label(origin_sp, |
| 106 | + "cannot infer an appropriate lifetime..."); |
| 107 | + err.span_label(*external_span, |
| 108 | + "...so that variable is valid at time of its \ |
| 109 | + declaration"); |
| 110 | + err.span_label(*closure_span, |
| 111 | + "borrowed data cannot outlive this closure"); |
| 112 | + } |
| 113 | + err.emit(); |
| 114 | + return Some(ErrorReported); |
| 115 | + } |
| 116 | + _ => {} |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + None |
| 122 | + } |
| 123 | +} |
| 124 | + |
0 commit comments