@@ -142,7 +142,7 @@ pub(crate) fn try_inline(
142142 Res :: Def ( DefKind :: Ctor ( ..) , _) | Res :: SelfCtor ( ..) => return Some ( Vec :: new ( ) ) ,
143143 Res :: Def ( DefKind :: Mod , did) => {
144144 record_extern_fqn ( cx, did, ItemType :: Module ) ;
145- clean:: ModuleItem ( build_module ( cx, did, visited) )
145+ clean:: ModuleItem ( build_module ( cx, did, name , visited) )
146146 }
147147 Res :: Def ( DefKind :: Static { .. } , did) => {
148148 record_extern_fqn ( cx, did, ItemType :: Static ) ;
@@ -207,6 +207,7 @@ pub(crate) fn try_inline_glob(
207207 let mut items = build_module_items (
208208 cx,
209209 did,
210+ cx. tcx . item_name ( did) ,
210211 visited,
211212 inlined_names,
212213 Some ( & reexports) ,
@@ -665,16 +666,43 @@ pub(crate) fn build_impl(
665666 ) ) ;
666667}
667668
668- fn build_module ( cx : & mut DocContext < ' _ > , did : DefId , visited : & mut DefIdSet ) -> clean:: Module {
669- let items = build_module_items ( cx, did, visited, & mut FxHashSet :: default ( ) , None , None ) ;
669+ fn build_module (
670+ cx : & mut DocContext < ' _ > ,
671+ did : DefId ,
672+ name : Symbol ,
673+ visited : & mut DefIdSet ,
674+ ) -> clean:: Module {
675+ let items = build_module_items ( cx, did, name, visited, & mut FxHashSet :: default ( ) , None , None ) ;
670676
671677 let span = clean:: Span :: new ( cx. tcx . def_span ( did) ) ;
672678 clean:: Module { items, span }
673679}
674680
681+ // We are only interested into `Res::Def`. And in there, we only want "items" which get their own
682+ // rustdoc page. So not `DefKind::Ctor` for example (which is returned by `tcx.module_children()`).
683+ fn should_ignore_res ( res : Res ) -> bool {
684+ !matches ! (
685+ res,
686+ Res :: Def ( DefKind :: Trait , _)
687+ | Res :: Def ( DefKind :: TraitAlias , _)
688+ | Res :: Def ( DefKind :: Fn , _)
689+ | Res :: Def ( DefKind :: Struct , _)
690+ | Res :: Def ( DefKind :: Union , _)
691+ | Res :: Def ( DefKind :: TyAlias , _)
692+ | Res :: Def ( DefKind :: Enum , _)
693+ | Res :: Def ( DefKind :: ForeignTy , _)
694+ | Res :: Def ( DefKind :: Variant , _)
695+ | Res :: Def ( DefKind :: Mod , _)
696+ | Res :: Def ( DefKind :: Static { .. } , _)
697+ | Res :: Def ( DefKind :: Const { .. } , _)
698+ | Res :: Def ( DefKind :: Macro ( _) , _)
699+ )
700+ }
701+
675702fn build_module_items (
676703 cx : & mut DocContext < ' _ > ,
677- did : DefId ,
704+ module_def_id : DefId ,
705+ module_name : Symbol ,
678706 visited : & mut DefIdSet ,
679707 inlined_names : & mut FxHashSet < ( ItemType , Symbol ) > ,
680708 allowed_def_ids : Option < & DefIdSet > ,
@@ -685,61 +713,107 @@ fn build_module_items(
685713 // If we're re-exporting a re-export it may actually re-export something in
686714 // two namespaces, so the target may be listed twice. Make sure we only
687715 // visit each node at most once.
688- for item in cx. tcx . module_children ( did) . iter ( ) {
689- if item. vis . is_public ( ) {
690- let res = item. res . expect_non_local ( ) ;
691- if let Some ( def_id) = res. opt_def_id ( )
692- && let Some ( allowed_def_ids) = allowed_def_ids
693- && !allowed_def_ids. contains ( & def_id)
716+ for item in cx. tcx . module_children ( module_def_id) . iter ( ) {
717+ if !item. vis . is_public ( ) {
718+ continue ;
719+ }
720+ let res = item. res . expect_non_local ( ) ;
721+ if let Some ( def_id) = res. opt_def_id ( )
722+ && let Some ( allowed_def_ids) = allowed_def_ids
723+ && !allowed_def_ids. contains ( & def_id)
724+ {
725+ continue ;
726+ }
727+ if let Some ( def_id) = res. mod_def_id ( ) {
728+ // If we're inlining a glob import, it's possible to have
729+ // two distinct modules with the same name. We don't want to
730+ // inline it, or mark any of its contents as visited.
731+ if module_def_id == def_id
732+ || inlined_names. contains ( & ( ItemType :: Module , item. ident . name ) )
733+ || !visited. insert ( def_id)
694734 {
695735 continue ;
696736 }
697- if let Some ( def_id) = res. mod_def_id ( ) {
698- // If we're inlining a glob import, it's possible to have
699- // two distinct modules with the same name. We don't want to
700- // inline it, or mark any of its contents as visited.
701- if did == def_id
702- || inlined_names. contains ( & ( ItemType :: Module , item. ident . name ) )
703- || !visited. insert ( def_id)
704- {
705- continue ;
706- }
707- }
708- if let Res :: PrimTy ( p) = res {
709- // Primitive types can't be inlined so generate an import instead.
710- let prim_ty = clean:: PrimitiveType :: from ( p) ;
711- items. push ( clean:: Item {
712- inner : Box :: new ( clean:: ItemInner {
713- name : None ,
714- // We can use the item's `DefId` directly since the only information ever
715- // used from it is `DefId.krate`.
716- item_id : ItemId :: DefId ( did) ,
717- attrs : Default :: default ( ) ,
718- stability : None ,
719- kind : clean:: ImportItem ( clean:: Import :: new_simple (
720- item. ident . name ,
721- clean:: ImportSource {
722- path : clean:: Path {
723- res,
724- segments : thin_vec ! [ clean:: PathSegment {
725- name: prim_ty. as_sym( ) ,
726- args: clean:: GenericArgs :: AngleBracketed {
727- args: Default :: default ( ) ,
728- constraints: ThinVec :: new( ) ,
729- } ,
730- } ] ,
731- } ,
732- did : None ,
737+ }
738+ if let Res :: PrimTy ( p) = res {
739+ // Primitive types can't be inlined so generate an import instead.
740+ let prim_ty = clean:: PrimitiveType :: from ( p) ;
741+ items. push ( clean:: Item {
742+ inner : Box :: new ( clean:: ItemInner {
743+ name : None ,
744+ // We can use the item's `DefId` directly since the only information ever
745+ // used from it is `DefId.krate`.
746+ item_id : ItemId :: DefId ( module_def_id) ,
747+ attrs : Default :: default ( ) ,
748+ stability : None ,
749+ kind : clean:: ImportItem ( clean:: Import :: new_simple (
750+ item. ident . name ,
751+ clean:: ImportSource {
752+ path : clean:: Path {
753+ res,
754+ segments : thin_vec ! [ clean:: PathSegment {
755+ name: prim_ty. as_sym( ) ,
756+ args: clean:: GenericArgs :: AngleBracketed {
757+ args: Default :: default ( ) ,
758+ constraints: ThinVec :: new( ) ,
759+ } ,
760+ } ] ,
733761 } ,
734- true ,
735- ) ) ,
736- cfg : None ,
737- inline_stmt_id : None ,
738- } ) ,
739- } ) ;
740- } else if let Some ( i) = try_inline ( cx, res, item. ident . name , attrs, visited) {
741- items. extend ( i)
762+ did : None ,
763+ } ,
764+ true ,
765+ ) ) ,
766+ cfg : None ,
767+ inline_stmt_id : None ,
768+ } ) ,
769+ } ) ;
770+ } else if let Some ( def_id) = res. opt_def_id ( )
771+ && let Some ( reexport) = item. reexport_chain . first ( )
772+ && let Some ( reexport_def_id) = reexport. id ( )
773+ && find_attr ! (
774+ load_attrs( cx. tcx, reexport_def_id) ,
775+ Doc ( d)
776+ if d. inline. first( ) . is_some_and( |( inline, _) | * inline == hir:: attrs:: DocInline :: NoInline )
777+ )
778+ {
779+ if should_ignore_res ( res) {
780+ continue ;
742781 }
782+ // This item is reexported as `no_inline` so it shouldn't be inlined.
783+ let item = Item :: from_def_id_and_parts (
784+ module_def_id,
785+ None ,
786+ clean:: ImportItem ( clean:: Import :: new_simple (
787+ item. ident . name ,
788+ clean:: ImportSource {
789+ path : clean:: Path {
790+ res,
791+ segments : thin_vec ! [
792+ clean:: PathSegment {
793+ name: module_name,
794+ args: clean:: GenericArgs :: AngleBracketed {
795+ args: Default :: default ( ) ,
796+ constraints: ThinVec :: new( ) ,
797+ } ,
798+ } ,
799+ clean:: PathSegment {
800+ name: cx. tcx. item_name( def_id) ,
801+ args: clean:: GenericArgs :: AngleBracketed {
802+ args: Default :: default ( ) ,
803+ constraints: ThinVec :: new( ) ,
804+ } ,
805+ } ,
806+ ] ,
807+ } ,
808+ did : None ,
809+ } ,
810+ true ,
811+ ) ) ,
812+ cx. tcx ,
813+ ) ;
814+ items. push ( item) ;
815+ } else if let Some ( i) = try_inline ( cx, res, item. ident . name , attrs, visited) {
816+ items. extend ( i)
743817 }
744818 }
745819
0 commit comments