@@ -203,9 +203,14 @@ use crate::errors::{
203203 EncounteredErrorWhileInstantiating , LargeAssignmentsLint , RecursionLimit , TypeLengthLimit ,
204204} ;
205205
206- #[ derive( PartialEq ) ]
206+ #[ derive( Debug , Copy , Clone ) ]
207207pub 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+ } ,
209214 Lazy ,
210215}
211216
@@ -351,6 +356,7 @@ pub fn collect_crate_mono_items(
351356 & mut recursion_depths,
352357 recursion_limit,
353358 inlining_map,
359+ mode,
354360 ) ;
355361 } ) ;
356362 } ) ;
@@ -408,6 +414,7 @@ fn collect_items_rec<'tcx>(
408414 recursion_depths : & mut DefIdMap < usize > ,
409415 recursion_limit : Limit ,
410416 inlining_map : MTRef < ' _ , MTLock < InliningMap < ' tcx > > > ,
417+ mode : MonoItemCollectionMode ,
411418) {
412419 if !visited. lock_mut ( ) . insert ( starting_point. node ) {
413420 // We've been here already, no need to search again.
@@ -476,7 +483,7 @@ fn collect_items_rec<'tcx>(
476483 check_type_length_limit ( tcx, instance) ;
477484
478485 rustc_data_structures:: stack:: ensure_sufficient_stack ( || {
479- collect_neighbours ( tcx, instance, & mut neighbors) ;
486+ collect_neighbours ( tcx, instance, & mut neighbors, mode ) ;
480487 } ) ;
481488 }
482489 MonoItem :: GlobalAsm ( item_id) => {
@@ -532,7 +539,15 @@ fn collect_items_rec<'tcx>(
532539 inlining_map. lock_mut ( ) . record_accesses ( starting_point. node , & neighbors. items ) ;
533540
534541 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+ ) ;
536551 }
537552
538553 if let Some ( ( def_id, depth) ) = recursion_depth_reset {
@@ -1191,7 +1206,7 @@ impl<'v> RootCollector<'_, 'v> {
11911206 fn process_item ( & mut self , id : hir:: ItemId ) {
11921207 match self . tcx . def_kind ( id. owner_id ) {
11931208 DefKind :: Enum | DefKind :: Struct | DefKind :: Union => {
1194- if self . mode == MonoItemCollectionMode :: Eager
1209+ if let MonoItemCollectionMode :: Eager { .. } = self . mode
11951210 && self . tcx . generics_of ( id. owner_id ) . count ( ) == 0
11961211 {
11971212 debug ! ( "RootCollector: ADT drop-glue for `{id:?}`" , ) ;
@@ -1224,7 +1239,7 @@ impl<'v> RootCollector<'_, 'v> {
12241239 }
12251240 }
12261241 DefKind :: Impl { .. } => {
1227- if self . mode == MonoItemCollectionMode :: Eager {
1242+ if let MonoItemCollectionMode :: Eager { .. } = self . mode {
12281243 create_mono_items_for_default_impls ( self . tcx , id, self . output ) ;
12291244 }
12301245 }
@@ -1244,7 +1259,7 @@ impl<'v> RootCollector<'_, 'v> {
12441259 fn is_root ( & self , def_id : LocalDefId ) -> bool {
12451260 !item_requires_monomorphization ( self . tcx , def_id)
12461261 && match self . mode {
1247- MonoItemCollectionMode :: Eager => true ,
1262+ MonoItemCollectionMode :: Eager { .. } => true ,
12481263 MonoItemCollectionMode :: Lazy => {
12491264 self . entry_fn . and_then ( |( id, _) | id. as_local ( ) ) == Some ( def_id)
12501265 || self . tcx . is_reachable_non_generic ( def_id)
@@ -1396,9 +1411,27 @@ fn collect_neighbours<'tcx>(
13961411 tcx : TyCtxt < ' tcx > ,
13971412 instance : Instance < ' tcx > ,
13981413 output : & mut MonoItems < ' tcx > ,
1414+ mode : MonoItemCollectionMode ,
13991415) {
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 ) ) ;
14021435}
14031436
14041437#[ instrument( skip( tcx, output) , level = "debug" ) ]
0 commit comments