check_loans.rs 23.0 KB
Newer Older
1 2 3 4 5 6 7 8 9
// ----------------------------------------------------------------------
// Checking loans
//
// Phase 2 of check: we walk down the tree and check that:
// 1. assignments are always made to mutable locations;
// 2. loans made in overlapping scopes do not conflict
// 3. assignments do not affect things loaned out as immutable
// 4. moves to dnot affect things loaned out in any way

P
Patrick Walton 已提交
10
use dvec::DVec;
11 12 13 14 15 16 17

export check_loans;

enum check_loan_ctxt = @{
    bccx: borrowck_ctxt,
    req_maps: req_maps,

B
Brian Anderson 已提交
18
    reported: HashMap<ast::node_id, ()>,
19 20

    mut declared_purity: ast::purity,
21
    mut fn_args: @~[ast::node_id]
22 23 24 25 26 27 28 29 30 31 32 33 34
};

// if we are enforcing purity, why are we doing so?
enum purity_cause {
    // enforcing purity because fn was declared pure:
    pc_pure_fn,

    // enforce purity because we need to guarantee the
    // validity of some alias; `bckerr` describes the
    // reason we needed to enforce purity.
    pc_cmt(bckerr)
}

35
impl purity_cause : cmp::Eq {
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    pure fn eq(&self, other: &purity_cause) -> bool {
        match (*self) {
            pc_pure_fn => {
                match (*other) {
                    pc_pure_fn => true,
                    _ => false
                }
            }
            pc_cmt(e0a) => {
                match (*other) {
                    pc_cmt(e0b) => e0a == e0b,
                    _ => false
                }
            }
        }
    }
    pure fn ne(&self, other: &purity_cause) -> bool { !(*self).eq(other) }
53
}
54

55 56 57 58 59
fn check_loans(bccx: borrowck_ctxt,
               req_maps: req_maps,
               crate: @ast::crate) {
    let clcx = check_loan_ctxt(@{bccx: bccx,
                                 req_maps: req_maps,
60
                                 reported: HashMap(),
61
                                 mut declared_purity: ast::impure_fn,
62
                                 mut fn_args: @~[]});
63
    let vt = visit::mk_vt(@{visit_expr: check_loans_in_expr,
64
                            visit_local: check_loans_in_local,
65
                            visit_block: check_loans_in_block,
B
Brian Anderson 已提交
66 67
                            visit_fn: check_loans_in_fn,
                            .. *visit::default_visitor()});
68 69 70 71 72
    visit::visit_crate(*crate, clcx, vt);
}

enum assignment_type {
    at_straight_up,
73
    at_swap
74 75
}

76
impl assignment_type : cmp::Eq {
77 78 79 80
    pure fn eq(&self, other: &assignment_type) -> bool {
        ((*self) as uint) == ((*other) as uint)
    }
    pure fn ne(&self, other: &assignment_type) -> bool { !(*self).eq(other) }
81
}
82

B
Brian Anderson 已提交
83
impl assignment_type {
84 85 86
    fn checked_by_liveness() -> bool {
        // the liveness pass guarantees that immutable local variables
        // are only assigned once; but it doesn't consider &mut
87
        match self {
B
Brian Anderson 已提交
88
          at_straight_up => true,
89
          at_swap => true
90 91
        }
    }
92
    fn ing_form(desc: ~str) -> ~str {
93
        match self {
B
Brian Anderson 已提交
94
          at_straight_up => ~"assigning to " + desc,
95
          at_swap => ~"swapping to and from " + desc
96 97 98 99
        }
    }
}

