@@ -11,7 +11,7 @@ use rustc_ast_pretty::pprust;
1111use  rustc_attr:: { ConstStability ,  Deprecation ,  Stability ,  StabilityLevel ,  StableSince } ; 
1212use  rustc_const_eval:: const_eval:: is_unstable_const_fn; 
1313use  rustc_data_structures:: fx:: { FxHashMap ,  FxHashSet } ; 
14- use  rustc_hir:: def:: { CtorKind ,  DefKind ,  Res } ; 
14+ use  rustc_hir:: def:: { CtorKind ,  DefKind ,  Namespace ,   Res } ; 
1515use  rustc_hir:: def_id:: { CrateNum ,  DefId ,  LocalDefId ,  LOCAL_CRATE } ; 
1616use  rustc_hir:: lang_items:: LangItem ; 
1717use  rustc_hir:: { BodyId ,  Mutability } ; 
@@ -57,20 +57,34 @@ pub(crate) type ItemIdSet = FxHashSet<ItemId>;
5757#[ derive( Debug ,  Clone ,  PartialEq ,  Eq ,  Hash ,  Copy ) ]  
5858pub ( crate )  enum  ItemId  { 
5959    /// A "normal" item that uses a [`DefId`] for identification. 
60- DefId ( DefId ) , 
60+ DefId ( DefId ,   Option < Namespace > ) , 
6161    /// Identifier that is used for auto traits. 
6262Auto  {  trait_ :  DefId ,  for_ :  DefId  } , 
6363    /// Identifier that is used for blanket implementations. 
6464Blanket  {  impl_id :  DefId ,  for_ :  DefId  } , 
6565} 
6666
6767impl  ItemId  { 
68+     pub ( crate )  fn  type_ns ( did :  DefId )  -> Self  { 
69+         Self :: DefId ( did,  Some ( Namespace :: TypeNS ) ) 
70+     } 
71+ 
72+     pub ( crate )  fn  opt_from_res < Id > ( res :  & Res < Id > )  -> Option < Self >  { 
73+         res. opt_def_id ( ) . map ( |did| Self :: DefId ( did,  res. ns ( ) ) ) 
74+     } 
75+ 
76+     #[ track_caller]  
77+     pub ( crate )  fn  from_res < Id :  std:: fmt:: Debug > ( res :  & Res < Id > )  -> Self  { 
78+         Self :: opt_from_res ( res) 
79+             . unwrap_or_else ( || panic ! ( "attempted to get the ItemId of a wrong Res: `{res:?}`" ) ) 
80+     } 
81+ 
6882    #[ inline]  
6983    pub ( crate )  fn  is_local ( self )  -> bool  { 
7084        match  self  { 
7185            ItemId :: Auto  {  for_ :  id,  .. } 
7286            | ItemId :: Blanket  {  for_ :  id,  .. } 
73-             | ItemId :: DefId ( id)  => id. is_local ( ) , 
87+             | ItemId :: DefId ( id,  _ )  => id. is_local ( ) , 
7488        } 
7589    } 
7690
@@ -84,7 +98,7 @@ impl ItemId {
8498    #[ inline]  
8599    pub ( crate )  fn  as_def_id ( self )  -> Option < DefId >  { 
86100        match  self  { 
87-             ItemId :: DefId ( id)  => Some ( id) , 
101+             ItemId :: DefId ( id,  _ )  => Some ( id) , 
88102            _ => None , 
89103        } 
90104    } 
@@ -99,17 +113,11 @@ impl ItemId {
99113        match  self  { 
100114            ItemId :: Auto  {  for_ :  id,  .. } 
101115            | ItemId :: Blanket  {  for_ :  id,  .. } 
102-             | ItemId :: DefId ( id)  => id. krate , 
116+             | ItemId :: DefId ( id,  _ )  => id. krate , 
103117        } 
104118    } 
105119} 
106120
107- impl  From < DefId >  for  ItemId  { 
108-     fn  from ( id :  DefId )  -> Self  { 
109-         Self :: DefId ( id) 
110-     } 
111- } 
112- 
113121/// The crate currently being documented. 
114122#[ derive( Clone ,  Debug ) ]  
115123pub ( crate )  struct  Crate  { 
@@ -136,6 +144,11 @@ pub(crate) struct ExternalCrate {
136144impl  ExternalCrate  { 
137145    const  LOCAL :  Self  = Self  {  crate_num :  LOCAL_CRATE  } ; 
138146
147+     #[ inline]  
148+     pub ( crate )  fn  item_id ( & self )  -> ItemId  { 
149+         ItemId :: type_ns ( self . crate_num . as_def_id ( ) ) 
150+     } 
151+ 
139152    #[ inline]  
140153    pub ( crate )  fn  def_id ( & self )  -> DefId  { 
141154        self . crate_num . as_def_id ( ) 
@@ -450,8 +463,15 @@ impl Item {
450463    )  -> Item  { 
451464        trace ! ( "name={name:?}, def_id={def_id:?} cfg={cfg:?}" ) ; 
452465
466+         let  ns = if  let  ItemKind :: ImportItem ( i)  = & kind
467+             && let  ImportKind :: Simple ( _)  = i. kind 
468+         { 
469+             i. source . path . res . ns ( ) 
470+         }  else  { 
471+             ItemType :: from ( & kind) . ns ( ) 
472+         } ; 
453473        Item  { 
454-             item_id :  def_id . into ( ) , 
474+             item_id :  ItemId :: DefId ( def_id ,  ns ) , 
455475            kind :  Box :: new ( kind) , 
456476            name, 
457477            attrs, 
@@ -466,7 +486,7 @@ impl Item {
466486        let  Some ( links)  = cx. cache ( ) . intra_doc_links . get ( & self . item_id )  else  {  return  vec ! [ ]  } ; 
467487        links
468488            . iter ( ) 
469-             . filter_map ( |ItemLink  {  link :  s,  link_text,  page_id :  id,  ref  fragment } | { 
489+             . filter_map ( |ItemLink  {  link :  s,  link_text,  page_id :  id,  ref  fragment,  ..  } | { 
470490                debug ! ( ?id) ; 
471491                if  let  Ok ( ( mut  href,  ..) )  = href ( * id,  cx)  { 
472492                    debug ! ( ?href) ; 
@@ -491,7 +511,7 @@ impl Item {
491511/// This is used for generating summary text, which does not include 
492512/// the link text, but does need to know which `[]`-bracketed names 
493513/// are actually links. 
494- pub ( crate )  fn  link_names ( & self ,  cache :  & Cache )  -> Vec < RenderedLink >  { 
514+ pub ( crate )  fn  link_names ( & self ,  cache :  & Cache < ' _ > )  -> Vec < RenderedLink >  { 
495515        let  Some ( links)  = cache. intra_doc_links . get ( & self . item_id )  else  { 
496516            return  vec ! [ ] ; 
497517        } ; 
@@ -675,7 +695,7 @@ impl Item {
675695        let  def_id = match  self . item_id  { 
676696            // Anything but DefId *shouldn't* matter, but return a reasonable value anyway. 
677697            ItemId :: Auto  {  .. }  | ItemId :: Blanket  {  .. }  => return  None , 
678-             ItemId :: DefId ( def_id)  => def_id, 
698+             ItemId :: DefId ( def_id,  _ )  => def_id, 
679699        } ; 
680700
681701        match  * self . kind  { 
@@ -714,17 +734,13 @@ impl Item {
714734        Some ( tcx. visibility ( def_id) ) 
715735    } 
716736
717-     pub ( crate )  fn  attributes ( 
718-         & self , 
719-         tcx :  TyCtxt < ' _ > , 
720-         cache :  & Cache , 
721-         keep_as_is :  bool , 
722-     )  -> Vec < String >  { 
737+     pub ( crate )  fn  attributes ( & self ,  cache :  & Cache < ' _ > ,  keep_as_is :  bool )  -> Vec < String >  { 
723738        const  ALLOWED_ATTRIBUTES :  & [ Symbol ]  =
724739            & [ sym:: export_name,  sym:: link_section,  sym:: no_mangle,  sym:: non_exhaustive] ; 
725740
726741        use  rustc_abi:: IntegerType ; 
727742
743+         let  tcx = cache. tcx ; 
728744        let  mut  attrs:  Vec < String >  = self 
729745            . attrs 
730746            . other_attrs 
@@ -1113,6 +1129,8 @@ pub(crate) struct ItemLink {
11131129/// linked to. This will be different to `item_id` on item's that don't 
11141130/// have their own page, such as struct fields and enum variants. 
11151131pub ( crate )  page_id :  DefId , 
1132+     /// The type of the item pointed to by this link 
1133+ pub ( crate )  item_type :  ItemType , 
11161134    /// The url fragment to append to the link 
11171135pub ( crate )  fragment :  Option < UrlFragment > , 
11181136} 
@@ -1532,7 +1550,7 @@ impl Type {
15321550/// 
15331551/// An owned type is also the same as its borrowed variants (this is commutative), 
15341552/// but `&T` is not the same as `&mut T`. 
1535- pub ( crate )  fn  is_doc_subtype_of ( & self ,  other :  & Self ,  cache :  & Cache )  -> bool  { 
1553+ pub ( crate )  fn  is_doc_subtype_of ( & self ,  other :  & Self ,  cache :  & Cache < ' _ > )  -> bool  { 
15361554        // Strip the references so that it can compare the actual types, unless both are references. 
15371555        // If both are references, leave them alone and compare the mutabilities later. 
15381556        let  ( self_cleared,  other_cleared)  = if  !self . is_borrowed_ref ( )  || !other. is_borrowed_ref ( )  { 
@@ -1668,7 +1686,7 @@ impl Type {
16681686    /// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s. 
16691687/// 
16701688/// [clean]: crate::clean 
1671- pub ( crate )  fn  def_id ( & self ,  cache :  & Cache )  -> Option < DefId >  { 
1689+ pub ( crate )  fn  def_id ( & self ,  cache :  & Cache < ' _ > )  -> Option < DefId >  { 
16721690        let  t:  PrimitiveType  = match  * self  { 
16731691            Type :: Path  {  ref  path }  => return  Some ( path. def_id ( ) ) , 
16741692            DynTrait ( ref  bounds,  _)  => return  bounds. first ( ) . map ( |b| b. trait_ . def_id ( ) ) , 
@@ -1692,6 +1710,10 @@ impl Type {
16921710        } ; 
16931711        Primitive ( t) . def_id ( cache) 
16941712    } 
1713+ 
1714+     pub ( crate )  fn  item_id ( & self ,  cache :  & Cache < ' _ > )  -> Option < ItemId >  { 
1715+         self . def_id ( cache) . map ( ItemId :: type_ns) 
1716+     } 
16951717} 
16961718
16971719#[ derive( Clone ,  PartialEq ,  Eq ,  Debug ,  Hash ) ]  
@@ -2164,6 +2186,10 @@ pub(crate) struct Path {
21642186} 
21652187
21662188impl  Path  { 
2189+     pub ( crate )  fn  item_id ( & self )  -> ItemId  { 
2190+         ItemId :: from_res ( & self . res ) 
2191+     } 
2192+ 
21672193    pub ( crate )  fn  def_id ( & self )  -> DefId  { 
21682194        self . res . def_id ( ) 
21692195    } 
0 commit comments