@@ -139,7 +139,6 @@ export regionck;
139
139
export demand;
140
140
export method;
141
141
export fn_ctxt;
142
- export lookup_local;
143
142
export impl_self_ty;
144
143
export DerefArgs ;
145
144
export DontDerefArgs ;
@@ -189,7 +188,7 @@ type self_info = {
189
188
/// share the inherited fields.
190
189
struct inherited {
191
190
infcx : @infer:: InferCtxt ,
192
- locals : HashMap < ast:: node_id , TyVid > ,
191
+ locals : HashMap < ast:: node_id , ty :: t > ,
193
192
node_types : HashMap < ast:: node_id , ty:: t > ,
194
193
node_type_substs : HashMap < ast:: node_id , ty:: substs > ,
195
194
adjustments : HashMap < ast:: node_id , @ty:: AutoAdjustment >
@@ -376,8 +375,7 @@ fn check_fn(ccx: @crate_ctxt,
376
375
}
377
376
} ;
378
377
379
- // XXX: Bad copy.
380
- gather_locals ( fcx, decl, body, copy arg_tys, self_info) ;
378
+ gather_locals ( fcx, decl, body, arg_tys, self_info) ;
381
379
check_block ( fcx, body) ;
382
380
383
381
// We unify the tail expr's type with the
@@ -414,38 +412,39 @@ fn check_fn(ccx: @crate_ctxt,
414
412
fn gather_locals ( fcx : @fn_ctxt ,
415
413
decl : & ast:: fn_decl ,
416
414
body : ast:: blk ,
417
- arg_tys : ~ [ ty:: t ] ,
415
+ arg_tys : & [ ty:: t ] ,
418
416
self_info : Option < self_info > ) {
419
417
let tcx = fcx. ccx . tcx ;
420
418
421
- let assign = fn @( span: span, nid: ast:: node_id,
422
- ty_opt: Option <ty:: t>) {
423
- let var_id = fcx. infcx ( ) . next_ty_var_id ( ) ;
424
- fcx. inh . locals . insert ( nid, var_id) ;
419
+ let assign = fn @( nid: ast:: node_id, ty_opt: Option <ty:: t>) {
425
420
match ty_opt {
426
- None => { /* nothing to do */ }
421
+ None => {
422
+ // infer the variable's type
423
+ let var_id = fcx. infcx ( ) . next_ty_var_id ( ) ;
424
+ let var_ty = ty:: mk_var ( fcx. tcx ( ) , var_id) ;
425
+ fcx. inh . locals . insert ( nid, var_ty) ;
426
+ }
427
427
Some ( typ) => {
428
- infer :: mk_eqty ( fcx . infcx ( ) , false , span ,
429
- ty :: mk_var ( tcx , var_id ) , typ) ;
428
+ // take type that the user specified
429
+ fcx . inh . locals . insert ( nid , typ) ;
430
430
}
431
431
}
432
432
} ;
433
433
434
434
// Add the self parameter
435
435
for self_info. each |self_info| {
436
- assign ( self_info. explicit_self . span ,
437
- self_info. self_id ,
438
- Some ( self_info. self_ty ) ) ;
436
+ assign ( self_info. self_id , Some ( self_info. self_ty ) ) ;
439
437
debug ! ( "self is assigned to %s" ,
440
- fcx. inh. locals. get( self_info. self_id) . to_str( ) ) ;
438
+ fcx. infcx( ) . ty_to_str(
439
+ fcx. inh. locals. get( self_info. self_id) ) ) ;
441
440
}
442
441
443
442
// Add formal parameters.
444
443
for vec:: each2( arg_tys, decl. inputs) |arg_ty, input| {
445
444
// Create type variables for each argument.
446
445
do pat_util:: pat_bindings( tcx. def_map, input. pat)
447
446
|_bm, pat_id, _sp, _path| {
448
- assign( input . ty . span , pat_id, None ) ;
447
+ assign( pat_id, None ) ;
449
448
}
450
449
451
450
// Check the pattern.
@@ -466,10 +465,11 @@ fn check_fn(ccx: @crate_ctxt,
466
465
ast:: ty_infer => None ,
467
466
_ => Some ( fcx. to_ty ( local. node . ty ) )
468
467
} ;
469
- assign ( local. span , local . node . id , o_ty) ;
470
- debug ! ( "Local variable %s is assigned to %s" ,
468
+ assign ( local. node . id , o_ty) ;
469
+ debug ! ( "Local variable %s is assigned type %s" ,
471
470
fcx. pat_to_str( local. node. pat) ,
472
- fcx. inh. locals. get( local. node. id) . to_str( ) ) ;
471
+ fcx. infcx( ) . ty_to_str(
472
+ fcx. inh. locals. get( local. node. id) ) ) ;
473
473
visit:: visit_local ( local, e, v) ;
474
474
} ;
475
475
@@ -478,10 +478,11 @@ fn check_fn(ccx: @crate_ctxt,
478
478
match p. node {
479
479
ast:: pat_ident( _, path, _)
480
480
if pat_util:: pat_is_binding ( fcx. ccx . tcx . def_map , p) => {
481
- assign ( p. span , p . id , None ) ;
481
+ assign ( p. id , None ) ;
482
482
debug ! ( "Pattern binding %s is assigned to %s" ,
483
483
tcx. sess. str_of( path. idents[ 0 ] ) ,
484
- fcx. inh. locals. get( p. id) . to_str( ) ) ;
484
+ fcx. infcx( ) . ty_to_str(
485
+ fcx. inh. locals. get( p. id) ) ) ;
485
486
}
486
487
_ => { }
487
488
}
@@ -694,6 +695,17 @@ impl @fn_ctxt: region_scope {
694
695
impl @fn_ctxt {
695
696
fn tag ( ) -> ~str { fmt ! ( "%x" , ptr:: addr_of( & ( * self ) ) as uint) }
696
697
698
+ fn local_ty ( span : span , nid : ast:: node_id ) -> ty:: t {
699
+ match self . inh . locals . find ( nid) {
700
+ Some ( t) => t,
701
+ None => {
702
+ self . tcx ( ) . sess . span_bug (
703
+ span,
704
+ fmt ! ( "No type for local variable %?" , nid) ) ;
705
+ }
706
+ }
707
+ }
708
+
697
709
fn expr_to_str ( expr : @ast:: expr ) -> ~str {
698
710
fmt ! ( "expr(%?:%s)" , expr. id,
699
711
pprust:: expr_to_str( expr, self . tcx( ) . sess. intr( ) ) )
@@ -1359,10 +1371,8 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
1359
1371
fn check_for ( fcx : @fn_ctxt , local : @ast:: local ,
1360
1372
element_ty : ty:: t , body : ast:: blk ,
1361
1373
node_id : ast:: node_id ) -> bool {
1362
- let locid = lookup_local ( fcx, local. span , local. node . id ) ;
1363
- demand:: suptype ( fcx, local. span ,
1364
- ty:: mk_var ( fcx. ccx . tcx , locid) ,
1365
- element_ty) ;
1374
+ let local_ty = fcx. local_ty ( local. span , local. node . id ) ;
1375
+ demand:: suptype ( fcx, local. span , local_ty, element_ty) ;
1366
1376
let bot = check_decl_local ( fcx, local) ;
1367
1377
check_block_no_value ( fcx, body) ;
1368
1378
fcx. write_nil ( node_id) ;
@@ -2551,15 +2561,15 @@ fn require_integral(fcx: @fn_ctxt, sp: span, t: ty::t) {
2551
2561
2552
2562
fn check_decl_initializer ( fcx : @fn_ctxt , nid : ast:: node_id ,
2553
2563
init : @ast:: expr ) -> bool {
2554
- let lty = ty :: mk_var ( fcx. ccx . tcx , lookup_local ( fcx , init. span , nid) ) ;
2555
- return check_expr_coercable_to_type ( fcx, init, lty ) ;
2564
+ let local_ty = fcx. local_ty ( init. span , nid) ;
2565
+ return check_expr_coercable_to_type ( fcx, init, local_ty ) ;
2556
2566
}
2557
2567
2558
2568
fn check_decl_local ( fcx : @fn_ctxt , local : @ast:: local ) -> bool {
2559
2569
let mut bot = false ;
2560
2570
let tcx = fcx. ccx . tcx ;
2561
2571
2562
- let t = ty :: mk_var ( tcx , fcx. inh . locals . get ( local. node . id ) ) ;
2572
+ let t = fcx. local_ty ( local. span , local . node . id ) ;
2563
2573
fcx. write_ty ( local. node . id , t) ;
2564
2574
2565
2575
match local. node . init {
@@ -2819,17 +2829,6 @@ fn check_enum_variants(ccx: @crate_ctxt,
2819
2829
check_instantiable ( ccx. tcx , sp, id) ;
2820
2830
}
2821
2831
2822
- pub fn lookup_local ( fcx : @fn_ctxt , sp : span , id : ast:: node_id ) -> TyVid {
2823
- match fcx. inh . locals . find ( id) {
2824
- Some ( x) => x,
2825
- _ => {
2826
- fcx. ccx . tcx . sess . span_fatal (
2827
- sp,
2828
- ~"internal error looking up a local var")
2829
- }
2830
- }
2831
- }
2832
-
2833
2832
fn lookup_def ( fcx : @fn_ctxt , sp : span , id : ast:: node_id ) -> ast:: def {
2834
2833
lookup_def_ccx ( fcx. ccx , sp, id)
2835
2834
}
@@ -2841,9 +2840,8 @@ fn ty_param_bounds_and_ty_for_def(fcx: @fn_ctxt, sp: span, defn: ast::def) ->
2841
2840
match defn {
2842
2841
ast:: def_arg( nid, _, _) | ast:: def_local( nid, _) |
2843
2842
ast:: def_self( nid, _) | ast:: def_binding( nid, _) => {
2844
- assert ( fcx. inh . locals . contains_key ( nid) ) ;
2845
- let typ = ty:: mk_var ( fcx. ccx . tcx , lookup_local ( fcx, sp, nid) ) ;
2846
- return no_params ( typ) ;
2843
+ let typ = fcx. local_ty ( sp, nid) ;
2844
+ return no_params ( typ) ;
2847
2845
}
2848
2846
ast:: def_fn( _, ast:: extern_fn) => {
2849
2847
// extern functions are just u8 pointers
0 commit comments