B
Brian Anderson 已提交
100
impl check_loan_ctxt {
101 102
    fn tcx() -> ty::ctxt { self.bccx.tcx }

B
Brian Anderson 已提交
103
    fn purity(scope_id: ast::node_id) -> Option<purity_cause> {
104
        let default_purity = match self.declared_purity {
105
          // an unsafe declaration overrides all
B
Brian Anderson 已提交
106
          ast::unsafe_fn => return None,
107 108 109 110

          // otherwise, remember what was declared as the
          // default, but we must scan for requirements
          // imposed by the borrow check
B
Brian Anderson 已提交
111 112
          ast::pure_fn => Some(pc_pure_fn),
          ast::extern_fn | ast::impure_fn => None
113 114 115 116 117 118 119 120 121
        };

        // scan to see if this scope or any enclosing scope requires
        // purity.  if so, that overrides the declaration.

        let mut scope_id = scope_id;
        let region_map = self.tcx().region_map;
        let pure_map = self.req_maps.pure_map;
        loop {
122
            match pure_map.find(scope_id) {
B
Brian Anderson 已提交
123 124
              None => (),
              Some(e) => return Some(pc_cmt(e))
125 126
            }

127
            match region_map.find(scope_id) {
B
Brian Anderson 已提交
128 129
              None => return default_purity,
              Some(next_scope_id) => scope_id = next_scope_id
130 131 132 133
            }
        }
    }

134
    fn walk_loans(scope_id: ast::node_id, f: fn(v: &Loan) -> bool) {
135 136 137 138 139
        let mut scope_id = scope_id;
        let region_map = self.tcx().region_map;
        let req_loan_map = self.req_maps.req_loan_map;

        loop {
140 141 142
            for req_loan_map.find(scope_id).each |loans| {
                for loans.each |loan| {
                    if !f(loan) { return; }
143 144 145
                }
            }

146
            match region_map.find(scope_id) {
B
Brian Anderson 已提交
147 148
              None => return,
              Some(next_scope_id) => scope_id = next_scope_id,
149 150 151 152 153 154
            }
        }
    }

    fn walk_loans_of(scope_id: ast::node_id,
                     lp: @loan_path,
155
                     f: fn(v: &Loan) -> bool) {
B
Brian Anderson 已提交
156
        for self.walk_loans(scope_id) |loan| {
157
            if loan.lp == lp {
B
Brian Anderson 已提交
158
                if !f(loan) { return; }
159 160 161 162 163 164
            }
        }
    }

    // when we are in a pure context, we check each call to ensure
    // that the function which is invoked is itself pure.
165 166 167 168 169
    //
    // note: we take opt_expr and expr_id separately because for
    // overloaded operators the callee has an id but no expr.
    // annoying.
    fn check_pure_callee_or_arg(pc: purity_cause,
B
Brian Anderson 已提交
170
                                opt_expr: Option<@ast::expr>,
171 172
                                callee_id: ast::node_id,
                                callee_span: span) {
173 174
        let tcx = self.tcx();

P
Paul Stansifer 已提交
175
        debug!("check_pure_callee_or_arg(pc=%?, expr=%?, \
176 177
                callee_id=%d, ty=%s)",
               pc,
178
               opt_expr.map(|e| pprust::expr_to_str(*e, tcx.sess.intr()) ),
179
               callee_id,
P
Paul Stansifer 已提交
180
               ty_to_str(self.tcx(), ty::node_id_to_type(tcx, callee_id)));
181 182 183 184 185 186 187 188 189 190

