Skip to content

Commit 91b88c5

Browse files
committed
Add ty to LoanPath.
To make this clean, refactored old `LoanPath` enum into a `LoanPath` struct with a `ty::t` and a newly-added `LoanPathVariant` enum. This enabled me to get rid of the ugly and fragile `LoanPath::to_type` method, and I can probably also get rid of other stuff that was supporting it, maybe.
1 parent d6c8f3b commit 91b88c5

File tree

8 files changed

+206
-175
lines changed

8 files changed

+206
-175
lines changed

src/librustc/middle/borrowck/check_loans.rs

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use self::UseError::*;
2020

2121
use middle::borrowck::*;
22+
use middle::borrowck::LoanPathElem::*;
23+
use middle::borrowck::LoanPathKind::*;
2224
use middle::expr_use_visitor as euv;
2325
use middle::mem_categorization as mc;
2426
use middle::region;
@@ -33,51 +35,51 @@ use std::rc::Rc;
3335
// be less precise in its handling of Box while still allowing moves out of a
3436
// Box. They should be removed when OwnedPtr is removed from LoanPath.
3537

36-
fn owned_ptr_base_path<'a>(loan_path: &'a LoanPath) -> &'a LoanPath {
38+
fn owned_ptr_base_path<'a, 'tcx>(loan_path: &'a LoanPath<'tcx>) -> &'a LoanPath<'tcx> {
3739
//! Returns the base of the leftmost dereference of an OwnedPtr in
3840
//! `loan_path`. If there is no dereference of an OwnedPtr in `loan_path`,
3941
//! then it just returns `loan_path` itself.
4042
41-
return match owned_ptr_base_path_helper(loan_path) {
43+
return match helper(loan_path) {
4244
Some(new_loan_path) => new_loan_path,
4345
None => loan_path.clone()
4446
};
4547

46-
fn owned_ptr_base_path_helper<'a>(loan_path: &'a LoanPath) -> Option<&'a LoanPath> {
47-
match *loan_path {
48+
fn helper<'a, 'tcx>(loan_path: &'a LoanPath<'tcx>) -> Option<&'a LoanPath<'tcx>> {
49+
match loan_path.kind {
4850
LpVar(_) | LpUpvar(_) => None,
4951
LpExtend(ref lp_base, _, LpDeref(mc::OwnedPtr)) => {
50-
match owned_ptr_base_path_helper(&**lp_base) {
52+
match helper(&**lp_base) {
5153
v @ Some(_) => v,
5254
None => Some(&**lp_base)
5355
}
5456
}
5557
LpDowncast(ref lp_base, _) |
56-
LpExtend(ref lp_base, _, _) => owned_ptr_base_path_helper(&**lp_base)
58+
LpExtend(ref lp_base, _, _) => helper(&**lp_base)
5759
}
5860
}
5961
}
6062

61-
fn owned_ptr_base_path_rc(loan_path: &Rc<LoanPath>) -> Rc<LoanPath> {
63+
fn owned_ptr_base_path_rc<'tcx>(loan_path: &Rc<LoanPath<'tcx>>) -> Rc<LoanPath<'tcx>> {
6264
//! The equivalent of `owned_ptr_base_path` for an &Rc<LoanPath> rather than
6365
//! a &LoanPath.
6466
65-
return match owned_ptr_base_path_helper(loan_path) {
67+
return match helper(loan_path) {
6668
Some(new_loan_path) => new_loan_path,
6769
None => loan_path.clone()
6870
};
6971

70-
fn owned_ptr_base_path_helper(loan_path: &Rc<LoanPath>) -> Option<Rc<LoanPath>> {
71-
match **loan_path {
72+
fn helper<'tcx>(loan_path: &Rc<LoanPath<'tcx>>) -> Option<Rc<LoanPath<'tcx>>> {
73+
match loan_path.kind {
7274
LpVar(_) | LpUpvar(_) => None,
7375
LpExtend(ref lp_base, _, LpDeref(mc::OwnedPtr)) => {
74-
match owned_ptr_base_path_helper(lp_base) {
76+
match helper(lp_base) {
7577
v @ Some(_) => v,
7678
None => Some(lp_base.clone())
7779
}
7880
}
7981
LpDowncast(ref lp_base, _) |
80-
LpExtend(ref lp_base, _, _) => owned_ptr_base_path_helper(lp_base)
82+
LpExtend(ref lp_base, _, _) => helper(lp_base)
8183
}
8284
}
8385
}
@@ -86,7 +88,7 @@ struct CheckLoanCtxt<'a, 'tcx: 'a> {
8688
bccx: &'a BorrowckCtxt<'a, 'tcx>,
8789
dfcx_loans: &'a LoanDataFlow<'a, 'tcx>,
8890
move_data: move_data::FlowedMoveData<'a, 'tcx>,
89-
all_loans: &'a [Loan],
91+
all_loans: &'a [Loan<'tcx>],
9092
}
9193

9294
impl<'a, 'tcx> euv::Delegate<'tcx> for CheckLoanCtxt<'a, 'tcx> {
@@ -185,7 +187,7 @@ impl<'a, 'tcx> euv::Delegate<'tcx> for CheckLoanCtxt<'a, 'tcx> {
185187
pub fn check_loans<'a, 'b, 'c, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
186188
dfcx_loans: &LoanDataFlow<'b, 'tcx>,
187189
move_data: move_data::FlowedMoveData<'c, 'tcx>,
188-
all_loans: &[Loan],
190+
all_loans: &[Loan<'tcx>],
189191
decl: &ast::FnDecl,
190192
body: &ast::Block) {
191193
debug!("check_loans(body id={})", body.id);
@@ -204,9 +206,9 @@ pub fn check_loans<'a, 'b, 'c, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
204206
}
205207

206208
#[deriving(PartialEq)]
207-
enum UseError {
209+
enum UseError<'tcx> {
208210
UseOk,
209-
UseWhileBorrowed(/*loan*/Rc<LoanPath>, /*loan*/Span)
211+
UseWhileBorrowed(/*loan*/Rc<LoanPath<'tcx>>, /*loan*/Span)
210212
}
211213

212214
fn compatible_borrow_kinds(borrow_kind1: ty::BorrowKind,
@@ -218,7 +220,7 @@ fn compatible_borrow_kinds(borrow_kind1: ty::BorrowKind,
218220
impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
219221
pub fn tcx(&self) -> &'a ty::ctxt<'tcx> { self.bccx.tcx }
220222

221-
pub fn each_issued_loan(&self, scope: region::CodeExtent, op: |&Loan| -> bool)
223+
pub fn each_issued_loan(&self, scope: region::CodeExtent, op: |&Loan<'tcx>| -> bool)
222224
-> bool {
223225
//! Iterates over each loan that has been issued
224226
//! on entrance to `scope`, regardless of whether it is
@@ -234,7 +236,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
234236

235237
pub fn each_in_scope_loan(&self,
236238
scope: region::CodeExtent,
237-
op: |&Loan| -> bool)
239+
op: |&Loan<'tcx>| -> bool)
238240
-> bool {
239241
//! Like `each_issued_loan()`, but only considers loans that are
240242
//! currently in scope.
@@ -251,8 +253,8 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
251253

252254
fn each_in_scope_loan_affecting_path(&self,
253255
scope: region::CodeExtent,
254-
loan_path: &LoanPath,
255-
op: |&Loan| -> bool)
256+
loan_path: &LoanPath<'tcx>,
257+
op: |&Loan<'tcx>| -> bool)
256258
-> bool {
257259
//! Iterates through all of the in-scope loans affecting `loan_path`,
258260
//! calling `op`, and ceasing iteration if `false` is returned.
@@ -296,7 +298,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
296298

297299
let mut loan_path = loan_path;
298300
loop {
299-
match *loan_path {
301+
match loan_path.kind {
300302
LpVar(_) | LpUpvar(_) => {
301303
break;
302304
}
@@ -366,8 +368,8 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
366368
}
367369

368370
pub fn report_error_if_loans_conflict(&self,
369-
old_loan: &Loan,
370-
new_loan: &Loan) {
371+
old_loan: &Loan<'tcx>,
372+
new_loan: &Loan<'tcx>) {
371373
//! Checks whether `old_loan` and `new_loan` can safely be issued
372374
//! simultaneously.
373375
@@ -386,10 +388,10 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
386388
}
387389

388390
pub fn report_error_if_loan_conflicts_with_restriction(&self,
389-
loan1: &Loan,
390-
loan2: &Loan,
391-
old_loan: &Loan,
392-
new_loan: &Loan)
391+
loan1: &Loan<'tcx>,
392+
loan2: &Loan<'tcx>,
393+
old_loan: &Loan<'tcx>,
394+
new_loan: &Loan<'tcx>)
393395
-> bool {
394396
//! Checks whether the restrictions introduced by `loan1` would
395397
//! prohibit `loan2`. Returns false if an error is reported.
@@ -552,7 +554,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
552554
true
553555
}
554556

555-
fn is_local_variable_or_arg(&self, cmt: mc::cmt) -> bool {
557+
fn is_local_variable_or_arg(&self, cmt: mc::cmt<'tcx>) -> bool {
556558
match cmt.cat {
557559
mc::cat_local(_) => true,
558560
_ => false
@@ -562,7 +564,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
562564
fn consume_common(&self,
563565
id: ast::NodeId,
564566
span: Span,
565-
cmt: mc::cmt,
567+
cmt: mc::cmt<'tcx>,
566568
mode: euv::ConsumeMode) {
567569
match opt_loan_path(&cmt) {
568570
Some(lp) => {
@@ -603,7 +605,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
603605
fn check_for_copy_of_frozen_path(&self,
604606
id: ast::NodeId,
605607
span: Span,
606-
copy_path: &LoanPath) {
608+
copy_path: &LoanPath<'tcx>) {
607609
match self.analyze_restrictions_on_use(id, copy_path, ty::ImmBorrow) {
608610
UseOk => { }
609611
UseWhileBorrowed(loan_path, loan_span) => {
@@ -624,7 +626,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
624626
fn check_for_move_of_borrowed_path(&self,
625627
id: ast::NodeId,
626628
span: Span,
627-
move_path: &LoanPath,
629+
move_path: &LoanPath<'tcx>,
628630
move_kind: move_data::MoveKind) {
629631
// We want to detect if there are any loans at all, so we search for
630632
// any loans incompatible with MutBorrrow, since all other kinds of
@@ -655,9 +657,9 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
655657

656658
pub fn analyze_restrictions_on_use(&self,
657659
expr_id: ast::NodeId,
658-
use_path: &LoanPath,
660+
use_path: &LoanPath<'tcx>,
659661
borrow_kind: ty::BorrowKind)
660-
-> UseError {
662+
-> UseError<'tcx> {
661663
debug!("analyze_restrictions_on_use(expr_id={}, use_path={})",
662664
self.tcx().map.node_to_string(expr_id),
663665
use_path.repr(self.tcx()));
@@ -681,7 +683,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
681683
id: ast::NodeId,
682684
span: Span,
683685
use_kind: MovedValueUseKind,
684-
lp: &Rc<LoanPath>) {
686+
lp: &Rc<LoanPath<'tcx>>) {
685687
/*!
686688
* Reports an error if `expr` (which should be a path)
687689
* is using a moved/uninitialized value
@@ -705,7 +707,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
705707
id: ast::NodeId,
706708
span: Span,
707709
use_kind: MovedValueUseKind,
708-
lp: &Rc<LoanPath>)
710+
lp: &Rc<LoanPath<'tcx>>)
709711
{
710712
/*!
711713
* Reports an error if assigning to `lp` will use a
@@ -725,7 +727,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
725727
* (*p).x = 22; // not ok, p is uninitialized, can't deref
726728
*/
727729

728-
match **lp {
730+
match lp.kind {
729731
LpVar(_) | LpUpvar(_) => {
730732
// assigning to `x` does not require that `x` is initialized
731733
}
@@ -923,11 +925,11 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
923925
}
924926
}
925927

926-
fn check_for_assignment_to_borrowed_path(
927-
this: &CheckLoanCtxt,
928+
fn check_for_assignment_to_borrowed_path<'a, 'tcx>(
929+
this: &CheckLoanCtxt<'a, 'tcx>,
928930
assignment_id: ast::NodeId,
929931
assignment_span: Span,
930-
assignee_cmt: mc::cmt)
932+
assignee_cmt: mc::cmt<'tcx>)
931933
{
932934
//! Check for assignments that violate the terms of an
933935
//! outstanding loan.
@@ -947,7 +949,7 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
947949

948950
pub fn report_illegal_mutation(&self,
949951
span: Span,
950-
loan_path: &LoanPath,
952+
loan_path: &LoanPath<'tcx>,
951953
loan: &Loan) {
952954
self.bccx.span_err(
953955
span,

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414

1515
use middle::borrowck::*;
16+
use middle::borrowck::LoanPathKind::*;
1617
use middle::borrowck::gather_loans::move_error::MoveSpanAndPath;
1718
use middle::borrowck::gather_loans::move_error::{MoveError, MoveErrorCollector};
1819
use middle::borrowck::move_data::*;
@@ -32,17 +33,18 @@ struct GatherMoveInfo<'tcx> {
3233
span_path_opt: Option<MoveSpanAndPath>
3334
}
3435

35-
pub fn gather_decl(bccx: &BorrowckCtxt,
36-
move_data: &MoveData,
37-
decl_id: ast::NodeId,
38-
_decl_span: Span,
39-
var_id: ast::NodeId) {
40-
let loan_path = Rc::new(LpVar(var_id));
36+
pub fn gather_decl<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
37+
move_data: &MoveData<'tcx>,
38+
decl_id: ast::NodeId,
39+
_decl_span: Span,
40+
var_id: ast::NodeId) {
41+
let ty = ty::node_id_to_type(bccx.tcx, var_id);
42+
let loan_path = Rc::new(LoanPath::new(LpVar(var_id), ty));
4143
move_data.add_move(bccx.tcx, loan_path, decl_id, Declared);
4244
}
4345

4446
pub fn gather_move_from_expr<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
45-
move_data: &MoveData,
47+
move_data: &MoveData<'tcx>,
4648
move_error_collector: &MoveErrorCollector<'tcx>,
4749
move_expr_id: ast::NodeId,
4850
cmt: mc::cmt<'tcx>,
@@ -61,7 +63,7 @@ pub fn gather_move_from_expr<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
6163
}
6264

6365
pub fn gather_move_from_pat<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
64-
move_data: &MoveData,
66+
move_data: &MoveData<'tcx>,
6567
move_error_collector: &MoveErrorCollector<'tcx>,
6668
move_pat: &ast::Pat,
6769
cmt: mc::cmt<'tcx>) {
@@ -82,7 +84,7 @@ pub fn gather_move_from_pat<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
8284
}
8385

8486
fn gather_move<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
85-
move_data: &MoveData,
87+
move_data: &MoveData<'tcx>,
8688
move_error_collector: &MoveErrorCollector<'tcx>,
8789
move_info: GatherMoveInfo<'tcx>) {
8890
debug!("gather_move(move_id={}, cmt={})",
@@ -112,13 +114,13 @@ fn gather_move<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
112114
}
113115
}
114116

115-
pub fn gather_assignment(bccx: &BorrowckCtxt,
116-
move_data: &MoveData,
117-
assignment_id: ast::NodeId,
118-
assignment_span: Span,
119-
assignee_loan_path: Rc<LoanPath>,
120-
assignee_id: ast::NodeId,
121-
mode: euv::MutateMode) {
117+
pub fn gather_assignment<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
118+
move_data: &MoveData<'tcx>,
119+
assignment_id: ast::NodeId,
120+
assignment_span: Span,
121+
assignee_loan_path: Rc<LoanPath<'tcx>>,
122+
assignee_id: ast::NodeId,
123+
mode: euv::MutateMode) {
122124
move_data.add_assignment(bccx.tcx,
123125
assignee_loan_path,
124126
assignment_id,

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// sure that all of these loans are honored.
1818

1919
use middle::borrowck::*;
20+
use middle::borrowck::LoanPathKind::*;
2021
use middle::borrowck::move_data::MoveData;
2122
use middle::expr_use_visitor as euv;
2223
use middle::mem_categorization as mc;
@@ -35,10 +36,10 @@ mod restrictions;
3536
mod gather_moves;
3637
mod move_error;
3738

38-
pub fn gather_loans_in_fn(bccx: &BorrowckCtxt,
39-
decl: &ast::FnDecl,
40-
body: &ast::Block)
41-
-> (Vec<Loan>, move_data::MoveData)
39+
pub fn gather_loans_in_fn<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
40+
decl: &ast::FnDecl,
41+
body: &ast::Block)
42+
-> (Vec<Loan<'tcx>>, move_data::MoveData<'tcx>)
4243
{
4344
let mut glcx = GatherLoanCtxt {
4445
bccx: bccx,
@@ -60,9 +61,9 @@ pub fn gather_loans_in_fn(bccx: &BorrowckCtxt,
6061

6162
struct GatherLoanCtxt<'a, 'tcx: 'a> {
6263
bccx: &'a BorrowckCtxt<'a, 'tcx>,
63-
move_data: move_data::MoveData,
64+
move_data: move_data::MoveData<'tcx>,
6465
move_error_collector: move_error::MoveErrorCollector<'tcx>,
65-
all_loans: Vec<Loan>,
66+
all_loans: Vec<Loan<'tcx>>,
6667
/// `item_ub` is used as an upper-bound on the lifetime whenever we
6768
/// ask for the scope of an expression categorized as an upvar.
6869
item_ub: region::CodeExtent,
@@ -395,7 +396,7 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
395396
//! For mutable loans of content whose mutability derives
396397
//! from a local variable, mark the mutability decl as necessary.
397398
398-
match *loan_path {
399+
match loan_path.kind {
399400
LpVar(local_id) |
400401
LpUpvar(ty::UpvarId{ var_id: local_id, closure_expr_id: _ }) => {
401402
self.tcx().used_mut_nodes.borrow_mut().insert(local_id);
@@ -427,7 +428,7 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
427428
}
428429
}
429430

430-
pub fn compute_kill_scope(&self, loan_scope: region::CodeExtent, lp: &LoanPath)
431+
pub fn compute_kill_scope(&self, loan_scope: region::CodeExtent, lp: &LoanPath<'tcx>)
431432
-> region::CodeExtent {
432433
//! Determine when the loan restrictions go out of scope.
433434
//! This is either when the lifetime expires or when the

0 commit comments

Comments
 (0)