1515//! opaque, its signature/"outer shell" will be translated (e.g. for functions that's the
1616//! signature) but not its contents.
1717use itertools:: Itertools ;
18+ use petgraph:: visit:: { Dfs , Walker } ;
1819use rustc_middle:: ty:: { self , TyCtxt } ;
1920use rustc_span:: { def_id:: DefId , sym} ;
2021use std:: cell:: RefCell ;
@@ -227,13 +228,12 @@ impl<'tcx> RustcItem<'tcx> {
227228}
228229
229230impl < ' tcx > TranslateCtx < ' tcx > {
230- /// If this is a method declaration without a default , return the `DefId` of its parent trait.
231- fn is_method_decl_without_default ( & mut self , def_id : & hax:: DefId ) -> Option < hax:: DefId > {
231+ /// If this is a trait method declaration, return the `DefId` of its parent trait.
232+ fn as_trait_method_decl ( & mut self , def_id : & hax:: DefId ) -> Option < hax:: DefId > {
232233 if matches ! ( def_id. kind, hax:: DefKind :: AssocFn )
233234 && let def = self . poly_hax_def ( def_id) . ok ( ) ?
234235 && let hax:: FullDefKind :: AssocFn ( f) = def. kind ( )
235236 && let associated_item = f. associated_item ( )
236- && !associated_item. has_value
237237 && let hax:: AssocItemContainer :: TraitContainer { trait_ref } =
238238 & associated_item. container
239239 {
@@ -270,7 +270,7 @@ impl<'tcx> TranslateCtx<'tcx> {
270270 fn register_polymorphic_function ( & mut self , def_id : DefId ) -> FunDeclId {
271271 let def_id = def_id. sinto ( & self . hax_state ) ;
272272 let item = TransItemSource :: polymorphic ( & def_id, TransItemSourceKind :: Fun ) ;
273- self . register_and_enqueue ( & None , item) . unwrap ( )
273+ self . register_and_enqueue ( & None , item) . expect_item ( )
274274 }
275275
276276 fn register_poly_function_by_path ( & mut self , path : & str ) -> Result < FunDeclId , Error > {
@@ -381,19 +381,22 @@ impl<'tcx> TranslateCtx<'tcx> {
381381 /// recursively register the items it refers to. We call this on the crate root and end up
382382 /// exploring the whole crate.
383383 #[ tracing:: instrument( skip( self ) ) ]
384- pub fn enqueue_module_item ( & mut self , def_id : & hax:: DefId , started_from : bool ) {
385- if started_from {
386- self . started_from . insert ( def_id. clone ( ) ) ;
387- }
388- if let Some ( trait_def_id) = self . is_method_decl_without_default ( def_id) {
389- // Don't translate the method itself as it doesn't correspond to an item, translate the
390- // trait instead.
391- self . enqueue_module_item ( & trait_def_id, started_from) ;
392- return ;
384+ pub fn enqueue_module_item (
385+ & mut self ,
386+ def_id : & hax:: DefId ,
387+ // Item that contains this one.
388+ parent : ContainmentNode ,
389+ ) -> Option < ItemOrModuleId > {
390+ if let Some ( trait_def_id) = self . as_trait_method_decl ( def_id) {
391+ // Selecting a method also selects its containing trait. Mark the method used so
392+ // its default body is enqueued even if the trait is opaque.
393+ let trait_item_id = self . enqueue_module_item ( & trait_def_id, parent) ?;
394+ let trait_id = trait_item_id. expect_item ( ) ;
395+ let method_id = self . translate_trait_method_id ( trait_id, def_id) . ok ( ) ?;
396+ self . record_containment ( parent, ContainmentNode :: TraitMethod ( trait_id, method_id) ) ;
397+ return Some ( trait_item_id) ;
393398 }
394- let Some ( kind) = self . base_kind_for_item ( def_id) else {
395- return ;
396- } ;
399+ let kind = self . base_kind_for_item ( def_id) ?;
397400 let item_src = if self . options . monomorphize_with_hax {
398401 if let Ok ( def) = self . poly_hax_def ( def_id)
399402 && !def. has_any_generics ( )
@@ -402,24 +405,60 @@ impl<'tcx> TranslateCtx<'tcx> {
402405 TransItemSource :: monomorphic ( def. this ( ) , kind)
403406 } else {
404407 // Skip polymorphic items and items that cause errors.
405- return ;
408+ return None ;
406409 }
407410 } else {
408411 TransItemSource :: polymorphic ( def_id, kind)
409412 } ;
410- let _: Option < ItemId > = self . register_and_enqueue ( & None , item_src) ;
413+ let id = self . register_and_enqueue ( & None , item_src) ;
414+ self . record_containment ( parent, id) ;
415+ Some ( id)
416+ }
417+
418+ /// Record that the parent contains the child, for the purposes of computing which items are
419+ /// included in the "started from" set.
420+ pub ( crate ) fn record_containment (
421+ & mut self ,
422+ parent : impl Into < ContainmentNode > ,
423+ child : impl Into < ContainmentNode > ,
424+ ) {
425+ self . containment_graph
426+ . add_edge ( parent. into ( ) , child. into ( ) , ( ) ) ;
427+ }
428+
429+ /// Use the computed `containment_graph` to deduce which items should be considered as being
430+ /// targeted by `--start-from`, which then affects which items are used to compute used
431+ /// declarations.
432+ fn compute_started_from ( & mut self ) {
433+ let reachable: HashSet < _ > = Dfs :: new ( & self . containment_graph , ContainmentNode :: Start )
434+ . iter ( & self . containment_graph )
435+ . collect ( ) ;
436+ for node in & reachable {
437+ if let ContainmentNode :: Item ( ItemOrModuleId :: Item ( id) ) = node
438+ && let Some ( mut item) = self . translated . get_item_mut ( * id)
439+ {
440+ item. item_meta ( ) . started_from = true ;
441+ }
442+ }
443+ // Trait declarations also store method metadata.
444+ for tdecl in self . translated . trait_decls . iter_mut ( ) {
445+ for ( method_id, method) in tdecl. methods . iter_mut_indexed ( ) {
446+ let node = ContainmentNode :: TraitMethod ( tdecl. def_id , method_id) ;
447+ method. skip_binder . item_meta . started_from = reachable. contains ( & node) ;
448+ }
449+ }
411450 }
412451
413452 /// Whether this item is polymorphic, while we are in monomorphic mode.
414453 pub ( crate ) fn is_poly_in_mono ( & self , item_src : & TransItemSource ) -> bool {
415454 self . options . monomorphize_with_hax && matches ! ( item_src. item, RustcItem :: Poly ( ..) )
416455 }
417456
418- pub ( crate ) fn register_no_enqueue < T : TryFrom < ItemId > > (
457+ pub ( crate ) fn register_no_enqueue (
419458 & mut self ,
420459 dep_src : & Option < DepSource > ,
421460 src : & TransItemSource < ' tcx > ,
422- ) -> Option < T > {
461+ ) -> ItemOrModuleId {
423462 let item_id = match self . id_map . get ( src) {
424463 Some ( tid) => * tid,
425464 None => {
@@ -435,7 +474,9 @@ impl<'tcx> TranslateCtx<'tcx> {
435474 | VTableMethod ( ..) | VTableDropShim ( ..) => {
436475 ItemId :: Fun ( self . translated . fun_decls . reserve_slot ( ) )
437476 }
438- InherentImpl | Module => return None ,
477+ InherentImpl | Module => {
478+ return ItemOrModuleId :: Module ( src. def_id ( ) . real_rust_def_id ( ) ) ;
479+ }
439480 } ;
440481 // Add the id to the queue of declarations to translate
441482 self . id_map . insert ( src. clone ( ) , trans_id) ;
@@ -450,15 +491,15 @@ impl<'tcx> TranslateCtx<'tcx> {
450491 self . errors
451492 . borrow_mut ( )
452493 . register_dep_source ( dep_src, item_id, src. def_id ( ) . is_local ( ) ) ;
453- item_id . try_into ( ) . ok ( )
494+ ItemOrModuleId :: Item ( item_id )
454495 }
455496
456497 /// Register this item source and enqueue it for translation.
457- pub ( crate ) fn register_and_enqueue < T : TryFrom < ItemId > > (
498+ pub ( crate ) fn register_and_enqueue (
458499 & mut self ,
459500 dep_src : & Option < DepSource > ,
460501 item_src : TransItemSource < ' tcx > ,
461- ) -> Option < T > {
502+ ) -> ItemOrModuleId {
462503 let id = self . register_no_enqueue ( dep_src, & item_src) ;
463504 self . items_to_translate . push_back ( item_src) ;
464505 id
@@ -604,12 +645,16 @@ impl<'tcx> TranslateCtx<'tcx> {
604645 } else {
605646 TransItemSource :: polymorphic ( & def_id, TransItemSourceKind :: Type )
606647 } ;
607- let id: Option < TypeDeclId > = if self . options . no_gen_tuple_structs {
648+ let id = if self . options . no_gen_tuple_structs {
608649 self . register_and_enqueue ( & None , item_src)
609650 } else {
610651 self . register_no_enqueue ( & None , & item_src)
611652 } ;
612- assert_eq ! ( id, Some ( TypeDeclId :: UNIT ) , "the unit type must come first" ) ;
653+ assert_eq ! (
654+ id,
655+ ItemOrModuleId :: Item ( TypeDeclId :: UNIT . into( ) ) ,
656+ "the unit type must come first"
657+ ) ;
613658 }
614659
615660 pub ( crate ) fn register_target_info ( & mut self ) {
@@ -705,7 +750,7 @@ impl<'tcx> TranslateCtx<'tcx> {
705750impl < ' tcx , ' ctx > ItemTransCtx < ' tcx , ' ctx > {
706751 pub ( crate ) fn make_dep_source ( & self , span : Span ) -> Option < DepSource > {
707752 Some ( DepSource {
708- src_id : self . item_id ?,
753+ src_id : self . item_id . as_item ( ) ?,
709754 span : self . item_src . def_id ( ) . is_local ( ) . then_some ( span) ,
710755 } )
711756 }
@@ -717,7 +762,9 @@ impl<'tcx, 'ctx> ItemTransCtx<'tcx, 'ctx> {
717762 item_src : TransItemSource < ' tcx > ,
718763 ) -> T {
719764 let dep_src = self . make_dep_source ( span) ;
720- self . t_ctx . register_and_enqueue ( & dep_src, item_src) . unwrap ( )
765+ self . t_ctx
766+ . register_and_enqueue ( & dep_src, item_src)
767+ . expect_item ( )
721768 }
722769
723770 pub ( crate ) fn register_no_enqueue < T : TryFrom < ItemId > > (
@@ -726,7 +773,7 @@ impl<'tcx, 'ctx> ItemTransCtx<'tcx, 'ctx> {
726773 src : & TransItemSource < ' tcx > ,
727774 ) -> T {
728775 let dep_src = self . make_dep_source ( span) ;
729- self . t_ctx . register_no_enqueue ( & dep_src, src) . unwrap ( )
776+ self . t_ctx . register_no_enqueue ( & dep_src, src) . expect_item ( )
730777 }
731778
732779 /// Register this item and maybe enqueue it for translation.
@@ -1176,7 +1223,7 @@ pub fn translate<'tcx>(
11761223 file_to_id : Default :: default ( ) ,
11771224 items_to_translate : Default :: default ( ) ,
11781225 processed : Default :: default ( ) ,
1179- started_from : Default :: default ( ) ,
1226+ containment_graph : Default :: default ( ) ,
11801227 translate_stack : Default :: default ( ) ,
11811228 cached_spans : Default :: default ( ) ,
11821229 cached_file_ids : Default :: default ( ) ,
@@ -1195,7 +1242,7 @@ pub fn translate<'tcx>(
11951242 if let Ok ( def_ids) = ctx. resolve_path ( Span :: dummy ( ) , & pattern, strict) {
11961243 for def_id in def_ids {
11971244 let def_id: hax:: DefId = def_id. sinto ( & ctx. hax_state ) ;
1198- ctx. enqueue_module_item ( & def_id, true ) ;
1245+ ctx. enqueue_module_item ( & def_id, ContainmentNode :: Start ) ;
11991246 }
12001247 }
12011248 }
@@ -1209,7 +1256,7 @@ pub fn translate<'tcx>(
12091256 if !matches ! ( def_id. kind, hax:: DefKind :: Mod )
12101257 && def_id. attrs ( tcx) . iter ( ) . any ( |a| a. path_matches ( & attr_path) )
12111258 {
1212- ctx. enqueue_module_item ( & def_id, true ) ;
1259+ ctx. enqueue_module_item ( & def_id, ContainmentNode :: Start ) ;
12131260 }
12141261 } ;
12151262 for ldid in tcx. hir_crate_items ( ( ) ) . definitions ( ) {
@@ -1222,7 +1269,7 @@ pub fn translate<'tcx>(
12221269 if !matches ! ( def_id. kind, hax:: DefKind :: Mod )
12231270 && def_id. visibility ( tcx) == Some ( true )
12241271 {
1225- ctx. enqueue_module_item ( & def_id, true ) ;
1272+ ctx. enqueue_module_item ( & def_id, ContainmentNode :: Start ) ;
12261273 }
12271274 } ;
12281275 for ldid in tcx. hir_crate_items ( ( ) ) . definitions ( ) {
@@ -1260,6 +1307,7 @@ pub fn translate<'tcx>(
12601307 // Remove methods not marked as "used". They are never called and we made sure not to translate
12611308 // them. This removes them from the traits and impls.
12621309 ctx. remove_unused_methods ( ) ;
1310+ ctx. compute_started_from ( ) ;
12631311
12641312 // Return the context, dropping the hax state and rustc `tcx`.
12651313 Ok ( TransformCtx {
0 commit comments