        // Purity rules: an expr B is a legal callee or argument to a
        // call within a pure function A if at least one of the
        // following holds:
        //
        // (a) A was declared pure and B is one of its arguments;
        // (b) B is a stack closure;
        // (c) B is a pure fn;
        // (d) B is not a fn.

191
        match opt_expr {
B
Brian Anderson 已提交
192
          Some(expr) => {
193
            match expr.node {
B
Brian Anderson 已提交
194
              ast::expr_path(_) if pc == pc_pure_fn => {
195 196 197 198
                let def = self.tcx().def_map.get(expr.id);
                let did = ast_util::def_id_of_def(def);
                let is_fn_arg =
                    did.crate == ast::local_crate &&
T
Tim Chevalier 已提交
199
                    (*self.fn_args).contains(&(did.node));
B
Brian Anderson 已提交
200
                if is_fn_arg { return; } // case (a) above
201 202
              }
              ast::expr_fn_block(*) | ast::expr_fn(*) |
B
Brian Anderson 已提交
203
              ast::expr_loop_body(*) | ast::expr_do_body(*) => {
B
Brian Anderson 已提交
204 205 206 207
                if self.is_stack_closure(expr.id) {
                    // case (b) above
                    return;
                }
208
              }
B
Brian Anderson 已提交
209
              _ => ()
210
            }
211
          }
B
Brian Anderson 已提交
212
          None => ()
213 214
        }

215
        let callee_ty = ty::node_id_to_type(tcx, callee_id);
216
        match ty::get(callee_ty).sty {
B
Brian Anderson 已提交
217
          ty::ty_fn(fn_ty) => {
218
            match fn_ty.meta.purity {
B
Brian Anderson 已提交
219 220
              ast::pure_fn => return, // case (c) above
              ast::impure_fn | ast::unsafe_fn | ast::extern_fn => {
221
                self.report_purity_error(
222
                    pc, callee_span,
P
Paul Stansifer 已提交
223
                    fmt!("access to %s function",
224
                         pprust::purity_to_str(fn_ty.meta.purity)));
225 226 227
              }
            }
          }
B
Brian Anderson 已提交
228
          _ => return, // case (d) above
229 230 231 232 233 234 235 236
        }
    }

    // True if the expression with the given `id` is a stack closure.
    // The expression must be an expr_fn(*) or expr_fn_block(*)
    fn is_stack_closure(id: ast::node_id) -> bool {
        let fn_ty = ty::node_id_to_type(self.tcx(), id);
        let proto = ty::ty_fn_proto(fn_ty);
237
        return proto == ast::ProtoBorrowed;
238 239 240
    }

    fn is_allowed_pure_arg(expr: @ast::expr) -> bool {
241
        return match expr.node {
B
Brian Anderson 已提交
242
          ast::expr_path(_) => {
243 244
            let def = self.tcx().def_map.get(expr.id);
            let did = ast_util::def_id_of_def(def);
245
            did.crate == ast::local_crate &&
T
Tim Chevalier 已提交
246
                (*self.fn_args).contains(&(did.node))
247
          }
B
Brian Anderson 已提交
248
          ast::expr_fn_block(*) | ast::expr_fn(*) => {
249 250
            self.is_stack_closure(expr.id)
          }
B
Brian Anderson 已提交
251
          _ => false
252 253 254 255
        };
    }

    fn check_for_conflicting_loans(scope_id: ast::node_id) {
256 257 258
        debug!("check_for_conflicting_loans(scope_id=%?)", scope_id);

        let new_loans = match self.req_maps.req_loan_map.find(scope_id) {
B
Brian Anderson 已提交
259
            None => return,
260
            Some(loans) => loans
261 262
        };

263 264
        debug!("new_loans has length %?", new_loans.len());

265
        let par_scope_id = self.tcx().region_map.get(scope_id);
B
Brian Anderson 已提交
266
        for self.walk_loans(par_scope_id) |old_loan| {
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
            debug!("old_loan=%?", self.bccx.loan_to_repr(old_loan));

            for new_loans.each |new_loan| {
                self.report_error_if_loans_conflict(old_loan, new_loan);
            }
        }

        let len = new_loans.len();
        for uint::range(0, len) |i| {
            let loan_i = new_loans[i];
            for uint::range(i+1, len) |j| {
                let loan_j = new_loans[j];
                self.report_error_if_loans_conflict(&loan_i, &loan_j);
            }
        }
    }

    fn report_error_if_loans_conflict(&self,
                                      old_loan: &Loan,
                                      new_loan: &Loan) {
        if old_loan.lp != new_loan.lp {
            return;
        }

        match (old_loan.mutbl, new_loan.mutbl) {
            (m_const, _) | (_, m_const) |
            (m_mutbl, m_mutbl) | (m_imm, m_imm) => {
                /*ok*/
            }

            (m_mutbl, m_imm) | (m_imm, m_mutbl) => {
                self.bccx.span_err(
                    new_loan.cmt.span,
                    fmt!("loan of %s as %s \
                          conflicts with prior loan",
                         self.bccx.cmt_to_str(new_loan.cmt),
                         self.bccx.mut_to_str(new_loan.mutbl)));
                self.bccx.span_note(
                    old_loan.cmt.span,
                    fmt!("prior loan as %s granted here",
                         self.bccx.mut_to_str(old_loan.mutbl)));
308 309 310 311 312
            }
        }
    }

    fn is_local_variable(cmt: cmt) -> bool {
313
        match cmt.cat {
B
Brian Anderson 已提交
314 315
          cat_local(_) => true,
          _ => false
316 317 318 319
        }
    }

    fn is_self_field(cmt: cmt) -> bool {
320
        match cmt.cat {
B
Brian Anderson 已提交
321
          cat_comp(cmt_base, comp_field(*)) => {
322
            match cmt_base.cat {
B
Brian Anderson 已提交
323 324
              cat_special(sk_self) => true,
              _ => false
325 326
            }
          }
B
Brian Anderson 已提交
327
          _ => false
328 329 330 331
        }
    }

    fn check_assignment(at: assignment_type, ex: @ast::expr) {
332 333 334 335 336 337
        // We don't use cat_expr() here because we don't want to treat
        // auto-ref'd parameters in overloaded operators as rvalues.
        let cmt = match self.bccx.tcx.adjustments.find(ex.id) {
            None => self.bccx.cat_expr_unadjusted(ex),
            Some(adj) => self.bccx.cat_expr_autoderefd(ex, adj)
        };
338

P
Paul Stansifer 已提交
339 340
        debug!("check_assignment(cmt=%s)",
               self.bccx.cmt_to_repr(cmt));
341

342
        if self.is_local_variable(cmt) && at.checked_by_liveness() {
343 344 345
            // liveness guarantees that immutable local variables
            // are only assigned once
        } else {
346
            match cmt.mutbl {
B
Brian Anderson 已提交
347 348
              m_mutbl => { /*ok*/ }
              m_const | m_imm => {
349 350 351
                self.bccx.span_err(
                    ex.span,
                    at.ing_form(self.bccx.cmt_to_str(cmt)));
B
Brian Anderson 已提交
352
                return;
353 354 355 356 357 358 359
              }
            }
        }

        // if this is a pure function, only loan-able state can be
        // assigned, because it is uniquely tied to this function and
        // is not visible from the outside
360
        match self.purity(ex.id) {
B
Brian Anderson 已提交
361 362
          None => (),
          Some(pc @ pc_cmt(_)) => {
363 364 365 366 367 368
            // Subtle: Issue #3162.  If we are enforcing purity
            // because there is a reference to aliasable, mutable data
            // that we require to be immutable, we can't allow writes
            // even to data owned by the current stack frame.  This is
            // because that aliasable data might have been located on
            // the current stack frame, we don't know.
369 370
            self.report_purity_error(
                pc, ex.span, at.ing_form(self.bccx.cmt_to_str(cmt)));
371
          }
B
Brian Anderson 已提交
372
          Some(pc_pure_fn) => {
373 374 375 376
            if cmt.lp.is_none() {
                self.report_purity_error(
                    pc_pure_fn, ex.span,
                    at.ing_form(self.bccx.cmt_to_str(cmt)));
377 378 379 380 381 382 383 384
            }
          }
        }

        // check for a conflicting loan as well, except in the case of
        // taking a mutable ref.  that will create a loan of its own
        // which will be checked for compat separately in
        // check_for_conflicting_loans()
385 386
        for cmt.lp.each |lp| {
            self.check_for_loan_conflicting_with_assignment(
387
                at, ex, cmt, *lp);
388 389 390 391 392
        }

        self.bccx.add_to_mutbl_map(cmt);
    }

