@@ -203,9 +203,14 @@ use crate::errors::{
203
203
EncounteredErrorWhileInstantiating , LargeAssignmentsLint , RecursionLimit , TypeLengthLimit ,
204
204
} ;
205
205
206
- #[ derive( PartialEq ) ]
206
+ #[ derive( Debug , Copy , Clone ) ]
207
207
pub enum MonoItemCollectionMode {
208
- Eager ,
208
+ Eager {
209
+ /// Whether to use optimized mir for collection or just analyis MIR.
210
+ /// By default uses optimized mir, but for `cargo check` we use unoptimized mir,
211
+ /// as that is faster.
212
+ optimized_mir : bool ,
213
+ } ,
209
214
Lazy ,
210
215
}
211
216
@@ -351,6 +356,7 @@ pub fn collect_crate_mono_items(
351
356
& mut recursion_depths,
352
357
recursion_limit,
353
358
inlining_map,
359
+ mode,
354
360
) ;
355
361
} ) ;
356
362
} ) ;
@@ -408,6 +414,7 @@ fn collect_items_rec<'tcx>(
408
414
recursion_depths : & mut DefIdMap < usize > ,
409
415
recursion_limit : Limit ,
410
416
inlining_map : MTRef < ' _ , MTLock < InliningMap < ' tcx > > > ,
417
+ mode : MonoItemCollectionMode ,
411
418
) {
412
419
if !visited. lock_mut ( ) . insert ( starting_point. node ) {
413
420
// We've been here already, no need to search again.
@@ -476,7 +483,7 @@ fn collect_items_rec<'tcx>(
476
483
check_type_length_limit ( tcx, instance) ;
477
484
478
485
rustc_data_structures:: stack:: ensure_sufficient_stack ( || {
479
- collect_neighbours ( tcx, instance, & mut neighbors) ;
486
+ collect_neighbours ( tcx, instance, & mut neighbors, mode ) ;
480
487
} ) ;
481
488
}
482
489
MonoItem :: GlobalAsm ( item_id) => {
@@ -532,7 +539,15 @@ fn collect_items_rec<'tcx>(
532
539
inlining_map. lock_mut ( ) . record_accesses ( starting_point. node , & neighbors. items ) ;
533
540
534
541
for ( neighbour, _) in neighbors. items {
535
- collect_items_rec ( tcx, neighbour, visited, recursion_depths, recursion_limit, inlining_map) ;
542
+ collect_items_rec (
543
+ tcx,
544
+ neighbour,
545
+ visited,
546
+ recursion_depths,
547
+ recursion_limit,
548
+ inlining_map,
549
+ mode,
550
+ ) ;
536
551
}
537
552
538
553
if let Some ( ( def_id, depth) ) = recursion_depth_reset {
@@ -1191,7 +1206,7 @@ impl<'v> RootCollector<'_, 'v> {
1191
1206
fn process_item ( & mut self , id : hir:: ItemId ) {
1192
1207
match self . tcx . def_kind ( id. owner_id ) {
1193
1208
DefKind :: Enum | DefKind :: Struct | DefKind :: Union => {
1194
- if self . mode == MonoItemCollectionMode :: Eager
1209
+ if let MonoItemCollectionMode :: Eager { .. } = self . mode
1195
1210
&& self . tcx . generics_of ( id. owner_id ) . count ( ) == 0
1196
1211
{
1197
1212
debug ! ( "RootCollector: ADT drop-glue for `{id:?}`" , ) ;
@@ -1224,7 +1239,7 @@ impl<'v> RootCollector<'_, 'v> {
1224
1239
}
1225
1240
}
1226
1241
DefKind :: Impl { .. } => {
1227
- if self . mode == MonoItemCollectionMode :: Eager {
1242
+ if let MonoItemCollectionMode :: Eager { .. } = self . mode {
1228
1243
create_mono_items_for_default_impls ( self . tcx , id, self . output ) ;
1229
1244
}
1230
1245
}
@@ -1244,7 +1259,7 @@ impl<'v> RootCollector<'_, 'v> {
1244
1259
fn is_root ( & self , def_id : LocalDefId ) -> bool {
1245
1260
!item_requires_monomorphization ( self . tcx , def_id)
1246
1261
&& match self . mode {
1247
- MonoItemCollectionMode :: Eager => true ,
1262
+ MonoItemCollectionMode :: Eager { .. } => true ,
1248
1263
MonoItemCollectionMode :: Lazy => {
1249
1264
self . entry_fn . and_then ( |( id, _) | id. as_local ( ) ) == Some ( def_id)
1250
1265
|| self . tcx . is_reachable_non_generic ( def_id)
@@ -1396,9 +1411,27 @@ fn collect_neighbours<'tcx>(
1396
1411
tcx : TyCtxt < ' tcx > ,
1397
1412
instance : Instance < ' tcx > ,
1398
1413
output : & mut MonoItems < ' tcx > ,
1414
+ mode : MonoItemCollectionMode ,
1399
1415
) {
1400
- let body = tcx. instance_mir ( instance. def ) ;
1401
- MirNeighborCollector { tcx, body : & body, output, instance } . visit_body ( & body) ;
1416
+ let mut collect = |body : & mir:: Body < ' tcx > | {
1417
+ MirNeighborCollector { tcx, body : & body, output, instance } . visit_body ( body)
1418
+ } ;
1419
+ if let MonoItemCollectionMode :: Eager { optimized_mir : false } = mode {
1420
+ if let ty:: InstanceDef :: Item ( def) = instance. def {
1421
+ let def_kind = tcx. def_kind ( def. did ) ;
1422
+ match def_kind {
1423
+ // Generators get their optimized_mir taken (and thus drop elab mir stolen) in order
1424
+ // to compute their Send/Sync bounds.
1425
+ DefKind :: Generator |
1426
+ DefKind :: Ctor ( ..) => { } ,
1427
+ _ => if let Some ( def) = def. as_local ( ) && !tcx. sess . opts . unstable_opts . polymorphize {
1428
+ collect ( & * tcx. mir_drops_elaborated_and_const_checked ( def) . borrow ( ) ) ;
1429
+ return ;
1430
+ } ,
1431
+ }
1432
+ }
1433
+ }
1434
+ collect ( tcx. instance_mir ( instance. def ) ) ;
1402
1435
}
1403
1436
1404
1437
#[ instrument( skip( tcx, output) , level = "debug" ) ]
0 commit comments