1 // Copyright 2012 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.
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.
12 * This module implements the check that the lifetime of a borrow
13 * does not exceed the lifetime of the value being borrowed.
16 use middle::borrowck::*;
17 use euv = middle::expr_use_visitor;
18 use mc = middle::mem_categorization;
20 use util::ppaux::Repr;
22 use syntax::codemap::Span;
24 type R = Result<(),()>;
26 pub fn guarantee_lifetime(bccx: &BorrowckCtxt,
27 item_scope_id: ast::NodeId,
29 cause: euv::LoanCause,
31 loan_region: ty::Region,
34 debug!("guarantee_lifetime(cmt={}, loan_region={})",
35 cmt.repr(bccx.tcx), loan_region.repr(bccx.tcx));
36 let ctxt = GuaranteeLifetimeContext {bccx: bccx,
37 item_scope_id: item_scope_id,
40 loan_region: loan_region,
41 cmt_original: cmt.clone()};
42 ctxt.check(&cmt, None)
45 ///////////////////////////////////////////////////////////////////////////
48 struct GuaranteeLifetimeContext<'a> {
49 bccx: &'a BorrowckCtxt<'a>,
51 // the node id of the function body for the enclosing item
52 item_scope_id: ast::NodeId,
55 cause: euv::LoanCause,
56 loan_region: ty::Region,
60 impl<'a> GuaranteeLifetimeContext<'a> {
62 fn check(&self, cmt: &mc::cmt, discr_scope: Option<ast::NodeId>) -> R {
63 //! Main routine. Walks down `cmt` until we find the "guarantor".
64 debug!("guarantee_lifetime.check(cmt={}, loan_region={})",
65 cmt.repr(self.bccx.tcx),
66 self.loan_region.repr(self.bccx.tcx));
70 mc::cat_copied_upvar(..) | // L-Local
71 mc::cat_local(..) | // L-Local
72 mc::cat_arg(..) | // L-Local
74 mc::cat_deref(_, _, mc::BorrowedPtr(..)) | // L-Deref-Borrowed
75 mc::cat_deref(_, _, mc::Implicit(..)) |
76 mc::cat_deref(_, _, mc::UnsafePtr(..)) => {
77 self.check_scope(self.scope(cmt))
80 mc::cat_static_item => {
84 mc::cat_downcast(ref base) |
85 mc::cat_deref(ref base, _, mc::OwnedPtr) | // L-Deref-Send
86 mc::cat_interior(ref base, _) | // L-Field
87 mc::cat_deref(ref base, _, mc::GcPtr) => {
88 self.check(base, discr_scope)
91 mc::cat_discr(ref base, new_discr_scope) => {
92 // Subtle: in a match, we must ensure that each binding
93 // variable remains valid for the duration of the arm in
94 // which it appears, presuming that this arm is taken.
95 // But it is inconvenient in trans to root something just
96 // for one arm. Therefore, we insert a cat_discr(),
97 // basically a special kind of category that says "if this
98 // value must be dynamically rooted, root it for the scope
101 // As an example, consider this scenario:
103 // let mut x = @Some(3);
104 // match *x { Some(y) {...} None {...} }
106 // Technically, the value `x` need only be rooted
107 // in the `some` arm. However, we evaluate `x` in trans
108 // before we know what arm will be taken, so we just
109 // always root it for the duration of the match.
111 // As a second example, consider *this* scenario:
113 // let x = @@Some(3);
114 // match x { @@Some(y) {...} @@None {...} }
116 // Here again, `x` need only be rooted in the `some` arm.
117 // In this case, the value which needs to be rooted is
118 // found only when checking which pattern matches: but
119 // this check is done before entering the arm. Therefore,
120 // even in this case we just choose to keep the value
121 // rooted for the entire match. This means the value will be
122 // rooted even if the none arm is taken. Oh well.
124 // At first, I tried to optimize the second case to only
125 // root in one arm, but the result was suboptimal: first,
126 // it interfered with the construction of phi nodes in the
127 // arm, as we were adding code to root values before the
128 // phi nodes were added. This could have been addressed
129 // with a second basic block. However, the naive approach
130 // also yielded suboptimal results for patterns like:
133 // match x { @@some_variant(y) | @@some_other_variant(y) =>
135 // The reason is that we would root the value once for
136 // each pattern and not once per arm. This is also easily
137 // fixed, but it's yet more code for what is really quite
140 // Nonetheless, if you decide to optimize this case in the
141 // future, you need only adjust where the cat_discr()
142 // node appears to draw the line between what will be rooted
143 // in the *arm* vs the *match*.
144 self.check(base, Some(new_discr_scope))
149 fn check_scope(&self, max_scope: ty::Region) -> R {
150 //! Reports an error if `loan_region` is larger than `valid_scope`
152 if !self.bccx.is_subregion_of(self.loan_region, max_scope) {
153 Err(self.report_error(err_out_of_scope(max_scope, self.loan_region)))
159 fn scope(&self, cmt: &mc::cmt) -> ty::Region {
160 //! Returns the maximal region scope for the which the
161 //! lvalue `cmt` is guaranteed to be valid without any
162 //! rooting etc, and presuming `cmt` is not mutated.
164 // See the SCOPE(LV) function in doc.rs
167 mc::cat_rvalue(temp_scope) => {
171 mc::cat_copied_upvar(_) => {
172 ty::ReScope(self.item_scope_id)
174 mc::cat_static_item => {
177 mc::cat_local(local_id) |
178 mc::cat_arg(local_id) => {
179 ty::ReScope(self.bccx.tcx.region_maps.var_scope(local_id))
181 mc::cat_deref(_, _, mc::UnsafePtr(..)) => {
184 mc::cat_deref(_, _, mc::BorrowedPtr(_, r)) |
185 mc::cat_deref(_, _, mc::Implicit(_, r)) => {
188 mc::cat_downcast(ref cmt) |
189 mc::cat_deref(ref cmt, _, mc::OwnedPtr) |
190 mc::cat_deref(ref cmt, _, mc::GcPtr) |
191 mc::cat_interior(ref cmt, _) |
192 mc::cat_discr(ref cmt, _) => {
198 fn report_error(&self, code: bckerr_code) {
199 self.bccx.report(BckError { cmt: self.cmt_original.clone(),