1 // Copyright 2012-2013 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.
14 use borrowck::gather_loans::move_error::MoveSpanAndPath;
15 use borrowck::gather_loans::move_error::{MoveError, MoveErrorCollector};
16 use borrowck::move_data::*;
17 use rustc::middle::expr_use_visitor as euv;
18 use rustc::middle::mem_categorization as mc;
19 use rustc::middle::mem_categorization::InteriorOffsetKind as Kind;
20 use rustc::middle::ty;
24 use syntax::codemap::Span;
27 struct GatherMoveInfo<'tcx> {
31 span_path_opt: Option<MoveSpanAndPath>
34 pub fn gather_decl<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
35 move_data: &MoveData<'tcx>,
38 var_id: ast::NodeId) {
39 let ty = bccx.tcx.node_id_to_type(var_id);
40 let loan_path = Rc::new(LoanPath::new(LpVar(var_id), ty));
41 move_data.add_move(bccx.tcx, loan_path, decl_id, Declared);
44 pub fn gather_move_from_expr<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
45 move_data: &MoveData<'tcx>,
46 move_error_collector: &MoveErrorCollector<'tcx>,
47 move_expr_id: ast::NodeId,
49 move_reason: euv::MoveReason) {
50 let kind = match move_reason {
51 euv::DirectRefMove | euv::PatBindingMove => MoveExpr,
52 euv::CaptureMove => Captured
54 let move_info = GatherMoveInfo {
60 gather_move(bccx, move_data, move_error_collector, move_info);
63 pub fn gather_match_variant<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
64 move_data: &MoveData<'tcx>,
65 _move_error_collector: &MoveErrorCollector<'tcx>,
68 mode: euv::MatchMode) {
70 debug!("gather_match_variant(move_pat={}, cmt={:?}, mode={:?})",
71 move_pat.id, cmt, mode);
73 let opt_lp = opt_loan_path(&cmt);
77 LpDowncast(ref base_lp, _) =>
78 move_data.add_variant_match(
79 tcx, lp.clone(), move_pat.id, base_lp.clone(), mode),
80 _ => panic!("should only call gather_match_variant \
81 for cat_downcast cmt"),
85 // We get None when input to match is non-path (e.g.
86 // temporary result like a function call). Since no
87 // loan-path is being matched, no need to record a
94 pub fn gather_move_from_pat<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
95 move_data: &MoveData<'tcx>,
96 move_error_collector: &MoveErrorCollector<'tcx>,
99 let pat_span_path_opt = match move_pat.node {
100 hir::PatIdent(_, ref path1, _) => {
101 Some(MoveSpanAndPath{span: move_pat.span,
102 name: path1.node.name})
106 let move_info = GatherMoveInfo {
110 span_path_opt: pat_span_path_opt,
112 gather_move(bccx, move_data, move_error_collector, move_info);
115 fn gather_move<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
116 move_data: &MoveData<'tcx>,
117 move_error_collector: &MoveErrorCollector<'tcx>,
118 move_info: GatherMoveInfo<'tcx>) {
119 debug!("gather_move(move_id={}, cmt={:?})",
120 move_info.id, move_info.cmt);
122 let potentially_illegal_move =
123 check_and_get_illegal_move_origin(bccx, &move_info.cmt);
124 match potentially_illegal_move {
125 Some(illegal_move_origin) => {
126 debug!("illegal_move_origin={:?}", illegal_move_origin);
127 let error = MoveError::with_move_info(illegal_move_origin,
128 move_info.span_path_opt);
129 move_error_collector.add_error(error);
135 match opt_loan_path(&move_info.cmt) {
137 move_data.add_move(bccx.tcx, loan_path,
138 move_info.id, move_info.kind);
141 // move from rvalue or raw pointer, hence ok
146 pub fn gather_assignment<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
147 move_data: &MoveData<'tcx>,
148 assignment_id: ast::NodeId,
149 assignment_span: Span,
150 assignee_loan_path: Rc<LoanPath<'tcx>>,
151 assignee_id: ast::NodeId,
152 mode: euv::MutateMode) {
153 move_data.add_assignment(bccx.tcx,
161 // (keep in sync with move_error::report_cannot_move_out_of )
162 fn check_and_get_illegal_move_origin<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
164 -> Option<mc::cmt<'tcx>> {
166 mc::cat_deref(_, _, mc::BorrowedPtr(..)) |
167 mc::cat_deref(_, _, mc::Implicit(..)) |
168 mc::cat_deref(_, _, mc::UnsafePtr(..)) |
169 mc::cat_static_item => {
175 mc::cat_upvar(..) => {
179 mc::cat_downcast(ref b, _) |
180 mc::cat_interior(ref b, mc::InteriorField(_)) |
181 mc::cat_interior(ref b, mc::InteriorElement(Kind::Pattern, _)) => {
183 ty::TyStruct(def, _) | ty::TyEnum(def, _) => {
187 check_and_get_illegal_move_origin(bccx, b)
191 check_and_get_illegal_move_origin(bccx, b)
196 mc::cat_interior(_, mc::InteriorElement(Kind::Index, _)) => {
197 // Forbid move of arr[i] for arr: [T; 3]; see RFC 533.
201 mc::cat_deref(ref b, _, mc::Unique) => {
202 check_and_get_illegal_move_origin(bccx, b)