@@ -17,35 +17,32 @@ tag copied { not_allowed; copied; not_copied; }
17
17
18
18
type binding = @{ node_id: node_id,
19
19
span: span,
20
- local_id: uint,
21
20
root_var: option:: t<node_id>,
21
+ local_id: uint,
22
22
unsafe_tys: [ ty:: t] ,
23
23
mutable ok: valid,
24
24
mutable copied: copied} ;
25
25
type scope = { bs : [ binding ] , ret_style : ast:: ret_style } ;
26
26
27
27
fn mk_binding ( cx : ctx , id : node_id , span : span , root_var : option:: t < node_id > ,
28
28
unsafe : [ ty:: t ] ) -> binding {
29
- ret @{ node_id : id, span : span, local_id : cx. next_local ,
30
- root_var : root_var, unsafe_tys : unsafe ,
31
- mutable ok: valid, mutable copied: not_copied} ;
29
+ ret @{ node_id : id, span : span, root_var : root_var,
30
+ local_id : local_id_of_node ( cx, id) ,
31
+ unsafe_tys : unsafe , mutable ok: valid,
32
+ mutable copied: not_copied} ;
32
33
}
33
34
34
35
tag local_info { local( uint) ; }
35
36
36
37
type copy_map = std:: map:: hashmap < node_id , ( ) > ;
37
38
38
39
type ctx = { tcx : ty:: ctxt ,
39
- local_map : std:: map:: hashmap < node_id , local_info > ,
40
- mutable next_local: uint ,
41
40
copy_map : copy_map } ;
42
41
43
42
fn check_crate ( tcx : ty:: ctxt , crate : @ast:: crate ) -> copy_map {
44
43
// Stores information about object fields and function
45
44
// arguments that's otherwise not easily available.
46
45
let cx = @{ tcx: tcx,
47
- local_map: std:: map:: new_int_hash ( ) ,
48
- mutable next_local: 0 u,
49
46
copy_map: std:: map:: new_int_hash ( ) } ;
50
47
let v = @{ visit_fn: bind visit_fn ( cx, _, _, _, _, _, _, _) ,
51
48
visit_expr: bind visit_expr ( cx, _, _, _) ,
@@ -133,13 +130,6 @@ fn visit_expr(cx: @ctx, ex: @ast::expr, sc: scope, v: vt<scope>) {
133
130
if !handled { visit:: visit_expr ( ex, sc, v) ; }
134
131
}
135
132
136
- fn register_locals ( cx : ctx , pat : @ast:: pat ) {
137
- for each pat in ast_util:: pat_bindings ( pat) {
138
- cx. local_map . insert ( pat. id , local ( cx. next_local ) ) ;
139
- cx. next_local += 1 u;
140
- }
141
- }
142
-
143
133
fn visit_decl ( cx : @ctx , d : @ast:: decl , sc : scope , v : vt < scope > ) {
144
134
visit:: visit_decl ( d, sc, v) ;
145
135
alt d. node {
@@ -154,7 +144,6 @@ fn visit_decl(cx: @ctx, d: @ast::decl, sc: scope, v: vt<scope>) {
154
144
}
155
145
none. { }
156
146
}
157
- register_locals ( * cx, loc. node . pat ) ;
158
147
}
159
148
}
160
149
_ { }
@@ -200,14 +189,17 @@ fn check_call(cx: ctx, f: @ast::expr, args: [@ast::expr]) -> [binding] {
200
189
}
201
190
}
202
191
let root_var = path_def_id ( cx, root. ex ) ;
203
- let new_bnd = mk_binding ( cx, arg. id , arg. span , root_var,
204
- inner_mut ( root. ds ) ) ;
205
- new_bnd. copied = alt arg_t. mode {
206
- ast:: by_move. { copied }
207
- ast:: by_ref. { ret_ref ? not_allowed : not_copied }
208
- ast:: by_mut_ref. { not_allowed }
209
- } ;
210
- bindings += [ new_bnd] ;
192
+ bindings += [ @{ node_id: arg. id ,
193
+ span: arg. span ,
194
+ root_var: root_var,
195
+ local_id: 0 u,
196
+ unsafe_tys: inner_mut ( root. ds ) ,
197
+ mutable ok: valid,
198
+ mutable copied: alt arg_t. mode {
199
+ ast:: by_move. { copied }
200
+ ast:: by_ref. { ret_ref ? not_allowed : not_copied }
201
+ ast:: by_mut_ref. { not_allowed }
202
+ } } ] ;
211
203
i += 1 u;
212
204
}
213
205
let f_may_close =
@@ -283,7 +275,7 @@ fn check_ret_ref(cx: ctx, sc: scope, mut: bool, expr: @ast::expr) {
283
275
let cur_node = did. node ;
284
276
while true {
285
277
alt cx. tcx . items . find ( cur_node) {
286
- some ( ast_map:: node_arg ( arg) ) {
278
+ some ( ast_map:: node_arg ( arg, _ ) ) {
287
279
if arg. mode == ast:: by_move {
288
280
bad = some ( "move-mode parameter" ) ;
289
281
}
@@ -358,7 +350,6 @@ fn check_alt(cx: ctx, input: @ast::expr, arms: [ast::arm], sc: scope,
358
350
new_bs += [ mk_binding ( cx, info. id , info. span , root_var,
359
351
copy info. unsafe ) ] ;
360
352
}
361
- register_locals ( cx, a. pats [ 0 ] ) ;
362
353
visit:: visit_arm ( a, { bs: new_bs with sc} , v) ;
363
354
}
364
355
}
@@ -373,7 +364,6 @@ fn check_for_each(cx: ctx, local: @ast::local, call: @ast::expr,
373
364
new_bs += [ mk_binding ( cx, proot. id , proot. span , none,
374
365
inner_mut ( proot. ds ) ) ] ;
375
366
}
376
- register_locals ( cx, local. node . pat ) ;
377
367
visit:: visit_block ( blk, { bs: new_bs with sc} , v) ;
378
368
}
379
369
}
@@ -401,17 +391,15 @@ fn check_for(cx: ctx, local: @ast::local, seq: @ast::expr, blk: ast::blk,
401
391
new_bs += [ mk_binding ( cx, proot. id , proot. span , root_var,
402
392
inner_mut ( proot. ds ) ) ] ;
403
393
}
404
- register_locals ( cx, local. node . pat ) ;
405
394
visit:: visit_block ( blk, { bs: new_bs with sc} , v) ;
406
395
}
407
396
408
397
fn check_var ( cx : ctx , ex : @ast:: expr , p : ast:: path , id : ast:: node_id ,
409
398
assign : bool , sc : scope ) {
410
399
let def = cx. tcx . def_map . get ( id) ;
411
- if !def_is_local ( def, true ) { ret; }
400
+ if !def_is_local ( def, false ) { ret; }
412
401
let my_defnum = ast_util:: def_id_of_def ( def) . node ;
413
- let my_local_id =
414
- alt cx. local_map . find ( my_defnum) { some ( local ( id) ) { id } _ { 0 u } } ;
402
+ let my_local_id = local_id_of_node ( cx, my_defnum) ;
415
403
let var_t = ty:: expr_ty ( cx. tcx , ex) ;
416
404
for b in sc. bs {
417
405
// excludes variables introduced since the alias was made
@@ -557,6 +545,12 @@ fn def_is_local(d: ast::def, objfields_count: bool) -> bool {
557
545
} ;
558
546
}
559
547
548
+ fn local_id_of_node ( cx : ctx , id : node_id ) -> uint {
549
+ alt cx. tcx . items . get ( id) {
550
+ ast_map:: node_arg ( _, id) | ast_map:: node_local ( id) { id }
551
+ }
552
+ }
553
+
560
554
// Heuristic, somewhat random way to decide whether to warn when inserting an
561
555
// implicit copy.
562
556
fn copy_is_expensive ( tcx : ty:: ctxt , ty : ty:: t ) -> bool {
0 commit comments