@@ -11,7 +11,7 @@ use rustc_ast_pretty::pprust;
11
11
use rustc_attr:: { ConstStability , Deprecation , Stability , StabilityLevel , StableSince } ;
12
12
use rustc_const_eval:: const_eval:: is_unstable_const_fn;
13
13
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
14
- use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
14
+ use rustc_hir:: def:: { CtorKind , DefKind , Namespace , Res } ;
15
15
use rustc_hir:: def_id:: { CrateNum , DefId , LocalDefId , LOCAL_CRATE } ;
16
16
use rustc_hir:: lang_items:: LangItem ;
17
17
use rustc_hir:: { BodyId , Mutability } ;
@@ -57,20 +57,34 @@ pub(crate) type ItemIdSet = FxHashSet<ItemId>;
57
57
#[ derive( Debug , Clone , PartialEq , Eq , Hash , Copy ) ]
58
58
pub ( crate ) enum ItemId {
59
59
/// A "normal" item that uses a [`DefId`] for identification.
60
- DefId ( DefId ) ,
60
+ DefId ( DefId , Option < Namespace > ) ,
61
61
/// Identifier that is used for auto traits.
62
62
Auto { trait_ : DefId , for_ : DefId } ,
63
63
/// Identifier that is used for blanket implementations.
64
64
Blanket { impl_id : DefId , for_ : DefId } ,
65
65
}
66
66
67
67
impl 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
+
68
82
#[ inline]
69
83
pub ( crate ) fn is_local ( self ) -> bool {
70
84
match self {
71
85
ItemId :: Auto { for_ : id, .. }
72
86
| ItemId :: Blanket { for_ : id, .. }
73
- | ItemId :: DefId ( id) => id. is_local ( ) ,
87
+ | ItemId :: DefId ( id, _ ) => id. is_local ( ) ,
74
88
}
75
89
}
76
90
@@ -84,7 +98,7 @@ impl ItemId {
84
98
#[ inline]
85
99
pub ( crate ) fn as_def_id ( self ) -> Option < DefId > {
86
100
match self {
87
- ItemId :: DefId ( id) => Some ( id) ,
101
+ ItemId :: DefId ( id, _ ) => Some ( id) ,
88
102
_ => None ,
89
103
}
90
104
}
@@ -99,17 +113,11 @@ impl ItemId {
99
113
match self {
100
114
ItemId :: Auto { for_ : id, .. }
101
115
| ItemId :: Blanket { for_ : id, .. }
102
- | ItemId :: DefId ( id) => id. krate ,
116
+ | ItemId :: DefId ( id, _ ) => id. krate ,
103
117
}
104
118
}
105
119
}
106
120
107
- impl From < DefId > for ItemId {
108
- fn from ( id : DefId ) -> Self {
109
- Self :: DefId ( id)
110
- }
111
- }
112
-
113
121
/// The crate currently being documented.
114
122
#[ derive( Clone , Debug ) ]
115
123
pub ( crate ) struct Crate {
@@ -136,6 +144,11 @@ pub(crate) struct ExternalCrate {
136
144
impl ExternalCrate {
137
145
const LOCAL : Self = Self { crate_num : LOCAL_CRATE } ;
138
146
147
+ #[ inline]
148
+ pub ( crate ) fn item_id ( & self ) -> ItemId {
149
+ ItemId :: type_ns ( self . crate_num . as_def_id ( ) )
150
+ }
151
+
139
152
#[ inline]
140
153
pub ( crate ) fn def_id ( & self ) -> DefId {
141
154
self . crate_num . as_def_id ( )
@@ -450,8 +463,15 @@ impl Item {
450
463
) -> Item {
451
464
trace ! ( "name={name:?}, def_id={def_id:?} cfg={cfg:?}" ) ;
452
465
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
+ } ;
453
473
Item {
454
- item_id : def_id . into ( ) ,
474
+ item_id : ItemId :: DefId ( def_id , ns ) ,
455
475
kind : Box :: new ( kind) ,
456
476
name,
457
477
attrs,
@@ -466,7 +486,7 @@ impl Item {
466
486
let Some ( links) = cx. cache ( ) . intra_doc_links . get ( & self . item_id ) else { return vec ! [ ] } ;
467
487
links
468
488
. 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, .. } | {
470
490
debug ! ( ?id) ;
471
491
if let Ok ( ( mut href, ..) ) = href ( * id, cx) {
472
492
debug ! ( ?href) ;
@@ -491,7 +511,7 @@ impl Item {
491
511
/// This is used for generating summary text, which does not include
492
512
/// the link text, but does need to know which `[]`-bracketed names
493
513
/// 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 > {
495
515
let Some ( links) = cache. intra_doc_links . get ( & self . item_id ) else {
496
516
return vec ! [ ] ;
497
517
} ;
@@ -675,7 +695,7 @@ impl Item {
675
695
let def_id = match self . item_id {
676
696
// Anything but DefId *shouldn't* matter, but return a reasonable value anyway.
677
697
ItemId :: Auto { .. } | ItemId :: Blanket { .. } => return None ,
678
- ItemId :: DefId ( def_id) => def_id,
698
+ ItemId :: DefId ( def_id, _ ) => def_id,
679
699
} ;
680
700
681
701
match * self . kind {
@@ -714,17 +734,13 @@ impl Item {
714
734
Some ( tcx. visibility ( def_id) )
715
735
}
716
736
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 > {
723
738
const ALLOWED_ATTRIBUTES : & [ Symbol ] =
724
739
& [ sym:: export_name, sym:: link_section, sym:: no_mangle, sym:: non_exhaustive] ;
725
740
726
741
use rustc_abi:: IntegerType ;
727
742
743
+ let tcx = cache. tcx ;
728
744
let mut attrs: Vec < String > = self
729
745
. attrs
730
746
. other_attrs
@@ -1113,6 +1129,8 @@ pub(crate) struct ItemLink {
1113
1129
/// linked to. This will be different to `item_id` on item's that don't
1114
1130
/// have their own page, such as struct fields and enum variants.
1115
1131
pub ( crate ) page_id : DefId ,
1132
+ /// The type of the item pointed to by this link
1133
+ pub ( crate ) item_type : ItemType ,
1116
1134
/// The url fragment to append to the link
1117
1135
pub ( crate ) fragment : Option < UrlFragment > ,
1118
1136
}
@@ -1532,7 +1550,7 @@ impl Type {
1532
1550
///
1533
1551
/// An owned type is also the same as its borrowed variants (this is commutative),
1534
1552
/// 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 {
1536
1554
// Strip the references so that it can compare the actual types, unless both are references.
1537
1555
// If both are references, leave them alone and compare the mutabilities later.
1538
1556
let ( self_cleared, other_cleared) = if !self . is_borrowed_ref ( ) || !other. is_borrowed_ref ( ) {
@@ -1668,7 +1686,7 @@ impl Type {
1668
1686
/// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s.
1669
1687
///
1670
1688
/// [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 > {
1672
1690
let t: PrimitiveType = match * self {
1673
1691
Type :: Path { ref path } => return Some ( path. def_id ( ) ) ,
1674
1692
DynTrait ( ref bounds, _) => return bounds. first ( ) . map ( |b| b. trait_ . def_id ( ) ) ,
@@ -1692,6 +1710,10 @@ impl Type {
1692
1710
} ;
1693
1711
Primitive ( t) . def_id ( cache)
1694
1712
}
1713
+
1714
+ pub ( crate ) fn item_id ( & self , cache : & Cache < ' _ > ) -> Option < ItemId > {
1715
+ self . def_id ( cache) . map ( ItemId :: type_ns)
1716
+ }
1695
1717
}
1696
1718
1697
1719
#[ derive( Clone , PartialEq , Eq , Debug , Hash ) ]
@@ -2164,6 +2186,10 @@ pub(crate) struct Path {
2164
2186
}
2165
2187
2166
2188
impl Path {
2189
+ pub ( crate ) fn item_id ( & self ) -> ItemId {
2190
+ ItemId :: from_res ( & self . res )
2191
+ }
2192
+
2167
2193
pub ( crate ) fn def_id ( & self ) -> DefId {
2168
2194
self . res . def_id ( )
2169
2195
}
0 commit comments