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.
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 * 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.
17 use middle::mem_categorization as mc;
19 use middle::mem_categorization::Typer;
22 use middle::typeck::{MethodCall, MethodObject, MethodTraitObject};
23 use middle::typeck::{MethodOrigin, MethodParam, MethodTypeParam};
24 use middle::typeck::{MethodStatic, MethodStaticUnboxedClosure};
26 use util::ppaux::Repr;
30 use syntax::codemap::Span;
32 ///////////////////////////////////////////////////////////////////////////
35 /// This trait defines the callbacks you can expect to receive when
36 /// employing the ExprUseVisitor.
38 // The value found at `cmt` is either copied or moved, depending
41 consume_id: ast::NodeId,
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,
53 // The value found at `borrow` is being borrowed at the point
54 // `borrow_id` for the region `loan_region` with kind `bk`.
56 borrow_id: ast::NodeId,
59 loan_region: ty::Region,
61 loan_cause: LoanCause);
63 // The local variable `id` is declared but not initialized.
64 fn decl_without_init(&mut self,
68 // The path at `cmt` is being assigned to.
70 assignment_id: ast::NodeId,
71 assignment_span: Span,
72 assignee_cmt: mc::cmt,
76 #[deriving(PartialEq)]
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
94 #[deriving(PartialEq,Show)]
101 #[deriving(PartialEq,Show)]
102 pub enum MutateMode {
105 WriteAndRead, // x += y
108 enum OverloadedCallType {
111 FnOnceOverloadedCall,
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)
122 match maybe_function_trait {
123 Some(function_trait) if function_trait == trait_id => {
124 return overloaded_call_type
130 tcx.sess.bug("overloaded call didn't map to known function trait")
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()
139 ty::TypeTraitItem(_) => {
140 tcx.sess.bug("overloaded call method wasn't in method map")
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?!")
148 ty::ImplContainer(impl_id) => impl_id,
150 let trait_ref = match ty::impl_trait_ref(tcx, impl_id) {
152 tcx.sess.bug("statically resolved overloaded call impl \
153 didn't implement a trait?!")
155 Some(ref trait_ref) => (*trait_ref).clone(),
157 OverloadedCallType::from_trait_id(tcx, trait_ref.def_id)
160 fn from_unboxed_closure(tcx: &ty::ctxt, closure_did: ast::DefId)
161 -> OverloadedCallType {
166 .expect("OverloadedCallType::from_unboxed_closure: didn't \
170 OverloadedCallType::from_trait_id(tcx, trait_did)
173 fn from_method_origin(tcx: &ty::ctxt, origin: &MethodOrigin)
174 -> OverloadedCallType {
176 MethodStatic(def_id) => {
177 OverloadedCallType::from_method_id(tcx, def_id)
179 MethodStaticUnboxedClosure(def_id) => {
180 OverloadedCallType::from_unboxed_closure(tcx, def_id)
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)
190 ///////////////////////////////////////////////////////////////////////////
191 // The ExprUseVisitor type
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.
198 pub struct ExprUseVisitor<'d,'t,TYPER:'t> {
200 mc: mc::MemCategorizationContext<'t,TYPER>,
201 delegate: &'d mut Delegate+'d,
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
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(
220 impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,TYPER> {
221 pub fn new(delegate: &'d mut Delegate,
223 -> ExprUseVisitor<'d,'t,TYPER> {
224 ExprUseVisitor { typer: typer,
225 mc: mc::MemCategorizationContext::new(typer),
229 pub fn walk_fn(&mut self,
232 self.walk_arg_patterns(decl, body);
233 self.walk_block(body);
236 fn walk_arg_patterns(&mut self,
239 for arg in decl.inputs.iter() {
240 let arg_ty = return_if_err!(self.typer.node_ty(arg.pat.id));
242 let arg_cmt = self.mc.cat_rvalue(
245 ty::ReScope(body.id), // Args live only as long as the fn body.
248 self.walk_pat(arg_cmt, &*arg.pat);
252 fn tcx(&self) -> &'t ty::ctxt<'tcx> {
256 fn delegate_consume(&mut self,
257 consume_id: ast::NodeId,
260 let mode = copy_or_move(self.tcx(), cmt.ty, DirectRefMove);
261 self.delegate.consume(consume_id, consume_span, cmt, mode);
264 fn consume_exprs(&mut self, exprs: &Vec<P<ast::Expr>>) {
265 for expr in exprs.iter() {
266 self.consume_expr(&**expr);
270 fn consume_expr(&mut self, expr: &ast::Expr) {
271 debug!("consume_expr(expr={})", expr.repr(self.tcx()));
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);
278 fn mutate_expr(&mut self,
279 assignment_expr: &ast::Expr,
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);
287 fn borrow_expr(&mut self,
292 debug!("borrow_expr(expr={}, r={}, bk={})",
293 expr.repr(self.tcx()), r.repr(self.tcx()), bk.repr(self.tcx()));
295 let cmt = return_if_err!(self.mc.cat_expr(expr));
296 self.delegate.borrow(expr.id, expr.span, cmt, r, bk, cause);
298 // Note: Unlike consume, we can ignore ExprParen. cat_expr
299 // already skips over them, and walk will uncover any
300 // attachments or whatever.
304 fn select_from_expr(&mut self, expr: &ast::Expr) {
308 pub fn walk_expr(&mut self, expr: &ast::Expr) {
309 debug!("walk_expr(expr={})", expr.repr(self.tcx()));
311 self.walk_adjustment(expr);
314 ast::ExprParen(ref subexpr) => {
315 self.walk_expr(&**subexpr)
318 ast::ExprPath(..) => { }
320 ast::ExprUnary(ast::UnDeref, ref base) => { // *base
321 if !self.walk_overloaded_operator(expr, &**base, Vec::new()) {
322 self.select_from_expr(&**base);
326 ast::ExprField(ref base, _, _) => { // base.f
327 self.select_from_expr(&**base);
330 ast::ExprTupField(ref base, _, _) => { // base.<n>
331 self.select_from_expr(&**base);
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);
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()
348 let overloaded = self.walk_overloaded_operator(expr, &**base, args);
352 ast::ExprCall(ref callee, ref args) => { // callee(args)
353 self.walk_callee(expr, &**callee);
354 self.consume_exprs(args);
357 ast::ExprMethodCall(_, _, ref args) => { // callee.m(args)
358 self.consume_exprs(args);
361 ast::ExprStruct(_, ref fields, ref opt_with) => {
362 self.walk_struct_expr(expr, fields, opt_with);
365 ast::ExprTup(ref exprs) => {
366 self.consume_exprs(exprs);
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);
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);
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);
387 ast::ExprVec(ref exprs) => {
388 self.consume_exprs(exprs);
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);
400 self.walk_expr(&**base);
404 ast::ExprInlineAsm(ref ia) => {
405 for &(_, ref input) in ia.inputs.iter() {
406 self.consume_expr(&**input);
409 for &(_, ref output, is_rw) in ia.outputs.iter() {
410 self.mutate_expr(expr, &**output,
411 if is_rw { WriteAndRead } else { JustWrite });
417 ast::ExprLit(..) => {}
419 ast::ExprLoop(ref blk, _) => {
420 self.walk_block(&**blk);
423 ast::ExprWhile(ref cond_expr, ref blk, _) => {
424 self.consume_expr(&**cond_expr);
425 self.walk_block(&**blk);
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);
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,
440 self.walk_pat(pat_cmt, &**pat);
442 self.walk_block(&**blk);
445 ast::ExprUnary(_, ref lhs) => {
446 if !self.walk_overloaded_operator(expr, &**lhs, Vec::new()) {
447 self.consume_expr(&**lhs);
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);
458 ast::ExprBlock(ref blk) => {
459 self.walk_block(&**blk);
462 ast::ExprRet(ref opt_expr) => {
463 for expr in opt_expr.iter() {
464 self.consume_expr(&**expr);
468 ast::ExprAssign(ref lhs, ref rhs) => {
469 self.mutate_expr(expr, &**lhs, JustWrite);
470 self.consume_expr(&**rhs);
473 ast::ExprCast(ref base, _) => {
474 self.consume_expr(&**base);
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);
484 ast::ExprRepeat(ref base, ref count) => {
485 self.consume_expr(&**base);
486 self.consume_expr(&**count);
489 ast::ExprFnBlock(..) |
490 ast::ExprUnboxedFn(..) |
491 ast::ExprProc(..) => {
492 self.walk_captures(expr)
495 ast::ExprBox(ref place, ref base) => {
496 self.consume_expr(&**place);
497 self.consume_expr(&**base);
500 ast::ExprMac(..) => {
501 self.tcx().sess.span_bug(
503 "macro expression remains after expansion");
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);
516 ty::ty_closure(ref f) => {
519 self.borrow_expr(callee,
520 ty::ReScope(call.id),
525 self.consume_expr(callee);
530 let overloaded_call_type =
534 .find(&MethodCall::expr(call.id)) {
535 Some(ref method_callee) => {
536 OverloadedCallType::from_method_origin(
538 &method_callee.origin)
541 self.tcx().sess.span_bug(
543 format!("unexpected callee type {}",
544 callee_ty.repr(self.tcx())).as_slice())
547 match overloaded_call_type {
548 FnMutOverloadedCall => {
549 self.borrow_expr(callee,
550 ty::ReScope(call.id),
554 FnOverloadedCall => {
555 self.borrow_expr(callee,
556 ty::ReScope(call.id),
560 FnOnceOverloadedCall => self.consume_expr(callee),
566 fn walk_stmt(&mut self, stmt: &ast::Stmt) {
568 ast::StmtDecl(ref decl, _) => {
570 ast::DeclLocal(ref local) => {
571 self.walk_local(&**local);
574 ast::DeclItem(_) => {
575 // we don't visit nested items in this visitor,
576 // only the fn body we were given.
581 ast::StmtExpr(ref expr, _) |
582 ast::StmtSemi(ref expr, _) => {
583 self.consume_expr(&**expr);
586 ast::StmtMac(..) => {
587 self.tcx().sess.span_bug(stmt.span, "unexpanded stmt macro");
592 fn walk_local(&mut self, local: &ast::Local) {
595 let delegate = &mut self.delegate;
596 pat_util::pat_bindings(&self.typer.tcx().def_map, &*local.pat,
598 delegate.decl_without_init(id, span);
603 // Variable declarations with
604 // initializers are considered
605 // "assigns", which is handled by
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);
614 fn walk_block(&mut self, blk: &ast::Block) {
616 * Indicates that the value of `blk` will be consumed,
617 * meaning either copied or moved depending on its type.
620 debug!("walk_block(blk.id={:?})", blk.id);
622 for stmt in blk.stmts.iter() {
623 self.walk_stmt(&**stmt);
626 for tail_expr in blk.expr.iter() {
627 self.consume_expr(&**tail_expr);
631 fn walk_struct_expr(&mut self,
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);
640 let with_expr = match *opt_with {
645 let with_cmt = return_if_err!(self.mc.cat_expr(&*with_expr));
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)
654 self.tcx().sess.span_bug(
656 "with expression doesn't evaluate to a struct");
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,
667 self.delegate_consume(with_expr.id, with_expr.span, cmt_field);
671 fn contains_field_named(field: &ty::field,
672 fields: &Vec<ast::Field>)
676 |f| f.ident.node.name == field.ident.name)
680 // Invoke the appropriate delegate calls for anything that gets
681 // consumed or borrowed as part of the automatic adjustment
683 fn walk_adjustment(&mut self, expr: &ast::Expr) {
684 let typer = self.typer;
685 match typer.adjustments().borrow().find(&expr.id) {
687 Some(adjustment) => {
689 ty::AdjustAddEnv(..) => {
690 // Creating a closure consumes the input and stores it
691 // into the resulting rvalue.
692 debug!("walk_adjustment(AutoAddEnv)");
694 return_if_err!(self.mc.cat_expr_unadjusted(expr));
695 self.delegate_consume(expr.id, expr.span, cmt_unadjusted);
697 ty::AdjustDerefRef(ty::AutoDerefRef {
698 autoref: ref opt_autoref,
701 self.walk_autoderefs(expr, n);
706 self.walk_autoref(expr, r, n);
715 fn walk_autoderefs(&mut self,
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`.
725 debug!("walk_autoderefs expr={} autoderefs={}", expr.repr(self.tcx()), autoderefs);
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) {
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())
740 let bk = ty::BorrowKind::from_mutbl(m);
741 self.delegate.borrow(expr.id, expr.span, cmt,
748 fn walk_autoref(&mut self,
750 autoref: &ty::AutoRef,
752 debug!("walk_autoref expr={}", expr.repr(self.tcx()));
754 // Match for unique trait coercions first, since we don't need the
755 // call to cat_expr_autoderefd.
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));
762 return_if_err!(self.mc.cat_expr_unadjusted(expr));
763 self.delegate_consume(expr.id, expr.span, cmt_unadjusted);
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()));
775 ty::AutoPtr(r, m, _) => {
776 self.delegate.borrow(expr.id,
780 ty::BorrowKind::from_mutbl(m),
783 ty::AutoUnsizeUniq(_) | ty::AutoUnsize(_) | ty::AutoUnsafe(..) => {}
787 fn walk_overloaded_operator(&mut self,
789 receiver: &ast::Expr,
790 rhs: Vec<&ast::Expr>)
793 if !self.typer.is_method_call(expr.id) {
797 self.walk_expr(receiver);
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.
803 let r = ty::ReScope(expr.id);
804 let bk = ty::ImmBorrow;
806 for &arg in rhs.iter() {
807 self.borrow_expr(arg, r, bk, OverloadedOperator);
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);
817 for guard in arm.guard.iter() {
818 self.consume_expr(&**guard);
821 self.consume_expr(&*arm.body);
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()));
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();
836 debug!("binding cmt_pat={} pat={}",
840 // pat_ty: the type of the binding being produced.
841 let pat_ty = return_if_err!(typer.node_ty(pat.id));
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) {
848 delegate.mutate(pat.id, pat.span, binding_cmt, Init);
853 // It is also a borrow or copy/move of the value being matched.
855 ast::PatIdent(ast::BindByRef(m), _, _) => {
857 (ty::ty_region(tcx, pat.span, pat_ty),
858 ty::BorrowKind::from_mutbl(m))
860 delegate.borrow(pat.id, pat.span, cmt_pat,
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);
869 typer.tcx().sess.span_bug(
871 "binding pattern not an identifier");
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
882 let (slice_cmt, slice_mutbl, slice_r) = {
883 match mc.cat_slice_pattern(cmt_pat, &**slice_pat) {
886 tcx.sess.span_bug(slice_pat.span,
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,
915 slice_bk, RefBinding);
923 fn walk_captures(&mut self, closure_expr: &ast::Expr) {
924 debug!("walk_captures({})", closure_expr.repr(self.tcx()));
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);
932 ast::CaptureByValue => {
933 self.walk_by_value_captures(closure_expr, freevars);
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,
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);
955 self.delegate.borrow(closure_expr.id,
960 ClosureCapture(freevar.span));
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,
971 let mode = copy_or_move(self.tcx(), cmt_var.ty, CaptureMove);
972 self.delegate.consume(closure_expr.id, freevar.span, cmt_var, mode);
976 fn cat_captured_var(&mut self,
977 closure_id: ast::NodeId,
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)
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 }