Skip to content

Commit 56f4d18

Browse files
committed
Link lifetimes in let patterns just as we do for match patterns
1 parent 0c916c5 commit 56f4d18

File tree

5 files changed

+72
-3
lines changed

5 files changed

+72
-3
lines changed

src/librustc/middle/borrowck/gather_loans/lifetime.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ impl<'a> GuaranteeLifetimeContext<'a> {
6767

6868
fn check(&self, cmt: mc::cmt, discr_scope: Option<ast::NodeId>) -> R {
6969
//! Main routine. Walks down `cmt` until we find the "guarantor".
70+
debug!("guarantee_lifetime.check(cmt={}, loan_region={})",
71+
cmt.repr(self.bccx.tcx),
72+
self.loan_region.repr(self.bccx.tcx));
7073

7174
match cmt.cat {
7275
mc::cat_rvalue(..) |

src/librustc/middle/typeck/check/regionck.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ fn visit_arm(rcx: &mut Rcx, arm: &ast::Arm) {
213213
fn visit_local(rcx: &mut Rcx, l: &ast::Local) {
214214
// see above
215215
constrain_bindings_in_pat(l.pat, rcx);
216+
guarantor::for_local(rcx, l);
216217
visit::walk_local(rcx, l, ());
217218
}
218219

@@ -828,6 +829,30 @@ pub mod guarantor {
828829
}
829830
}
830831

832+
pub fn for_local(rcx: &mut Rcx, local: &ast::Local) {
833+
/*!
834+
* Link the lifetimes of any ref bindings in a let
835+
* pattern to the lifetimes in the initializer.
836+
*
837+
* For example, given something like this:
838+
*
839+
* let &Foo(ref x) = ...;
840+
*
841+
* this would ensure that the lifetime 'a of the
842+
* region pointer being matched must be >= the lifetime
843+
* of the ref binding.
844+
*/
845+
846+
debug!("regionck::for_match()");
847+
let init_expr = match local.init {
848+
None => { return; }
849+
Some(e) => e
850+
};
851+
let init_guarantor = guarantor(rcx, init_expr);
852+
debug!("init_guarantor={}", init_guarantor.repr(rcx.tcx()));
853+
link_ref_bindings_in_pat(rcx, local.pat, init_guarantor);
854+
}
855+
831856
pub fn for_autoref(rcx: &mut Rcx,
832857
expr: &ast::Expr,
833858
autoderefs: uint,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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.
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+
// Test lifetimes are linked properly when we take reference
12+
// to interior.
13+
14+
struct Foo(int);
15+
16+
fn foo() -> &int {
17+
let &Foo(ref x) = &Foo(3); //~ ERROR borrowed value does not live long enough
18+
x
19+
}
20+
21+
pub fn main() {
22+
}

src/test/debug-info/destructured-local.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ fn main() {
196196
let Unit(ii) = Unit(51);
197197

198198
// univariant enum with ref binding
199-
let Unit(ref jj) = Unit(52);
199+
let &Unit(ref jj) = &Unit(52);
200200

201201
// tuple struct
202-
let TupleStruct(kk, ll) = TupleStruct(53.0, 54);
202+
let &TupleStruct(kk, ll) = &TupleStruct(53.0, 54);
203203

204204
// tuple struct with ref binding
205-
let TupleStruct(mm, ref nn) = TupleStruct(55.0, 56);
205+
let &TupleStruct(mm, ref nn) = &TupleStruct(55.0, 56);
206206

207207
zzz();
208208
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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.
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+
// Test lifetimes are linked properly when we take reference
12+
// to interior.
13+
14+
struct Foo(int);
15+
pub fn main() {
16+
// Here the lifetime of the `&` should be at least the
17+
// block, since a ref binding is created to the interior.
18+
let &Foo(ref _x) = &Foo(3);
19+
}

0 commit comments

Comments
 (0)