393 394 395 396 397 398
    fn check_for_loan_conflicting_with_assignment(
        at: assignment_type,
        ex: @ast::expr,
        cmt: cmt,
        lp: @loan_path) {

B
Brian Anderson 已提交
399
        for self.walk_loans_of(ex.id, lp) |loan| {
400
            match loan.mutbl {
B
Brian Anderson 已提交
401 402
              m_mutbl | m_const => { /*ok*/ }
              m_imm => {
403 404
                self.bccx.span_err(
                    ex.span,
P
Paul Stansifer 已提交
405 406
                    fmt!("%s prohibited due to outstanding loan",
                         at.ing_form(self.bccx.cmt_to_str(cmt))));
407 408
                self.bccx.span_note(
                    loan.cmt.span,
P
Paul Stansifer 已提交
409 410
                    fmt!("loan of %s granted here",
                         self.bccx.cmt_to_str(loan.cmt)));
B
Brian Anderson 已提交
411
                return;
412 413 414 415 416 417 418 419
              }
            }
        }

        // Subtle: if the mutability of the component being assigned
        // is inherited from the thing that the component is embedded
        // within, then we have to check whether that thing has been
        // loaned out as immutable!  An example:
B
Brian Anderson 已提交
420
        //    let mut x = {f: Some(3)};
421 422
        //    let y = &x; // x loaned out as immutable
        //    x.f = none; // changes type of y.f, which appears to be imm
423
        match *lp {
B
Brian Anderson 已提交
424
          lp_comp(lp_base, ck) if inherent_mutability(ck) != m_mutbl => {
425 426 427
            self.check_for_loan_conflicting_with_assignment(
                at, ex, cmt, lp_base);
          }
B
Brian Anderson 已提交
428
          lp_comp(*) | lp_local(*) | lp_arg(*) | lp_deref(*) => ()
429 430 431
        }
    }

