]> git.proxmox.com Git - rustc.git/blob - src/librustc_borrowck/borrowck/gather_loans/gather_moves.rs
Imported Upstream version 1.3.0+dfsg1
[rustc.git] / src / librustc_borrowck / borrowck / gather_loans / gather_moves.rs
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.
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 //! Computes moves.
12
13 use borrowck::*;
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;
21
22 use std::rc::Rc;
23 use syntax::ast;
24 use syntax::codemap::Span;
25
26 struct GatherMoveInfo<'tcx> {
27 id: ast::NodeId,
28 kind: MoveKind,
29 cmt: mc::cmt<'tcx>,
30 span_path_opt: Option<MoveSpanAndPath>
31 }
32
33 pub fn gather_decl<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
34 move_data: &MoveData<'tcx>,
35 decl_id: ast::NodeId,
36 _decl_span: Span,
37 var_id: ast::NodeId) {
38 let ty = bccx.tcx.node_id_to_type(var_id);
39 let loan_path = Rc::new(LoanPath::new(LpVar(var_id), ty));
40 move_data.add_move(bccx.tcx, loan_path, decl_id, Declared);
41 }
42
43 pub fn gather_move_from_expr<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
44 move_data: &MoveData<'tcx>,
45 move_error_collector: &MoveErrorCollector<'tcx>,
46 move_expr_id: ast::NodeId,
47 cmt: mc::cmt<'tcx>,
48 move_reason: euv::MoveReason) {
49 let kind = match move_reason {
50 euv::DirectRefMove | euv::PatBindingMove => MoveExpr,
51 euv::CaptureMove => Captured
52 };
53 let move_info = GatherMoveInfo {
54 id: move_expr_id,
55 kind: kind,
56 cmt: cmt,
57 span_path_opt: None,
58 };
59 gather_move(bccx, move_data, move_error_collector, move_info);
60 }
61
62 pub fn gather_match_variant<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
63 move_data: &MoveData<'tcx>,
64 _move_error_collector: &MoveErrorCollector<'tcx>,
65 move_pat: &ast::Pat,
66 cmt: mc::cmt<'tcx>,
67 mode: euv::MatchMode) {
68 let tcx = bccx.tcx;
69 debug!("gather_match_variant(move_pat={}, cmt={:?}, mode={:?})",
70 move_pat.id, cmt, mode);
71
72 let opt_lp = opt_loan_path(&cmt);
73 match opt_lp {
74 Some(lp) => {
75 match lp.kind {
76 LpDowncast(ref base_lp, _) =>
77 move_data.add_variant_match(
78 tcx, lp.clone(), move_pat.id, base_lp.clone(), mode),
79 _ => panic!("should only call gather_match_variant \
80 for cat_downcast cmt"),
81 }
82 }
83 None => {
84 // We get None when input to match is non-path (e.g.
85 // temporary result like a function call). Since no
86 // loan-path is being matched, no need to record a
87 // downcast.
88 return;
89 }
90 }
91 }
92
93 pub fn gather_move_from_pat<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
94 move_data: &MoveData<'tcx>,
95 move_error_collector: &MoveErrorCollector<'tcx>,
96 move_pat: &ast::Pat,
97 cmt: mc::cmt<'tcx>) {
98 let pat_span_path_opt = match move_pat.node {
99 ast::PatIdent(_, ref path1, _) => {
100 Some(MoveSpanAndPath{span: move_pat.span,
101 ident: path1.node})
102 },
103 _ => None,
104 };
105 let move_info = GatherMoveInfo {
106 id: move_pat.id,
107 kind: MovePat,
108 cmt: cmt,
109 span_path_opt: pat_span_path_opt,
110 };
111 gather_move(bccx, move_data, move_error_collector, move_info);
112 }
113
114 fn gather_move<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
115 move_data: &MoveData<'tcx>,
116 move_error_collector: &MoveErrorCollector<'tcx>,
117 move_info: GatherMoveInfo<'tcx>) {
118 debug!("gather_move(move_id={}, cmt={:?})",
119 move_info.id, move_info.cmt);
120
121 let potentially_illegal_move =
122 check_and_get_illegal_move_origin(bccx, &move_info.cmt);
123 match potentially_illegal_move {
124 Some(illegal_move_origin) => {
125 debug!("illegal_move_origin={:?}", illegal_move_origin);
126 let error = MoveError::with_move_info(illegal_move_origin,
127 move_info.span_path_opt);
128 move_error_collector.add_error(error);
129 return
130 }
131 None => ()
132 }
133
134 match opt_loan_path(&move_info.cmt) {
135 Some(loan_path) => {
136 move_data.add_move(bccx.tcx, loan_path,
137 move_info.id, move_info.kind);
138 }
139 None => {
140 // move from rvalue or raw pointer, hence ok
141 }
142 }
143 }
144
145 pub fn gather_assignment<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
146 move_data: &MoveData<'tcx>,
147 assignment_id: ast::NodeId,
148 assignment_span: Span,
149 assignee_loan_path: Rc<LoanPath<'tcx>>,
150 assignee_id: ast::NodeId,
151 mode: euv::MutateMode) {
152 move_data.add_assignment(bccx.tcx,
153 assignee_loan_path,
154 assignment_id,
155 assignment_span,
156 assignee_id,
157 mode);
158 }
159
160 // (keep in sync with move_error::report_cannot_move_out_of )
161 fn check_and_get_illegal_move_origin<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
162 cmt: &mc::cmt<'tcx>)
163 -> Option<mc::cmt<'tcx>> {
164 match cmt.cat {
165 mc::cat_deref(_, _, mc::BorrowedPtr(..)) |
166 mc::cat_deref(_, _, mc::Implicit(..)) |
167 mc::cat_deref(_, _, mc::UnsafePtr(..)) |
168 mc::cat_static_item => {
169 Some(cmt.clone())
170 }
171
172 mc::cat_rvalue(..) |
173 mc::cat_local(..) |
174 mc::cat_upvar(..) => {
175 None
176 }
177
178 mc::cat_downcast(ref b, _) |
179 mc::cat_interior(ref b, mc::InteriorField(_)) |
180 mc::cat_interior(ref b, mc::InteriorElement(Kind::Pattern, _)) => {
181 match b.ty.sty {
182 ty::TyStruct(did, _) | ty::TyEnum(did, _) => {
183 if bccx.tcx.has_dtor(did) {
184 Some(cmt.clone())
185 } else {
186 check_and_get_illegal_move_origin(bccx, b)
187 }
188 }
189 _ => {
190 check_and_get_illegal_move_origin(bccx, b)
191 }
192 }
193 }
194
195 mc::cat_interior(_, mc::InteriorElement(Kind::Index, _)) => {
196 // Forbid move of arr[i] for arr: [T; 3]; see RFC 533.
197 Some(cmt.clone())
198 }
199
200 mc::cat_deref(ref b, _, mc::Unique) => {
201 check_and_get_illegal_move_origin(bccx, b)
202 }
203 }
204 }