10
10
11
11
// Type substitutions.
12
12
13
-
14
13
use middle:: ty;
14
+ use middle:: ty_fold;
15
+ use middle:: ty_fold:: TypeFolder ;
15
16
use syntax:: opt_vec:: OptVec ;
16
- use util :: ppaux :: Repr ;
17
+ use std :: at_vec ;
17
18
18
19
///////////////////////////////////////////////////////////////////////////
19
20
// Public trait `Subst`
@@ -33,39 +34,43 @@ pub trait Subst {
33
34
// to all subst methods but ran into trouble due to the limitations of
34
35
// our current method/trait matching algorithm. - Niko
35
36
36
- trait EffectfulSubst {
37
- fn effectfulSubst ( & self , tcx : ty:: ctxt , substs : & ty:: substs ) -> Self ;
38
- }
39
-
40
37
impl Subst for ty:: t {
41
38
fn subst ( & self , tcx : ty:: ctxt , substs : & ty:: substs ) -> ty:: t {
42
39
if ty:: substs_is_noop ( substs) {
43
- return * self ;
40
+ * self
44
41
} else {
45
- return self . effectfulSubst ( tcx, substs) ;
42
+ let mut folder = SubstFolder { tcx : tcx, substs : substs} ;
43
+ folder. fold_ty ( * self )
46
44
}
47
45
}
48
46
}
49
47
50
- impl EffectfulSubst for ty:: t {
51
- fn effectfulSubst ( & self , tcx : ty:: ctxt , substs : & ty:: substs ) -> ty:: t {
52
- if !ty:: type_needs_subst ( * self ) {
53
- return * self ;
48
+ struct SubstFolder < ' self > {
49
+ tcx : ty:: ctxt ,
50
+ substs : & ' self ty:: substs
51
+ }
52
+
53
+ impl < ' self > TypeFolder for SubstFolder < ' self > {
54
+ fn tcx ( & self ) -> ty:: ctxt { self . tcx }
55
+
56
+ fn fold_region ( & mut self , r : ty:: Region ) -> ty:: Region {
57
+ r. subst ( self . tcx , self . substs )
58
+ }
59
+
60
+ fn fold_ty ( & mut self , t : ty:: t ) -> ty:: t {
61
+ if !ty:: type_needs_subst ( t) {
62
+ return t;
54
63
}
55
64
56
- match ty:: get ( * self ) . sty {
65
+ match ty:: get ( t ) . sty {
57
66
ty:: ty_param( p) => {
58
- substs. tps [ p. idx ]
67
+ self . substs . tps [ p. idx ]
59
68
}
60
69
ty:: ty_self( _) => {
61
- substs. self_ty . expect ( "ty_self not found in substs" )
70
+ self . substs . self_ty . expect ( "ty_self not found in substs" )
62
71
}
63
72
_ => {
64
- ty:: fold_regions_and_ty (
65
- tcx, * self ,
66
- |r| r. subst ( tcx, substs) ,
67
- |t| t. effectfulSubst ( tcx, substs) ,
68
- |t| t. effectfulSubst ( tcx, substs) )
73
+ ty_fold:: super_fold_ty ( self , t)
69
74
}
70
75
}
71
76
}
@@ -80,6 +85,12 @@ impl<T:Subst> Subst for ~[T] {
80
85
}
81
86
}
82
87
88
+ impl < T : Subst > Subst for @[ T ] {
89
+ fn subst ( & self , tcx : ty:: ctxt , substs : & ty:: substs ) -> @[ T ] {
90
+ at_vec:: map ( * self , |t| t. subst ( tcx, substs) )
91
+ }
92
+ }
93
+
83
94
impl < T : Subst > Subst for OptVec < T > {
84
95
fn subst ( & self , tcx : ty:: ctxt , substs : & ty:: substs ) -> OptVec < T > {
85
96
self . map ( |t| t. subst ( tcx, substs) )
@@ -134,7 +145,8 @@ impl Subst for ty::RegionSubsts {
134
145
135
146
impl Subst for ty:: BareFnTy {
136
147
fn subst ( & self , tcx : ty:: ctxt , substs : & ty:: substs ) -> ty:: BareFnTy {
137
- ty:: fold_bare_fn_ty ( self , |t| t. subst ( tcx, substs) )
148
+ let mut folder = SubstFolder { tcx : tcx, substs : substs} ;
149
+ folder. fold_bare_fn_ty ( self )
138
150
}
139
151
}
140
152
0 commit comments