432
    fn report_purity_error(pc: purity_cause, sp: span, msg: ~str) {
433
        match pc {
B
Brian Anderson 已提交
434
          pc_pure_fn => {
435 436
            self.tcx().sess.span_err(
                sp,
P
Paul Stansifer 已提交
437
                fmt!("%s prohibited in pure context", msg));
438
          }
B
Brian Anderson 已提交
439
          pc_cmt(e) => {
440 441 442
            if self.reported.insert(e.cmt.id, ()) {
                self.tcx().sess.span_err(
                    e.cmt.span,
P
Paul Stansifer 已提交
443
                    fmt!("illegal borrow unless pure: %s",
444 445
                         self.bccx.bckerr_to_str(e)));
                self.bccx.note_and_explain_bckerr(e);
446 447
                self.tcx().sess.span_note(
                    sp,
P
Paul Stansifer 已提交
448
                    fmt!("impure due to %s", msg));
449 450 451 452 453 454 455 456 457 458 459
            }
          }
        }
    }

    fn check_move_out(ex: @ast::expr) {
        let cmt = self.bccx.cat_expr(ex);
        self.check_move_out_from_cmt(cmt);
    }

    fn check_move_out_from_cmt(cmt: cmt) {
P
Paul Stansifer 已提交
460 461
        debug!("check_move_out_from_cmt(cmt=%s)",
               self.bccx.cmt_to_repr(cmt));
462

463
        match cmt.cat {
464
          // Rvalues, locals, and arguments can be moved:
B
Brian Anderson 已提交
465
          cat_rvalue | cat_local(_) | cat_arg(_) => {}
466 467 468 469

          // We allow moving out of static items because the old code
          // did.  This seems consistent with permitting moves out of
          // rvalues, I guess.
B
Brian Anderson 已提交
470
          cat_special(sk_static_item) => {}
471

B
Brian Anderson 已提交
472
          cat_deref(_, _, unsafe_ptr) => {}
473

474
          // Nothing else.
B
Brian Anderson 已提交
475
          _ => {
476 477
            self.bccx.span_err(
                cmt.span,
P
Paul Stansifer 已提交
478
                fmt!("moving out of %s", self.bccx.cmt_to_str(cmt)));
B
Brian Anderson 已提交
479
            return;
480 481 482 483 484 485
          }
        }

        self.bccx.add_to_mutbl_map(cmt);

        // check for a conflicting loan:
486
        let lp = match cmt.lp {
B
Brian Anderson 已提交
487 488
          None => return,
          Some(lp) => lp
489
        };
B
Brian Anderson 已提交
490
        for self.walk_loans_of(cmt.id, lp) |loan| {
491 492
            self.bccx.span_err(
                cmt.span,
P
Paul Stansifer 已提交
493 494
                fmt!("moving out of %s prohibited due to outstanding loan",
                     self.bccx.cmt_to_str(cmt)));
495 496
            self.bccx.span_note(
                loan.cmt.span,
P
Paul Stansifer 已提交
497 498
                fmt!("loan of %s granted here",
                     self.bccx.cmt_to_str(loan.cmt)));
B
Brian Anderson 已提交
499
            return;
500 501
        }
    }
