]> git.lizzy.rs Git - rust.git/blob - src/librustc/middle/expr_use_visitor.rs
Removed some unnecessary RefCells from resolve
[rust.git] / src / librustc / middle / expr_use_visitor.rs
1 // Copyright 2014 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 /*!
12  * A different sort of visitor for walking fn bodies.  Unlike the
13  * normal visitor, which just walks the entire body in one shot, the
14  * `ExprUseVisitor` determines how expressions are being used.
15  */
16
17 use middle::mem_categorization as mc;
18 use middle::def;
19 use middle::mem_categorization::Typer;
20 use middle::pat_util;
21 use middle::ty;
22 use middle::typeck::{MethodCall, MethodObject, MethodTraitObject};
23 use middle::typeck::{MethodOrigin, MethodParam, MethodTypeParam};
24 use middle::typeck::{MethodStatic, MethodStaticUnboxedClosure};
25 use middle::typeck;
26 use util::ppaux::Repr;
27
28 use syntax::ast;
29 use syntax::ptr::P;
30 use syntax::codemap::Span;
31
32 ///////////////////////////////////////////////////////////////////////////
33 // The Delegate trait
34
35 /// This trait defines the callbacks you can expect to receive when
36 /// employing the ExprUseVisitor.
37 pub trait Delegate {
38     // The value found at `cmt` is either copied or moved, depending
39     // on mode.
40     fn consume(&mut self,
41                consume_id: ast::NodeId,
42                consume_span: Span,
43                cmt: mc::cmt,
44                mode: ConsumeMode);
45
46     // The value found at `cmt` is either copied or moved via the
47     // pattern binding `consume_pat`, depending on mode.
48     fn consume_pat(&mut self,
49                    consume_pat: &ast::Pat,
50                    cmt: mc::cmt,
51                    mode: ConsumeMode);
52
53     // The value found at `borrow` is being borrowed at the point
54     // `borrow_id` for the region `loan_region` with kind `bk`.
55     fn borrow(&mut self,
56               borrow_id: ast::NodeId,
57               borrow_span: Span,
58               cmt: mc::cmt,
59               loan_region: ty::Region,
60               bk: ty::BorrowKind,
61               loan_cause: LoanCause);
62
63     // The local variable `id` is declared but not initialized.
64     fn decl_without_init(&mut self,
65                          id: ast::NodeId,
66                          span: Span);
67
68     // The path at `cmt` is being assigned to.
69     fn mutate(&mut self,
70               assignment_id: ast::NodeId,
71               assignment_span: Span,
72               assignee_cmt: mc::cmt,
73               mode: MutateMode);
74 }
75
76 #[deriving(PartialEq)]
77 pub enum LoanCause {
78     ClosureCapture(Span),
79     AddrOf,
80     AutoRef,
81     RefBinding,
82     OverloadedOperator,
83     ClosureInvocation,
84     ForLoop,
85     MatchDiscriminant
86 }
87
88 #[deriving(PartialEq,Show)]
89 pub enum ConsumeMode {
90     Copy,                // reference to x where x has a type that copies
91     Move(MoveReason),    // reference to x where x has a type that moves
92 }
93
94 #[deriving(PartialEq,Show)]
95 pub enum MoveReason {
96     DirectRefMove,
97     PatBindingMove,
98     CaptureMove,
99 }
100
101 #[deriving(PartialEq,Show)]
102 pub enum MutateMode {
103     Init,
104     JustWrite,    // x = y
105     WriteAndRead, // x += y
106 }
107
108 enum OverloadedCallType {
109     FnOverloadedCall,
110     FnMutOverloadedCall,
111     FnOnceOverloadedCall,
112 }
113
114 impl OverloadedCallType {
115     fn from_trait_id(tcx: &ty::ctxt, trait_id: ast::DefId)
116                      -> OverloadedCallType {
117         for &(maybe_function_trait, overloaded_call_type) in [
118             (tcx.lang_items.fn_once_trait(), FnOnceOverloadedCall),
119             (tcx.lang_items.fn_mut_trait(), FnMutOverloadedCall),
120             (tcx.lang_items.fn_trait(), FnOverloadedCall)
121         ].iter() {
122             match maybe_function_trait {
123                 Some(function_trait) if function_trait == trait_id => {
124                     return overloaded_call_type
125                 }
126                 _ => continue,
127             }
128         }
129
130         tcx.sess.bug("overloaded call didn't map to known function trait")
131     }
132
133     fn from_method_id(tcx: &ty::ctxt, method_id: ast::DefId)
134                       -> OverloadedCallType {
135         let method_descriptor = match ty::impl_or_trait_item(tcx, method_id) {
136             ty::MethodTraitItem(ref method_descriptor) => {
137                 (*method_descriptor).clone()
138             }
139             ty::TypeTraitItem(_) => {
140                 tcx.sess.bug("overloaded call method wasn't in method map")
141             }
142         };
143         let impl_id = match method_descriptor.container {
144             ty::TraitContainer(_) => {
145                 tcx.sess.bug("statically resolved overloaded call method \
146                               belonged to a trait?!")
147             }
148             ty::ImplContainer(impl_id) => impl_id,
149         };
150         let trait_ref = match ty::impl_trait_ref(tcx, impl_id) {
151             None => {
152                 tcx.sess.bug("statically resolved overloaded call impl \
153                               didn't implement a trait?!")
154             }
155             Some(ref trait_ref) => (*trait_ref).clone(),
156         };
157         OverloadedCallType::from_trait_id(tcx, trait_ref.def_id)
158     }
159
160     fn from_unboxed_closure(tcx: &ty::ctxt, closure_did: ast::DefId)
161                             -> OverloadedCallType {
162         let trait_did =
163             tcx.unboxed_closures
164                .borrow()
165                .find(&closure_did)
166                .expect("OverloadedCallType::from_unboxed_closure: didn't \
167                         find closure id")
168                .kind
169                .trait_did(tcx);
170         OverloadedCallType::from_trait_id(tcx, trait_did)
171     }
172
173     fn from_method_origin(tcx: &ty::ctxt, origin: &MethodOrigin)
174                           -> OverloadedCallType {
175         match *origin {
176             MethodStatic(def_id) => {
177                 OverloadedCallType::from_method_id(tcx, def_id)
178             }
179             MethodStaticUnboxedClosure(def_id) => {
180                 OverloadedCallType::from_unboxed_closure(tcx, def_id)
181             }
182             MethodTypeParam(MethodParam { trait_ref: ref trait_ref, .. }) |
183             MethodTraitObject(MethodObject { trait_ref: ref trait_ref, .. }) => {
184                 OverloadedCallType::from_trait_id(tcx, trait_ref.def_id)
185             }
186         }
187     }
188 }
189
190 ///////////////////////////////////////////////////////////////////////////
191 // The ExprUseVisitor type
192 //
193 // This is the code that actually walks the tree. Like
194 // mem_categorization, it requires a TYPER, which is a type that
195 // supplies types from the tree. After type checking is complete, you
196 // can just use the tcx as the typer.
197
198 pub struct ExprUseVisitor<'d,'t,TYPER:'t> {
199     typer: &'t TYPER,
200     mc: mc::MemCategorizationContext<'t,TYPER>,
201     delegate: &'d mut Delegate+'d,
202 }
203
204 // If the TYPER results in an error, it's because the type check
205 // failed (or will fail, when the error is uncovered and reported
206 // during writeback). In this case, we just ignore this part of the
207 // code.
208 //
209 // Note that this macro appears similar to try!(), but, unlike try!(),
210 // it does not propagate the error.
211 macro_rules! return_if_err(
212     ($inp: expr) => (
213         match $inp {
214             Ok(v) => v,
215             Err(()) => return
216         }
217     )
218 )
219
220 impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,TYPER> {
221     pub fn new(delegate: &'d mut Delegate,
222                typer: &'t TYPER)
223                -> ExprUseVisitor<'d,'t,TYPER> {
224         ExprUseVisitor { typer: typer,
225                          mc: mc::MemCategorizationContext::new(typer),
226                          delegate: delegate }
227     }
228
229     pub fn walk_fn(&mut self,
230                    decl: &ast::FnDecl,
231                    body: &ast::Block) {
232         self.walk_arg_patterns(decl, body);
233         self.walk_block(body);
234     }
235
236     fn walk_arg_patterns(&mut self,
237                          decl: &ast::FnDecl,
238                          body: &ast::Block) {
239         for arg in decl.inputs.iter() {
240             let arg_ty = return_if_err!(self.typer.node_ty(arg.pat.id));
241
242             let arg_cmt = self.mc.cat_rvalue(
243                 arg.id,
244                 arg.pat.span,
245                 ty::ReScope(body.id), // Args live only as long as the fn body.
246                 arg_ty);
247
248             self.walk_pat(arg_cmt, &*arg.pat);
249         }
250     }
251
252     fn tcx(&self) -> &'t ty::ctxt<'tcx> {
253         self.typer.tcx()
254     }
255
256     fn delegate_consume(&mut self,
257                         consume_id: ast::NodeId,
258                         consume_span: Span,
259                         cmt: mc::cmt) {
260         let mode = copy_or_move(self.tcx(), cmt.ty, DirectRefMove);
261         self.delegate.consume(consume_id, consume_span, cmt, mode);
262     }
263
264     fn consume_exprs(&mut self, exprs: &Vec<P<ast::Expr>>) {
265         for expr in exprs.iter() {
266             self.consume_expr(&**expr);
267         }
268     }
269
270     fn consume_expr(&mut self, expr: &ast::Expr) {
271         debug!("consume_expr(expr={})", expr.repr(self.tcx()));
272
273         let cmt = return_if_err!(self.mc.cat_expr(expr));
274         self.delegate_consume(expr.id, expr.span, cmt);
275         self.walk_expr(expr);
276     }
277
278     fn mutate_expr(&mut self,
279                    assignment_expr: &ast::Expr,
280                    expr: &ast::Expr,
281                    mode: MutateMode) {
282         let cmt = return_if_err!(self.mc.cat_expr(expr));
283         self.delegate.mutate(assignment_expr.id, assignment_expr.span, cmt, mode);
284         self.walk_expr(expr);
285     }
286
287     fn borrow_expr(&mut self,
288                    expr: &ast::Expr,
289                    r: ty::Region,
290                    bk: ty::BorrowKind,
291                    cause: LoanCause) {
292         debug!("borrow_expr(expr={}, r={}, bk={})",
293                expr.repr(self.tcx()), r.repr(self.tcx()), bk.repr(self.tcx()));
294
295         let cmt = return_if_err!(self.mc.cat_expr(expr));
296         self.delegate.borrow(expr.id, expr.span, cmt, r, bk, cause);
297
298         // Note: Unlike consume, we can ignore ExprParen. cat_expr
299         // already skips over them, and walk will uncover any
300         // attachments or whatever.
301         self.walk_expr(expr)
302     }
303
304     fn select_from_expr(&mut self, expr: &ast::Expr) {
305         self.walk_expr(expr)
306     }
307
308     pub fn walk_expr(&mut self, expr: &ast::Expr) {
309         debug!("walk_expr(expr={})", expr.repr(self.tcx()));
310
311         self.walk_adjustment(expr);
312
313         match expr.node {
314             ast::ExprParen(ref subexpr) => {
315                 self.walk_expr(&**subexpr)
316             }
317
318             ast::ExprPath(..) => { }
319
320             ast::ExprUnary(ast::UnDeref, ref base) => {      // *base
321                 if !self.walk_overloaded_operator(expr, &**base, Vec::new()) {
322                     self.select_from_expr(&**base);
323                 }
324             }
325
326             ast::ExprField(ref base, _, _) => {         // base.f
327                 self.select_from_expr(&**base);
328             }
329
330             ast::ExprTupField(ref base, _, _) => {         // base.<n>
331                 self.select_from_expr(&**base);
332             }
333
334             ast::ExprIndex(ref lhs, ref rhs) => {       // lhs[rhs]
335                 if !self.walk_overloaded_operator(expr, &**lhs, vec![&**rhs]) {
336                     self.select_from_expr(&**lhs);
337                     self.consume_expr(&**rhs);
338                 }
339             }
340
341             ast::ExprSlice(ref base, ref start, ref end, _) => {    // base[start..end]
342                 let args = match (start, end) {
343                     (&Some(ref e1), &Some(ref e2)) => vec![&**e1, &**e2],
344                     (&Some(ref e), &None) => vec![&**e],
345                     (&None, &Some(ref e)) => vec![&**e],
346                     (&None, &None) => Vec::new()
347                 };
348                 let overloaded = self.walk_overloaded_operator(expr, &**base, args);
349                 assert!(overloaded);
350             }
351
352             ast::ExprCall(ref callee, ref args) => {    // callee(args)
353                 self.walk_callee(expr, &**callee);
354                 self.consume_exprs(args);
355             }
356
357             ast::ExprMethodCall(_, _, ref args) => { // callee.m(args)
358                 self.consume_exprs(args);
359             }
360
361             ast::ExprStruct(_, ref fields, ref opt_with) => {
362                 self.walk_struct_expr(expr, fields, opt_with);
363             }
364
365             ast::ExprTup(ref exprs) => {
366                 self.consume_exprs(exprs);
367             }
368
369             ast::ExprIf(ref cond_expr, ref then_blk, ref opt_else_expr) => {
370                 self.consume_expr(&**cond_expr);
371                 self.walk_block(&**then_blk);
372                 for else_expr in opt_else_expr.iter() {
373                     self.consume_expr(&**else_expr);
374                 }
375             }
376
377             ast::ExprMatch(ref discr, ref arms) => {
378                 let discr_cmt = return_if_err!(self.mc.cat_expr(&**discr));
379                 self.borrow_expr(&**discr, ty::ReEmpty, ty::ImmBorrow, MatchDiscriminant);
380
381                 // treatment of the discriminant is handled while walking the arms.
382                 for arm in arms.iter() {
383                     self.walk_arm(discr_cmt.clone(), arm);
384                 }
385             }
386
387             ast::ExprVec(ref exprs) => {
388                 self.consume_exprs(exprs);
389             }
390
391             ast::ExprAddrOf(m, ref base) => {   // &base
392                 // make sure that the thing we are pointing out stays valid
393                 // for the lifetime `scope_r` of the resulting ptr:
394                 let expr_ty = ty::expr_ty(self.tcx(), expr);
395                 if !ty::type_is_bot(expr_ty) {
396                     let r = ty::ty_region(self.tcx(), expr.span, expr_ty);
397                     let bk = ty::BorrowKind::from_mutbl(m);
398                     self.borrow_expr(&**base, r, bk, AddrOf);
399                 } else {
400                     self.walk_expr(&**base);
401                 }
402             }
403
404             ast::ExprInlineAsm(ref ia) => {
405                 for &(_, ref input) in ia.inputs.iter() {
406                     self.consume_expr(&**input);
407                 }
408
409                 for &(_, ref output, is_rw) in ia.outputs.iter() {
410                     self.mutate_expr(expr, &**output,
411                                            if is_rw { WriteAndRead } else { JustWrite });
412                 }
413             }
414
415             ast::ExprBreak(..) |
416             ast::ExprAgain(..) |
417             ast::ExprLit(..) => {}
418
419             ast::ExprLoop(ref blk, _) => {
420                 self.walk_block(&**blk);
421             }
422
423             ast::ExprWhile(ref cond_expr, ref blk, _) => {
424                 self.consume_expr(&**cond_expr);
425                 self.walk_block(&**blk);
426             }
427
428             ast::ExprForLoop(ref pat, ref head, ref blk, _) => {
429                 // The pattern lives as long as the block.
430                 debug!("walk_expr for loop case: blk id={}", blk.id);
431                 self.consume_expr(&**head);
432
433                 // Fetch the type of the value that the iteration yields to
434                 // produce the pattern's categorized mutable type.
435                 let pattern_type = return_if_err!(self.typer.node_ty(pat.id));
436                 let pat_cmt = self.mc.cat_rvalue(pat.id,
437                                                  pat.span,
438                                                  ty::ReScope(blk.id),
439                                                  pattern_type);
440                 self.walk_pat(pat_cmt, &**pat);
441
442                 self.walk_block(&**blk);
443             }
444
445             ast::ExprUnary(_, ref lhs) => {
446                 if !self.walk_overloaded_operator(expr, &**lhs, Vec::new()) {
447                     self.consume_expr(&**lhs);
448                 }
449             }
450
451             ast::ExprBinary(_, ref lhs, ref rhs) => {
452                 if !self.walk_overloaded_operator(expr, &**lhs, vec![&**rhs]) {
453                     self.consume_expr(&**lhs);
454                     self.consume_expr(&**rhs);
455                 }
456             }
457
458             ast::ExprBlock(ref blk) => {
459                 self.walk_block(&**blk);
460             }
461
462             ast::ExprRet(ref opt_expr) => {
463                 for expr in opt_expr.iter() {
464                     self.consume_expr(&**expr);
465                 }
466             }
467
468             ast::ExprAssign(ref lhs, ref rhs) => {
469                 self.mutate_expr(expr, &**lhs, JustWrite);
470                 self.consume_expr(&**rhs);
471             }
472
473             ast::ExprCast(ref base, _) => {
474                 self.consume_expr(&**base);
475             }
476
477             ast::ExprAssignOp(_, ref lhs, ref rhs) => {
478                 // This will have to change if/when we support
479                 // overloaded operators for `+=` and so forth.
480                 self.mutate_expr(expr, &**lhs, WriteAndRead);
481                 self.consume_expr(&**rhs);
482             }
483
484             ast::ExprRepeat(ref base, ref count) => {
485                 self.consume_expr(&**base);
486                 self.consume_expr(&**count);
487             }
488
489             ast::ExprFnBlock(..) |
490             ast::ExprUnboxedFn(..) |
491             ast::ExprProc(..) => {
492                 self.walk_captures(expr)
493             }
494
495             ast::ExprBox(ref place, ref base) => {
496                 self.consume_expr(&**place);
497                 self.consume_expr(&**base);
498             }
499
500             ast::ExprMac(..) => {
501                 self.tcx().sess.span_bug(
502                     expr.span,
503                     "macro expression remains after expansion");
504             }
505         }
506     }
507
508     fn walk_callee(&mut self, call: &ast::Expr, callee: &ast::Expr) {
509         let callee_ty = ty::expr_ty_adjusted(self.tcx(), callee);
510         debug!("walk_callee: callee={} callee_ty={}",
511                callee.repr(self.tcx()), callee_ty.repr(self.tcx()));
512         match ty::get(callee_ty).sty {
513             ty::ty_bare_fn(..) => {
514                 self.consume_expr(callee);
515             }
516             ty::ty_closure(ref f) => {
517                 match f.onceness {
518                     ast::Many => {
519                         self.borrow_expr(callee,
520                                          ty::ReScope(call.id),
521                                          ty::UniqueImmBorrow,
522                                          ClosureInvocation);
523                     }
524                     ast::Once => {
525                         self.consume_expr(callee);
526                     }
527                 }
528             }
529             _ => {
530                 let overloaded_call_type =
531                     match self.tcx()
532                               .method_map
533                               .borrow()
534                               .find(&MethodCall::expr(call.id)) {
535                     Some(ref method_callee) => {
536                         OverloadedCallType::from_method_origin(
537                             self.tcx(),
538                             &method_callee.origin)
539                     }
540                     None => {
541                         self.tcx().sess.span_bug(
542                             callee.span,
543                             format!("unexpected callee type {}",
544                                     callee_ty.repr(self.tcx())).as_slice())
545                     }
546                 };
547                 match overloaded_call_type {
548                     FnMutOverloadedCall => {
549                         self.borrow_expr(callee,
550                                          ty::ReScope(call.id),
551                                          ty::MutBorrow,
552                                          ClosureInvocation);
553                     }
554                     FnOverloadedCall => {
555                         self.borrow_expr(callee,
556                                          ty::ReScope(call.id),
557                                          ty::ImmBorrow,
558                                          ClosureInvocation);
559                     }
560                     FnOnceOverloadedCall => self.consume_expr(callee),
561                 }
562             }
563         }
564     }
565
566     fn walk_stmt(&mut self, stmt: &ast::Stmt) {
567         match stmt.node {
568             ast::StmtDecl(ref decl, _) => {
569                 match decl.node {
570                     ast::DeclLocal(ref local) => {
571                         self.walk_local(&**local);
572                     }
573
574                     ast::DeclItem(_) => {
575                         // we don't visit nested items in this visitor,
576                         // only the fn body we were given.
577                     }
578                 }
579             }
580
581             ast::StmtExpr(ref expr, _) |
582             ast::StmtSemi(ref expr, _) => {
583                 self.consume_expr(&**expr);
584             }
585
586             ast::StmtMac(..) => {
587                 self.tcx().sess.span_bug(stmt.span, "unexpanded stmt macro");
588             }
589         }
590     }
591
592     fn walk_local(&mut self, local: &ast::Local) {
593         match local.init {
594             None => {
595                 let delegate = &mut self.delegate;
596                 pat_util::pat_bindings(&self.typer.tcx().def_map, &*local.pat,
597                                        |_, id, span, _| {
598                     delegate.decl_without_init(id, span);
599                 })
600             }
601
602             Some(ref expr) => {
603                 // Variable declarations with
604                 // initializers are considered
605                 // "assigns", which is handled by
606                 // `walk_pat`:
607                 self.walk_expr(&**expr);
608                 let init_cmt = return_if_err!(self.mc.cat_expr(&**expr));
609                 self.walk_pat(init_cmt, &*local.pat);
610             }
611         }
612     }
613
614     fn walk_block(&mut self, blk: &ast::Block) {
615         /*!
616          * Indicates that the value of `blk` will be consumed,
617          * meaning either copied or moved depending on its type.
618          */
619
620         debug!("walk_block(blk.id={:?})", blk.id);
621
622         for stmt in blk.stmts.iter() {
623             self.walk_stmt(&**stmt);
624         }
625
626         for tail_expr in blk.expr.iter() {
627             self.consume_expr(&**tail_expr);
628         }
629     }
630
631     fn walk_struct_expr(&mut self,
632                         _expr: &ast::Expr,
633                         fields: &Vec<ast::Field>,
634                         opt_with: &Option<P<ast::Expr>>) {
635         // Consume the expressions supplying values for each field.
636         for field in fields.iter() {
637             self.consume_expr(&*field.expr);
638         }
639
640         let with_expr = match *opt_with {
641             Some(ref w) => &**w,
642             None => { return; }
643         };
644
645         let with_cmt = return_if_err!(self.mc.cat_expr(&*with_expr));
646
647         // Select just those fields of the `with`
648         // expression that will actually be used
649         let with_fields = match ty::get(with_cmt.ty).sty {
650             ty::ty_struct(did, ref substs) => {
651                 ty::struct_fields(self.tcx(), did, substs)
652             }
653             _ => {
654                 self.tcx().sess.span_bug(
655                     with_expr.span,
656                     "with expression doesn't evaluate to a struct");
657             }
658         };
659
660         // Consume those fields of the with expression that are needed.
661         for with_field in with_fields.iter() {
662             if !contains_field_named(with_field, fields) {
663                 let cmt_field = self.mc.cat_field(&*with_expr,
664                                                   with_cmt.clone(),
665                                                   with_field.ident,
666                                                   with_field.mt.ty);
667                 self.delegate_consume(with_expr.id, with_expr.span, cmt_field);
668             }
669         }
670
671         fn contains_field_named(field: &ty::field,
672                                 fields: &Vec<ast::Field>)
673                                 -> bool
674         {
675             fields.iter().any(
676                 |f| f.ident.node.name == field.ident.name)
677         }
678     }
679
680     // Invoke the appropriate delegate calls for anything that gets
681     // consumed or borrowed as part of the automatic adjustment
682     // process.
683     fn walk_adjustment(&mut self, expr: &ast::Expr) {
684         let typer = self.typer;
685         match typer.adjustments().borrow().find(&expr.id) {
686             None => { }
687             Some(adjustment) => {
688                 match *adjustment {
689                     ty::AdjustAddEnv(..) => {
690                         // Creating a closure consumes the input and stores it
691                         // into the resulting rvalue.
692                         debug!("walk_adjustment(AutoAddEnv)");
693                         let cmt_unadjusted =
694                             return_if_err!(self.mc.cat_expr_unadjusted(expr));
695                         self.delegate_consume(expr.id, expr.span, cmt_unadjusted);
696                     }
697                     ty::AdjustDerefRef(ty::AutoDerefRef {
698                         autoref: ref opt_autoref,
699                         autoderefs: n
700                     }) => {
701                         self.walk_autoderefs(expr, n);
702
703                         match *opt_autoref {
704                             None => { }
705                             Some(ref r) => {
706                                 self.walk_autoref(expr, r, n);
707                             }
708                         }
709                     }
710                 }
711             }
712         }
713     }
714
715     fn walk_autoderefs(&mut self,
716                        expr: &ast::Expr,
717                        autoderefs: uint) {
718         /*!
719          * Autoderefs for overloaded Deref calls in fact reference
720          * their receiver. That is, if we have `(*x)` where `x` is of
721          * type `Rc<T>`, then this in fact is equivalent to
722          * `x.deref()`. Since `deref()` is declared with `&self`, this
723          * is an autoref of `x`.
724          */
725         debug!("walk_autoderefs expr={} autoderefs={}", expr.repr(self.tcx()), autoderefs);
726
727         for i in range(0, autoderefs) {
728             let deref_id = typeck::MethodCall::autoderef(expr.id, i);
729             match self.typer.node_method_ty(deref_id) {
730                 None => {}
731                 Some(method_ty) => {
732                     let cmt = return_if_err!(self.mc.cat_expr_autoderefd(expr, i));
733                     let self_ty = *ty::ty_fn_args(method_ty).get(0);
734                     let (m, r) = match ty::get(self_ty).sty {
735                         ty::ty_rptr(r, ref m) => (m.mutbl, r),
736                         _ => self.tcx().sess.span_bug(expr.span,
737                                 format!("bad overloaded deref type {}",
738                                     method_ty.repr(self.tcx())).as_slice())
739                     };
740                     let bk = ty::BorrowKind::from_mutbl(m);
741                     self.delegate.borrow(expr.id, expr.span, cmt,
742                                          r, bk, AutoRef);
743                 }
744             }
745         }
746     }
747
748     fn walk_autoref(&mut self,
749                     expr: &ast::Expr,
750                     autoref: &ty::AutoRef,
751                     n: uint) {
752         debug!("walk_autoref expr={}", expr.repr(self.tcx()));
753
754         // Match for unique trait coercions first, since we don't need the
755         // call to cat_expr_autoderefd.
756         match *autoref {
757             ty::AutoUnsizeUniq(ty::UnsizeVtable(..)) |
758             ty::AutoUnsize(ty::UnsizeVtable(..)) => {
759                 assert!(n == 1, format!("Expected exactly 1 deref with Uniq \
760                                          AutoRefs, found: {}", n));
761                 let cmt_unadjusted =
762                     return_if_err!(self.mc.cat_expr_unadjusted(expr));
763                 self.delegate_consume(expr.id, expr.span, cmt_unadjusted);
764                 return;
765             }
766             _ => {}
767         }
768
769         let cmt_derefd = return_if_err!(
770             self.mc.cat_expr_autoderefd(expr, n));
771         debug!("walk_adjustment: cmt_derefd={}",
772                cmt_derefd.repr(self.tcx()));
773
774         match *autoref {
775             ty::AutoPtr(r, m, _) => {
776                 self.delegate.borrow(expr.id,
777                                      expr.span,
778                                      cmt_derefd,
779                                      r,
780                                      ty::BorrowKind::from_mutbl(m),
781                                      AutoRef);
782             }
783             ty::AutoUnsizeUniq(_) | ty::AutoUnsize(_) | ty::AutoUnsafe(..) => {}
784         }
785     }
786
787     fn walk_overloaded_operator(&mut self,
788                                 expr: &ast::Expr,
789                                 receiver: &ast::Expr,
790                                 rhs: Vec<&ast::Expr>)
791                                 -> bool
792     {
793         if !self.typer.is_method_call(expr.id) {
794             return false;
795         }
796
797         self.walk_expr(receiver);
798
799         // Arguments (but not receivers) to overloaded operator
800         // methods are implicitly autoref'd which sadly does not use
801         // adjustments, so we must hardcode the borrow here.
802
803         let r = ty::ReScope(expr.id);
804         let bk = ty::ImmBorrow;
805
806         for &arg in rhs.iter() {
807             self.borrow_expr(arg, r, bk, OverloadedOperator);
808         }
809         return true;
810     }
811
812     fn walk_arm(&mut self, discr_cmt: mc::cmt, arm: &ast::Arm) {
813         for pat in arm.pats.iter() {
814             self.walk_pat(discr_cmt.clone(), &**pat);
815         }
816
817         for guard in arm.guard.iter() {
818             self.consume_expr(&**guard);
819         }
820
821         self.consume_expr(&*arm.body);
822     }
823
824     fn walk_pat(&mut self, cmt_discr: mc::cmt, pat: &ast::Pat) {
825         debug!("walk_pat cmt_discr={} pat={}", cmt_discr.repr(self.tcx()),
826                pat.repr(self.tcx()));
827         let mc = &self.mc;
828         let typer = self.typer;
829         let tcx = typer.tcx();
830         let def_map = &self.typer.tcx().def_map;
831         let delegate = &mut self.delegate;
832         return_if_err!(mc.cat_pattern(cmt_discr, &*pat, |mc, cmt_pat, pat| {
833             if pat_util::pat_is_binding(def_map, pat) {
834                 let tcx = typer.tcx();
835
836                 debug!("binding cmt_pat={} pat={}",
837                        cmt_pat.repr(tcx),
838                        pat.repr(tcx));
839
840                 // pat_ty: the type of the binding being produced.
841                 let pat_ty = return_if_err!(typer.node_ty(pat.id));
842
843                 // Each match binding is effectively an assignment to the
844                 // binding being produced.
845                 let def = def_map.borrow().get_copy(&pat.id);
846                 match mc.cat_def(pat.id, pat.span, pat_ty, def) {
847                     Ok(binding_cmt) => {
848                         delegate.mutate(pat.id, pat.span, binding_cmt, Init);
849                     }
850                     Err(_) => { }
851                 }
852
853                 // It is also a borrow or copy/move of the value being matched.
854                 match pat.node {
855                     ast::PatIdent(ast::BindByRef(m), _, _) => {
856                         let (r, bk) = {
857                             (ty::ty_region(tcx, pat.span, pat_ty),
858                              ty::BorrowKind::from_mutbl(m))
859                         };
860                         delegate.borrow(pat.id, pat.span, cmt_pat,
861                                              r, bk, RefBinding);
862                     }
863                     ast::PatIdent(ast::BindByValue(_), _, _) => {
864                         let mode = copy_or_move(typer.tcx(), cmt_pat.ty, PatBindingMove);
865                         debug!("walk_pat binding consuming pat");
866                         delegate.consume_pat(pat, cmt_pat, mode);
867                     }
868                     _ => {
869                         typer.tcx().sess.span_bug(
870                             pat.span,
871                             "binding pattern not an identifier");
872                     }
873                 }
874             } else {
875                 match pat.node {
876                     ast::PatVec(_, Some(ref slice_pat), _) => {
877                         // The `slice_pat` here creates a slice into
878                         // the original vector.  This is effectively a
879                         // borrow of the elements of the vector being
880                         // matched.
881
882                         let (slice_cmt, slice_mutbl, slice_r) = {
883                             match mc.cat_slice_pattern(cmt_pat, &**slice_pat) {
884                                 Ok(v) => v,
885                                 Err(()) => {
886                                     tcx.sess.span_bug(slice_pat.span,
887                                                       "Err from mc")
888                                 }
889                             }
890                         };
891
892                         // Note: We declare here that the borrow
893                         // occurs upon entering the `[...]`
894                         // pattern. This implies that something like
895                         // `[a, ..b]` where `a` is a move is illegal,
896                         // because the borrow is already in effect.
897                         // In fact such a move would be safe-ish, but
898                         // it effectively *requires* that we use the
899                         // nulling out semantics to indicate when a
900                         // value has been moved, which we are trying
901                         // to move away from.  Otherwise, how can we
902                         // indicate that the first element in the
903                         // vector has been moved?  Eventually, we
904                         // could perhaps modify this rule to permit
905                         // `[..a, b]` where `b` is a move, because in
906                         // that case we can adjust the length of the
907                         // original vec accordingly, but we'd have to
908                         // make trans do the right thing, and it would
909                         // only work for `~` vectors. It seems simpler
910                         // to just require that people call
911                         // `vec.pop()` or `vec.unshift()`.
912                         let slice_bk = ty::BorrowKind::from_mutbl(slice_mutbl);
913                         delegate.borrow(pat.id, pat.span,
914                                         slice_cmt, slice_r,
915                                         slice_bk, RefBinding);
916                     }
917                     _ => { }
918                 }
919             }
920         }));
921     }
922
923     fn walk_captures(&mut self, closure_expr: &ast::Expr) {
924         debug!("walk_captures({})", closure_expr.repr(self.tcx()));
925
926         let tcx = self.typer.tcx();
927         ty::with_freevars(tcx, closure_expr.id, |freevars| {
928             match self.tcx().capture_mode(closure_expr.id) {
929                 ast::CaptureByRef => {
930                     self.walk_by_ref_captures(closure_expr, freevars);
931                 }
932                 ast::CaptureByValue => {
933                     self.walk_by_value_captures(closure_expr, freevars);
934                 }
935             }
936         });
937     }
938
939     fn walk_by_ref_captures(&mut self,
940                             closure_expr: &ast::Expr,
941                             freevars: &[ty::Freevar]) {
942         for freevar in freevars.iter() {
943             let id_var = freevar.def.def_id().node;
944             let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.id,
945                                                                closure_expr.span,
946                                                                freevar.def));
947
948             // Lookup the kind of borrow the callee requires, as
949             // inferred by regionbk
950             let upvar_id = ty::UpvarId { var_id: id_var,
951                                          closure_expr_id: closure_expr.id };
952             let upvar_borrow = self.tcx().upvar_borrow_map.borrow()
953                                    .get_copy(&upvar_id);
954
955             self.delegate.borrow(closure_expr.id,
956                                  closure_expr.span,
957                                  cmt_var,
958                                  upvar_borrow.region,
959                                  upvar_borrow.kind,
960                                  ClosureCapture(freevar.span));
961         }
962     }
963
964     fn walk_by_value_captures(&mut self,
965                               closure_expr: &ast::Expr,
966                               freevars: &[ty::Freevar]) {
967         for freevar in freevars.iter() {
968             let cmt_var = return_if_err!(self.cat_captured_var(closure_expr.id,
969                                                                closure_expr.span,
970                                                                freevar.def));
971             let mode = copy_or_move(self.tcx(), cmt_var.ty, CaptureMove);
972             self.delegate.consume(closure_expr.id, freevar.span, cmt_var, mode);
973         }
974     }
975
976     fn cat_captured_var(&mut self,
977                         closure_id: ast::NodeId,
978                         closure_span: Span,
979                         upvar_def: def::Def)
980                         -> mc::McResult<mc::cmt> {
981         // Create the cmt for the variable being borrowed, from the
982         // caller's perspective
983         let var_id = upvar_def.def_id().node;
984         let var_ty = try!(self.typer.node_ty(var_id));
985         self.mc.cat_def(closure_id, closure_span, var_ty, upvar_def)
986     }
987 }
988
989 fn copy_or_move(tcx: &ty::ctxt, ty: ty::t, move_reason: MoveReason) -> ConsumeMode {
990     if ty::type_moves_by_default(tcx, ty) { Move(move_reason) } else { Copy }
991 }
992