@@ -26,7 +26,7 @@ use syntax::parse::token;
26
26
#[ derive( Copy , Clone , Debug ) ]
27
27
enum AutoderefKind {
28
28
Builtin ,
29
- Overloaded
29
+ Overloaded ,
30
30
}
31
31
32
32
pub struct Autoderef < ' a , ' gcx : ' tcx , ' tcx : ' a > {
@@ -35,7 +35,7 @@ pub struct Autoderef<'a, 'gcx: 'tcx, 'tcx: 'a> {
35
35
cur_ty : Ty < ' tcx > ,
36
36
obligations : Vec < traits:: PredicateObligation < ' tcx > > ,
37
37
at_start : bool ,
38
- span : Span
38
+ span : Span ,
39
39
}
40
40
41
41
impl < ' a , ' gcx , ' tcx > Iterator for Autoderef < ' a , ' gcx , ' tcx > {
@@ -45,7 +45,8 @@ impl<'a, 'gcx, 'tcx> Iterator for Autoderef<'a, 'gcx, 'tcx> {
45
45
let tcx = self . fcx . tcx ;
46
46
47
47
debug ! ( "autoderef: steps={:?}, cur_ty={:?}" ,
48
- self . steps, self . cur_ty) ;
48
+ self . steps,
49
+ self . cur_ty) ;
49
50
if self . at_start {
50
51
self . at_start = false ;
51
52
debug ! ( "autoderef stage #0 is {:?}" , self . cur_ty) ;
@@ -54,11 +55,13 @@ impl<'a, 'gcx, 'tcx> Iterator for Autoderef<'a, 'gcx, 'tcx> {
54
55
55
56
if self . steps . len ( ) == tcx. sess . recursion_limit . get ( ) {
56
57
// We've reached the recursion limit, error gracefully.
57
- struct_span_err ! ( tcx. sess, self . span, E0055 ,
58
- "reached the recursion limit while auto-dereferencing {:?}" ,
59
- self . cur_ty)
60
- . span_label ( self . span , & format ! ( "deref recursion limit reached" ) )
61
- . emit ( ) ;
58
+ struct_span_err ! ( tcx. sess,
59
+ self . span,
60
+ E0055 ,
61
+ "reached the recursion limit while auto-dereferencing {:?}" ,
62
+ self . cur_ty)
63
+ . span_label ( self . span , & format ! ( "deref recursion limit reached" ) )
64
+ . emit ( ) ;
62
65
return None ;
63
66
}
64
67
@@ -72,7 +75,7 @@ impl<'a, 'gcx, 'tcx> Iterator for Autoderef<'a, 'gcx, 'tcx> {
72
75
} else {
73
76
match self . overloaded_deref_ty ( self . cur_ty ) {
74
77
Some ( ty) => ( AutoderefKind :: Overloaded , ty) ,
75
- _ => return None
78
+ _ => return None ,
76
79
}
77
80
} ;
78
81
@@ -81,8 +84,10 @@ impl<'a, 'gcx, 'tcx> Iterator for Autoderef<'a, 'gcx, 'tcx> {
81
84
}
82
85
83
86
self . steps . push ( ( self . cur_ty , kind) ) ;
84
- debug ! ( "autoderef stage #{:?} is {:?} from {:?}" , self . steps. len( ) ,
85
- new_ty, ( self . cur_ty, kind) ) ;
87
+ debug ! ( "autoderef stage #{:?} is {:?} from {:?}" ,
88
+ self . steps. len( ) ,
89
+ new_ty,
90
+ ( self . cur_ty, kind) ) ;
86
91
self . cur_ty = new_ty;
87
92
88
93
Some ( ( self . cur_ty , self . steps . len ( ) ) )
@@ -99,9 +104,9 @@ impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
99
104
let trait_ref = TraitRef {
100
105
def_id : match tcx. lang_items . deref_trait ( ) {
101
106
Some ( f) => f,
102
- None => return None
107
+ None => return None ,
103
108
} ,
104
- substs : Substs :: new_trait ( tcx, self . cur_ty , & [ ] )
109
+ substs : Substs :: new_trait ( tcx, self . cur_ty , & [ ] ) ,
105
110
} ;
106
111
107
112
let cause = traits:: ObligationCause :: misc ( self . span , self . fcx . body_id ) ;
@@ -113,15 +118,13 @@ impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
113
118
return None ;
114
119
}
115
120
116
- let normalized = traits:: normalize_projection_type (
117
- & mut selcx,
118
- ty:: ProjectionTy {
119
- trait_ref : trait_ref,
120
- item_name : token:: intern ( "Target" )
121
- } ,
122
- cause,
123
- 0
124
- ) ;
121
+ let normalized = traits:: normalize_projection_type ( & mut selcx,
122
+ ty:: ProjectionTy {
123
+ trait_ref : trait_ref,
124
+ item_name : token:: intern ( "Target" ) ,
125
+ } ,
126
+ cause,
127
+ 0 ) ;
125
128
126
129
debug ! ( "overloaded_deref_ty({:?}) = {:?}" , ty, normalized) ;
127
130
self . obligations . extend ( normalized. obligations ) ;
@@ -134,17 +137,23 @@ impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
134
137
}
135
138
136
139
pub fn finalize < ' b , I > ( self , pref : LvaluePreference , exprs : I )
137
- where I : IntoIterator < Item = & ' b hir:: Expr >
140
+ where I : IntoIterator < Item = & ' b hir:: Expr >
138
141
{
139
- let methods : Vec < _ > = self . steps . iter ( ) . map ( |& ( ty, kind) | {
140
- if let AutoderefKind :: Overloaded = kind {
141
- self . fcx . try_overloaded_deref ( self . span , None , ty, pref)
142
- } else {
143
- None
144
- }
145
- } ) . collect ( ) ;
142
+ let methods: Vec < _ > = self . steps
143
+ . iter ( )
144
+ . map ( |& ( ty, kind) | {
145
+ if let AutoderefKind :: Overloaded = kind {
146
+ self . fcx . try_overloaded_deref ( self . span , None , ty, pref)
147
+ } else {
148
+ None
149
+ }
150
+ } )
151
+ . collect ( ) ;
146
152
147
- debug ! ( "finalize({:?}) - {:?},{:?}" , pref, methods, self . obligations) ;
153
+ debug ! ( "finalize({:?}) - {:?},{:?}" ,
154
+ pref,
155
+ methods,
156
+ self . obligations) ;
148
157
149
158
for expr in exprs {
150
159
debug ! ( "finalize - finalizing #{} - {:?}" , expr. id, expr) ;
@@ -163,18 +172,14 @@ impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
163
172
}
164
173
165
174
impl < ' a , ' gcx , ' tcx > FnCtxt < ' a , ' gcx , ' tcx > {
166
- pub fn autoderef ( & ' a self ,
167
- span : Span ,
168
- base_ty : Ty < ' tcx > )
169
- -> Autoderef < ' a , ' gcx , ' tcx >
170
- {
175
+ pub fn autoderef ( & ' a self , span : Span , base_ty : Ty < ' tcx > ) -> Autoderef < ' a , ' gcx , ' tcx > {
171
176
Autoderef {
172
177
fcx : self ,
173
178
steps : vec ! [ ] ,
174
179
cur_ty : self . resolve_type_vars_if_possible ( & base_ty) ,
175
180
obligations : vec ! [ ] ,
176
181
at_start : true ,
177
- span : span
182
+ span : span,
178
183
}
179
184
}
180
185
@@ -183,28 +188,36 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
183
188
base_expr : Option < & hir:: Expr > ,
184
189
base_ty : Ty < ' tcx > ,
185
190
lvalue_pref : LvaluePreference )
186
- -> Option < MethodCallee < ' tcx > >
187
- {
191
+ -> Option < MethodCallee < ' tcx > > {
188
192
debug ! ( "try_overloaded_deref({:?},{:?},{:?},{:?})" ,
189
- span, base_expr, base_ty, lvalue_pref) ;
193
+ span,
194
+ base_expr,
195
+ base_ty,
196
+ lvalue_pref) ;
190
197
// Try DerefMut first, if preferred.
191
198
let method = match ( lvalue_pref, self . tcx . lang_items . deref_mut_trait ( ) ) {
192
199
( PreferMutLvalue , Some ( trait_did) ) => {
193
- self . lookup_method_in_trait ( span, base_expr,
194
- token:: intern ( "deref_mut" ) , trait_did,
195
- base_ty, None )
200
+ self . lookup_method_in_trait ( span,
201
+ base_expr,
202
+ token:: intern ( "deref_mut" ) ,
203
+ trait_did,
204
+ base_ty,
205
+ None )
196
206
}
197
- _ => None
207
+ _ => None ,
198
208
} ;
199
209
200
210
// Otherwise, fall back to Deref.
201
211
let method = match ( method, self . tcx . lang_items . deref_trait ( ) ) {
202
212
( None , Some ( trait_did) ) => {
203
- self . lookup_method_in_trait ( span, base_expr,
204
- token:: intern ( "deref" ) , trait_did,
205
- base_ty, None )
213
+ self . lookup_method_in_trait ( span,
214
+ base_expr,
215
+ token:: intern ( "deref" ) ,
216
+ trait_did,
217
+ base_ty,
218
+ None )
206
219
}
207
- ( method, _) => method
220
+ ( method, _) => method,
208
221
} ;
209
222
210
223
method
0 commit comments