@@ -42,30 +42,27 @@ pub struct BorrowckResult {
42
42
fn all_mir_bodies (
43
43
db : & dyn HirDatabase ,
44
44
def : DefWithBodyId ,
45
- ) -> Box < dyn Iterator < Item = Result < Arc < MirBody > , MirLowerError > > + ' _ > {
45
+ mut cb : impl FnMut ( Arc < MirBody > ) ,
46
+ ) -> Result < ( ) , MirLowerError > {
46
47
fn for_closure (
47
48
db : & dyn HirDatabase ,
48
49
c : ClosureId ,
49
- ) -> Box < dyn Iterator < Item = Result < Arc < MirBody > , MirLowerError > > + ' _ > {
50
+ cb : & mut impl FnMut ( Arc < MirBody > ) ,
51
+ ) -> Result < ( ) , MirLowerError > {
50
52
match db. mir_body_for_closure ( c) {
51
53
Ok ( body) => {
52
- let closures = body. closures . clone ( ) ;
53
- Box :: new (
54
- iter:: once ( Ok ( body) )
55
- . chain ( closures. into_iter ( ) . flat_map ( |it| for_closure ( db, it) ) ) ,
56
- )
54
+ cb ( body. clone ( ) ) ;
55
+ body. closures . iter ( ) . map ( |& it| for_closure ( db, it, cb) ) . collect ( )
57
56
}
58
- Err ( e) => Box :: new ( iter :: once ( Err ( e) ) ) ,
57
+ Err ( e) => Err ( e) ,
59
58
}
60
59
}
61
60
match db. mir_body ( def) {
62
61
Ok ( body) => {
63
- let closures = body. closures . clone ( ) ;
64
- Box :: new (
65
- iter:: once ( Ok ( body) ) . chain ( closures. into_iter ( ) . flat_map ( |it| for_closure ( db, it) ) ) ,
66
- )
62
+ cb ( body. clone ( ) ) ;
63
+ body. closures . iter ( ) . map ( |& it| for_closure ( db, it, & mut cb) ) . collect ( )
67
64
}
68
- Err ( e) => Box :: new ( iter :: once ( Err ( e) ) ) ,
65
+ Err ( e) => Err ( e) ,
69
66
}
70
67
}
71
68
@@ -74,17 +71,15 @@ pub fn borrowck_query(
74
71
def : DefWithBodyId ,
75
72
) -> Result < Arc < [ BorrowckResult ] > , MirLowerError > {
76
73
let _p = profile:: span ( "borrowck_query" ) ;
77
- let r = all_mir_bodies ( db, def)
78
- . map ( |body| {
79
- let body = body?;
80
- Ok ( BorrowckResult {
81
- mutability_of_locals : mutability_of_locals ( db, & body) ,
82
- moved_out_of_ref : moved_out_of_ref ( db, & body) ,
83
- mir_body : body,
84
- } )
85
- } )
86
- . collect :: < Result < Vec < _ > , MirLowerError > > ( ) ?;
87
- Ok ( r. into ( ) )
74
+ let mut res = vec ! [ ] ;
75
+ all_mir_bodies ( db, def, |body| {
76
+ res. push ( BorrowckResult {
77
+ mutability_of_locals : mutability_of_locals ( db, & body) ,
78
+ moved_out_of_ref : moved_out_of_ref ( db, & body) ,
79
+ mir_body : body,
80
+ } ) ;
81
+ } ) ?;
82
+ Ok ( res. into ( ) )
88
83
}
89
84
90
85
fn moved_out_of_ref ( db : & dyn HirDatabase , body : & MirBody ) -> Vec < MovedOutOfRef > {
@@ -277,21 +272,35 @@ fn ever_initialized_map(
277
272
) ;
278
273
return ;
279
274
} ;
280
- let targets = match & terminator. kind {
281
- TerminatorKind :: Goto { target } => vec ! [ * target] ,
282
- TerminatorKind :: SwitchInt { targets, .. } => targets. all_targets ( ) . to_vec ( ) ,
275
+ let mut process = |target, is_ever_initialized| {
276
+ if !result[ target] . contains_idx ( l) || !result[ target] [ l] && is_ever_initialized {
277
+ result[ target] . insert ( l, is_ever_initialized) ;
278
+ dfs ( db, body, target, l, result) ;
279
+ }
280
+ } ;
281
+ match & terminator. kind {
282
+ TerminatorKind :: Goto { target } => process ( * target, is_ever_initialized) ,
283
+ TerminatorKind :: SwitchInt { targets, .. } => {
284
+ targets. all_targets ( ) . iter ( ) . for_each ( |& it| process ( it, is_ever_initialized) ) ;
285
+ }
283
286
TerminatorKind :: UnwindResume
284
287
| TerminatorKind :: Abort
285
288
| TerminatorKind :: Return
286
- | TerminatorKind :: Unreachable => vec ! [ ] ,
289
+ | TerminatorKind :: Unreachable => ( ) ,
287
290
TerminatorKind :: Call { target, cleanup, destination, .. } => {
288
291
if destination. projection . len ( ) == 0 && destination. local == l {
289
292
is_ever_initialized = true ;
290
293
}
291
- target. into_iter ( ) . chain ( cleanup. into_iter ( ) ) . copied ( ) . collect ( )
294
+ target
295
+ . into_iter ( )
296
+ . chain ( cleanup. into_iter ( ) )
297
+ . for_each ( |& it| process ( it, is_ever_initialized) ) ;
292
298
}
293
299
TerminatorKind :: Drop { target, unwind, place : _ } => {
294
- Some ( target) . into_iter ( ) . chain ( unwind. into_iter ( ) ) . copied ( ) . collect ( )
300
+ iter:: once ( target)
301
+ . into_iter ( )
302
+ . chain ( unwind. into_iter ( ) )
303
+ . for_each ( |& it| process ( it, is_ever_initialized) ) ;
295
304
}
296
305
TerminatorKind :: DropAndReplace { .. }
297
306
| TerminatorKind :: Assert { .. }
@@ -300,13 +309,7 @@ fn ever_initialized_map(
300
309
| TerminatorKind :: FalseEdge { .. }
301
310
| TerminatorKind :: FalseUnwind { .. } => {
302
311
never ! ( "We don't emit these MIR terminators yet" ) ;
303
- vec ! [ ]
304
- }
305
- } ;
306
- for target in targets {
307
- if !result[ target] . contains_idx ( l) || !result[ target] [ l] && is_ever_initialized {
308
- result[ target] . insert ( l, is_ever_initialized) ;
309
- dfs ( db, body, target, l, result) ;
312
+ ( )
310
313
}
311
314
}
312
315
}
0 commit comments