502

503 504 505 506
    // Very subtle (#2633): liveness can mark options as last_use even
    // when there is an outstanding loan.  In that case, it is not
    // safe to consider the use a last_use.
    fn check_last_use(expr: @ast::expr) {
507
        debug!("Checking last use of expr %?", expr.id);
508
        let cmt = self.bccx.cat_expr(expr);
509
        let lp = match cmt.lp {
510 511 512 513 514
            None => {
                debug!("Not a loanable expression");
                return;
            }
            Some(lp) => lp
515
        };
B
Brian Anderson 已提交
516
        for self.walk_loans_of(cmt.id, lp) |_loan| {
P
Paul Stansifer 已提交
517 518
            debug!("Removing last use entry %? due to outstanding loan",
                   expr.id);
519
            self.bccx.last_use_map.remove(expr.id);
B
Brian Anderson 已提交
520
            return;
521 522 523
        }
    }

524
    fn check_call(expr: @ast::expr,
B
Brian Anderson 已提交
525
                  callee: Option<@ast::expr>,
526 527
                  callee_id: ast::node_id,
                  callee_span: span,
528
                  args: ~[@ast::expr]) {
529
        match self.purity(expr.id) {
B
Brian Anderson 已提交
530 531
          None => {}
          Some(pc) => {
532 533
            self.check_pure_callee_or_arg(
                pc, callee, callee_id, callee_span);
B
Brian Anderson 已提交
534
            for args.each |arg| {
535
                self.check_pure_callee_or_arg(
536
                    pc, Some(*arg), arg.id, arg.span);
537 538 539 540 541 542
            }
          }
        }
        let arg_tys =
            ty::ty_fn_args(
                ty::node_id_to_type(self.tcx(), callee_id));
543
        for vec::each2(args, arg_tys) |arg, arg_ty| {
544
            match ty::resolved_mode(self.tcx(), arg_ty.mode) {
545
                ast::by_move => {
N
Niko Matsakis 已提交
546
                    self.check_move_out(*arg);
547
                }
548
                ast::by_ref |
T
Tim Chevalier 已提交
549
                ast::by_copy | ast::by_val => {
550
                }
551 552 553
            }
        }
    }
554 555 556 557 558 559
}

fn check_loans_in_fn(fk: visit::fn_kind, decl: ast::fn_decl, body: ast::blk,
                     sp: span, id: ast::node_id, &&self: check_loan_ctxt,
                     visitor: visit::vt<check_loan_ctxt>) {

P
Paul Stansifer 已提交
560
    debug!("purity on entry=%?", copy self.declared_purity);
561 562 563 564 565 566 567 568 569 570 571 572
    do save_and_restore(&mut(self.declared_purity)) {
        do save_and_restore(&mut(self.fn_args)) {
            let is_stack_closure = self.is_stack_closure(id);
            let fty = ty::node_id_to_type(self.tcx(), id);
            self.declared_purity = ty::determine_inherited_purity(
                copy self.declared_purity,
                ty::ty_fn_purity(fty),
                ty::ty_fn_proto(fty));

            match fk {
                visit::fk_anon(*) |
                visit::fk_fn_block(*) if is_stack_closure => {
573
                    // inherits the fn_args from enclosing ctxt
574 575 576 577
                }
                visit::fk_anon(*) | visit::fk_fn_block(*) |
                visit::fk_method(*) | visit::fk_item_fn(*) |
                visit::fk_dtor(*) => {
578 579 580 581 582 583 584 585 586 587 588 589 590 591
                    let mut fn_args = ~[];
                    for decl.inputs.each |input| {
                        // For the purposes of purity, only consider function-
                        // typed bindings in trivial patterns to be function
                        // arguments. For example, do not allow `f` and `g` in
                        // (f, g): (&fn(), &fn()) to be called.
                        match input.pat.node {
                            ast::pat_ident(_, _, None) => {
                                fn_args.push(input.pat.id);
                            }
                            _ => {} // Ignore this argument.
                        }
                    }
                    self.fn_args = @move fn_args;
592
                }
T
Tim Chevalier 已提交
593
            }
594 595

            visit::visit_fn(fk, decl, body, sp, id, self, visitor);
596 597
        }
    }
P
Paul Stansifer 已提交
598
    debug!("purity on exit=%?", copy self.declared_purity);
599 600
}

