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::UnsafePtr(..)) => {
76 self.check_scope(self.scope(cmt))
79 mc::cat_static_item => {
83 mc::cat_downcast(ref base) |
84 mc::cat_deref(ref base, _, mc::OwnedPtr) | // L-Deref-Send
85 mc::cat_interior(ref base, _) | // L-Field
86 mc::cat_deref(ref base, _, mc::GcPtr) => {
87 self.check(base, discr_scope)
90 mc::cat_discr(ref base, new_discr_scope) => {
91 // Subtle: in a match, we must ensure that each binding
92 // variable remains valid for the duration of the arm in
93 // which it appears, presuming that this arm is taken.
94 // But it is inconvenient in trans to root something just
95 // for one arm. Therefore, we insert a cat_discr(),
96 // basically a special kind of category that says "if this
97 // value must be dynamically rooted, root it for the scope
100 // As an example, consider this scenario:
102 // let mut x = @Some(3);
103 // match *x { Some(y) {...} None {...} }
105 // Technically, the value `x` need only be rooted
106 // in the `some` arm. However, we evaluate `x` in trans
107 // before we know what arm will be taken, so we just
108 // always root it for the duration of the match.
110 // As a second example, consider *this* scenario:
112 // let x = @@Some(3);
113 // match x { @@Some(y) {...} @@None {...} }
115 // Here again, `x` need only be rooted in the `some` arm.
116 // In this case, the value which needs to be rooted is
117 // found only when checking which pattern matches: but
118 // this check is done before entering the arm. Therefore,
119 // even in this case we just choose to keep the value
120 // rooted for the entire match. This means the value will be
121 // rooted even if the none arm is taken. Oh well.
123 // At first, I tried to optimize the second case to only
124 // root in one arm, but the result was suboptimal: first,
125 // it interfered with the construction of phi nodes in the
126 // arm, as we were adding code to root values before the
127 // phi nodes were added. This could have been addressed
128 // with a second basic block. However, the naive approach
129 // also yielded suboptimal results for patterns like:
132 // match x { @@some_variant(y) | @@some_other_variant(y) =>
134 // The reason is that we would root the value once for
135 // each pattern and not once per arm. This is also easily
136 // fixed, but it's yet more code for what is really quite
139 // Nonetheless, if you decide to optimize this case in the
140 // future, you need only adjust where the cat_discr()
141 // node appears to draw the line between what will be rooted
142 // in the *arm* vs the *match*.
143 self.check(base, Some(new_discr_scope))
148 fn check_scope(&self, max_scope: ty::Region) -> R {
149 //! Reports an error if `loan_region` is larger than `valid_scope`
151 if !self.bccx.is_subregion_of(self.loan_region, max_scope) {
152 Err(self.report_error(err_out_of_scope(max_scope, self.loan_region)))
158 fn scope(&self, cmt: &mc::cmt) -> ty::Region {
159 //! Returns the maximal region scope for the which the
160 //! lvalue `cmt` is guaranteed to be valid without any
161 //! rooting etc, and presuming `cmt` is not mutated.
163 // See the SCOPE(LV) function in doc.rs
166 mc::cat_rvalue(temp_scope) => {
170 mc::cat_copied_upvar(_) => {
171 ty::ReScope(self.item_scope_id)
173 mc::cat_static_item => {
176 mc::cat_local(local_id) |
177 mc::cat_arg(local_id) => {
178 ty::ReScope(self.bccx.tcx.region_maps.var_scope(local_id))
180 mc::cat_deref(_, _, mc::UnsafePtr(..)) => {
183 mc::cat_deref(_, _, mc::BorrowedPtr(_, r)) => {
186 mc::cat_downcast(ref cmt) |
187 mc::cat_deref(ref cmt, _, mc::OwnedPtr) |
188 mc::cat_deref(ref cmt, _, mc::GcPtr) |
189 mc::cat_interior(ref cmt, _) |
190 mc::cat_discr(ref cmt, _) => {
196 fn report_error(&self, code: bckerr_code) {
197 self.bccx.report(BckError { cmt: self.cmt_original.clone(),