601 602 603 604 605 606
fn check_loans_in_local(local: @ast::local,
                        &&self: check_loan_ctxt,
                        vt: visit::vt<check_loan_ctxt>) {
    visit::visit_local(local, self, vt);
}

607 608 609
fn check_loans_in_expr(expr: @ast::expr,
                       &&self: check_loan_ctxt,
                       vt: visit::vt<check_loan_ctxt>) {
610 611 612
    debug!("check_loans_in_expr(expr=%?/%s)",
           expr.id, pprust::expr_to_str(expr, self.tcx().sess.intr()));

613 614
    self.check_for_conflicting_loans(expr.id);

615
    match expr.node {
B
Brian Anderson 已提交
616
      ast::expr_path(*) if self.bccx.last_use_map.contains_key(expr.id) => {
617 618 619
        self.check_last_use(expr);
      }

B
Brian Anderson 已提交
620
      ast::expr_swap(l, r) => {
621 622 623
        self.check_assignment(at_swap, l);
        self.check_assignment(at_swap, r);
      }
B
Brian Anderson 已提交
624
      ast::expr_unary_move(src) => {
625 626
        self.check_move_out(src);
      }
627
      ast::expr_assign(dest, _) |
B
Brian Anderson 已提交
628
      ast::expr_assign_op(_, dest, _) => {
629 630 631
        self.check_assignment(at_straight_up, dest);
      }
      ast::expr_fn(_, _, _, cap_clause) |
B
Brian Anderson 已提交
632
      ast::expr_fn_block(_, _, cap_clause) => {
B
Brian Anderson 已提交
633
        for (*cap_clause).each |cap_item| {
634 635 636 637 638 639 640 641 642 643 644 645
            if cap_item.is_move {
                let def = self.tcx().def_map.get(cap_item.id);

                // Hack: the type that is used in the cmt doesn't actually
                // matter here, so just subst nil instead of looking up
                // the type of the def that is referred to
                let cmt = self.bccx.cat_def(cap_item.id, cap_item.span,
                                            ty::mk_nil(self.tcx()), def);
                self.check_move_out_from_cmt(cmt);
            }
        }
      }
B
Brian Anderson 已提交
646
      ast::expr_call(f, args, _) => {
B
Brian Anderson 已提交
647
        self.check_call(expr, Some(f), f.id, f.span, args);
648 649 650
      }
      ast::expr_index(_, rval) |
      ast::expr_binary(_, _, rval)
B
Brian Anderson 已提交
651
      if self.bccx.method_map.contains_key(expr.id) => {
652
        self.check_call(expr,
B
Brian Anderson 已提交
653
                        None,
T
Tim Chevalier 已提交
654
                        expr.callee_id,
655
                        expr.span,
656
                        ~[rval]);
657
      }
658
      ast::expr_unary(*) | ast::expr_index(*)
B
Brian Anderson 已提交
659
      if self.bccx.method_map.contains_key(expr.id) => {
660
        self.check_call(expr,
B
Brian Anderson 已提交
661
                        None,
T
Tim Chevalier 已提交
662
                        expr.callee_id,
663
                        expr.span,
664
                        ~[]);
665
      }
B
Brian Anderson 已提交
666
      _ => { }
667 668 669 670 671 672 673 674
    }

    visit::visit_expr(expr, self, vt);
}

fn check_loans_in_block(blk: ast::blk,
                        &&self: check_loan_ctxt,
                        vt: visit::vt<check_loan_ctxt>) {
T
Tim Chevalier 已提交
675
    do save_and_restore(&mut(self.declared_purity)) {
676 677
        self.check_for_conflicting_loans(blk.node.id);

678
        match blk.node.rules {
B
Brian Anderson 已提交
679
          ast::default_blk => {
680
          }
B
Brian Anderson 已提交
681
          ast::unsafe_blk => {
682 683 684 685 686 687 688 689
            self.declared_purity = ast::unsafe_fn;
          }
        }

        visit::visit_block(blk, self, vt);
    }
}