@@ -14,7 +14,7 @@ use rustc_attr as attr;
14
14
use rustc_data_structures:: fx:: { FxHashMap , FxHashSet } ;
15
15
use rustc_hir as hir;
16
16
use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
17
- use rustc_hir:: def_id:: { CrateNum , DefId , CRATE_DEF_INDEX } ;
17
+ use rustc_hir:: def_id:: { CrateNum , DefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
18
18
use rustc_index:: vec:: { Idx , IndexVec } ;
19
19
use rustc_infer:: infer:: region_constraints:: { Constraint , RegionConstraintData } ;
20
20
use rustc_middle:: bug;
@@ -229,15 +229,11 @@ impl Clean<Item> for doctree::Module<'_> {
229
229
let attrs = self . attrs . clean ( cx) ;
230
230
231
231
let mut items: Vec < Item > = vec ! [ ] ;
232
- items. extend ( self . extern_crates . iter ( ) . flat_map ( |x| x. clean ( cx) ) ) ;
233
232
items. extend ( self . imports . iter ( ) . flat_map ( |x| x. clean ( cx) ) ) ;
234
- items. extend ( self . fns . iter ( ) . map ( |x| x. clean ( cx) ) ) ;
235
233
items. extend ( self . foreigns . iter ( ) . map ( |x| x. clean ( cx) ) ) ;
236
234
items. extend ( self . mods . iter ( ) . map ( |x| x. clean ( cx) ) ) ;
237
235
items. extend ( self . items . iter ( ) . map ( |x| x. clean ( cx) ) . flatten ( ) ) ;
238
- items. extend ( self . traits . iter ( ) . map ( |x| x. clean ( cx) ) ) ;
239
236
items. extend ( self . macros . iter ( ) . map ( |x| x. clean ( cx) ) ) ;
240
- items. extend ( self . proc_macros . iter ( ) . map ( |x| x. clean ( cx) ) ) ;
241
237
242
238
// determine if we should display the inner contents or
243
239
// the outer `mod` item for the source code.
@@ -871,6 +867,66 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
871
867
}
872
868
}
873
869
870
+ fn clean_fn_or_proc_macro (
871
+ item : & hir:: Item < ' _ > ,
872
+ sig : & ' a hir:: FnSig < ' a > ,
873
+ generics : & ' a hir:: Generics < ' a > ,
874
+ body_id : hir:: BodyId ,
875
+ name : & mut Symbol ,
876
+ cx : & DocContext < ' _ > ,
877
+ ) -> ItemKind {
878
+ let macro_kind = item. attrs . iter ( ) . find_map ( |a| {
879
+ if a. has_name ( sym:: proc_macro) {
880
+ Some ( MacroKind :: Bang )
881
+ } else if a. has_name ( sym:: proc_macro_derive) {
882
+ Some ( MacroKind :: Derive )
883
+ } else if a. has_name ( sym:: proc_macro_attribute) {
884
+ Some ( MacroKind :: Attr )
885
+ } else {
886
+ None
887
+ }
888
+ } ) ;
889
+ match macro_kind {
890
+ Some ( kind) => {
891
+ if kind == MacroKind :: Derive {
892
+ * name = item
893
+ . attrs
894
+ . lists ( sym:: proc_macro_derive)
895
+ . find_map ( |mi| mi. ident ( ) )
896
+ . expect ( "proc-macro derives require a name" )
897
+ . name ;
898
+ }
899
+
900
+ let mut helpers = Vec :: new ( ) ;
901
+ for mi in item. attrs . lists ( sym:: proc_macro_derive) {
902
+ if !mi. has_name ( sym:: attributes) {
903
+ continue ;
904
+ }
905
+
906
+ if let Some ( list) = mi. meta_item_list ( ) {
907
+ for inner_mi in list {
908
+ if let Some ( ident) = inner_mi. ident ( ) {
909
+ helpers. push ( ident. name ) ;
910
+ }
911
+ }
912
+ }
913
+ }
914
+ ProcMacroItem ( ProcMacro { kind, helpers : helpers. clean ( cx) } )
915
+ }
916
+ None => {
917
+ let mut func = ( sig, generics, body_id) . clean ( cx) ;
918
+ let def_id = cx. tcx . hir ( ) . local_def_id ( item. hir_id ) . to_def_id ( ) ;
919
+ func. header . constness =
920
+ if is_const_fn ( cx. tcx , def_id) && is_unstable_const_fn ( cx. tcx , def_id) . is_none ( ) {
921
+ hir:: Constness :: Const
922
+ } else {
923
+ hir:: Constness :: NotConst
924
+ } ;
925
+ FunctionItem ( func)
926
+ }
927
+ }
928
+ }
929
+
874
930
impl < ' a > Clean < Function > for ( & ' a hir:: FnSig < ' a > , & ' a hir:: Generics < ' a > , hir:: BodyId ) {
875
931
fn clean ( & self , cx : & DocContext < ' _ > ) -> Function {
876
932
let ( generics, decl) =
@@ -880,34 +936,6 @@ impl<'a> Clean<Function> for (&'a hir::FnSig<'a>, &'a hir::Generics<'a>, hir::Bo
880
936
}
881
937
}
882
938
883
- impl Clean < Item > for doctree:: Function < ' _ > {
884
- fn clean ( & self , cx : & DocContext < ' _ > ) -> Item {
885
- let ( generics, decl) =
886
- enter_impl_trait ( cx, || ( self . generics . clean ( cx) , ( self . decl , self . body ) . clean ( cx) ) ) ;
887
-
888
- let did = cx. tcx . hir ( ) . local_def_id ( self . id ) . to_def_id ( ) ;
889
- let constness = if is_const_fn ( cx. tcx , did) && !is_unstable_const_fn ( cx. tcx , did) . is_some ( )
890
- {
891
- hir:: Constness :: Const
892
- } else {
893
- hir:: Constness :: NotConst
894
- } ;
895
- let ( all_types, ret_types) = get_all_types ( & generics, & decl, cx) ;
896
- Item :: from_def_id_and_parts (
897
- did,
898
- Some ( self . name ) ,
899
- FunctionItem ( Function {
900
- decl,
901
- generics,
902
- header : hir:: FnHeader { constness, ..self . header } ,
903
- all_types,
904
- ret_types,
905
- } ) ,
906
- cx,
907
- )
908
- }
909
- }
910
-
911
939
impl < ' a > Clean < Arguments > for ( & ' a [ hir:: Ty < ' a > ] , & ' a [ Ident ] ) {
912
940
fn clean ( & self , cx : & DocContext < ' _ > ) -> Arguments {
913
941
Arguments {
@@ -992,26 +1020,6 @@ impl Clean<FnRetTy> for hir::FnRetTy<'_> {
992
1020
}
993
1021
}
994
1022
995
- impl Clean < Item > for doctree:: Trait < ' _ > {
996
- fn clean ( & self , cx : & DocContext < ' _ > ) -> Item {
997
- let attrs = self . attrs . clean ( cx) ;
998
- let is_spotlight = attrs. has_doc_flag ( sym:: spotlight) ;
999
- Item :: from_hir_id_and_parts (
1000
- self . id ,
1001
- Some ( self . name ) ,
1002
- TraitItem ( Trait {
1003
- unsafety : self . unsafety ,
1004
- items : self . items . iter ( ) . map ( |ti| ti. clean ( cx) ) . collect ( ) ,
1005
- generics : self . generics . clean ( cx) ,
1006
- bounds : self . bounds . clean ( cx) ,
1007
- is_spotlight,
1008
- is_auto : self . is_auto . clean ( cx) ,
1009
- } ) ,
1010
- cx,
1011
- )
1012
- }
1013
- }
1014
-
1015
1023
impl Clean < bool > for hir:: IsAuto {
1016
1024
fn clean ( & self , _: & DocContext < ' _ > ) -> bool {
1017
1025
match * self {
@@ -1927,7 +1935,7 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
1927
1935
1928
1936
let ( item, renamed) = self ;
1929
1937
let def_id = cx. tcx . hir ( ) . local_def_id ( item. hir_id ) . to_def_id ( ) ;
1930
- let name = match renamed {
1938
+ let mut name = match renamed {
1931
1939
Some ( ident) => ident. name ,
1932
1940
None => cx. tcx . hir ( ) . name ( item. hir_id ) ,
1933
1941
} ;
@@ -1977,6 +1985,27 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
1977
1985
fields_stripped : false ,
1978
1986
} ) ,
1979
1987
ItemKind :: Impl { .. } => return clean_impl ( item, cx) ,
1988
+ // proc macros can have a name set by attributes
1989
+ ItemKind :: Fn ( ref sig, ref generics, body_id) => {
1990
+ clean_fn_or_proc_macro ( item, sig, generics, body_id, & mut name, cx)
1991
+ }
1992
+ hir:: ItemKind :: Trait ( is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
1993
+ let items =
1994
+ item_ids. iter ( ) . map ( |ti| cx. tcx . hir ( ) . trait_item ( ti. id ) . clean ( cx) ) . collect ( ) ;
1995
+ let attrs = item. attrs . clean ( cx) ;
1996
+ let is_spotlight = attrs. has_doc_flag ( sym:: spotlight) ;
1997
+ TraitItem ( Trait {
1998
+ unsafety,
1999
+ items,
2000
+ generics : generics. clean ( cx) ,
2001
+ bounds : bounds. clean ( cx) ,
2002
+ is_spotlight,
2003
+ is_auto : is_auto. clean ( cx) ,
2004
+ } )
2005
+ }
2006
+ ItemKind :: ExternCrate ( orig_name) => {
2007
+ return clean_extern_crate ( item, name, orig_name, cx) ;
2008
+ }
1980
2009
_ => unreachable ! ( "not yet converted" ) ,
1981
2010
} ;
1982
2011
@@ -2054,45 +2083,54 @@ fn clean_impl(impl_: &hir::Item<'_>, cx: &DocContext<'_>) -> Vec<Item> {
2054
2083
ret
2055
2084
}
2056
2085
2057
- impl Clean < Vec < Item > > for doctree:: ExternCrate < ' _ > {
2058
- fn clean ( & self , cx : & DocContext < ' _ > ) -> Vec < Item > {
2059
- let please_inline = self . vis . node . is_pub ( )
2060
- && self . attrs . iter ( ) . any ( |a| {
2061
- a. has_name ( sym:: doc)
2062
- && match a. meta_item_list ( ) {
2063
- Some ( l) => attr:: list_contains_name ( & l, sym:: inline) ,
2064
- None => false ,
2065
- }
2066
- } ) ;
2086
+ fn clean_extern_crate (
2087
+ krate : & hir:: Item < ' _ > ,
2088
+ name : Symbol ,
2089
+ orig_name : Option < Symbol > ,
2090
+ cx : & DocContext < ' _ > ,
2091
+ ) -> Vec < Item > {
2092
+ // this is the ID of the `extern crate` statement
2093
+ let def_id = cx. tcx . hir ( ) . local_def_id ( krate. hir_id ) ;
2094
+ let cnum = cx. tcx . extern_mod_stmt_cnum ( def_id) . unwrap_or ( LOCAL_CRATE ) ;
2095
+ // this is the ID of the crate itself
2096
+ let crate_def_id = DefId { krate : cnum, index : CRATE_DEF_INDEX } ;
2097
+ let please_inline = krate. vis . node . is_pub ( )
2098
+ && krate. attrs . iter ( ) . any ( |a| {
2099
+ a. has_name ( sym:: doc)
2100
+ && match a. meta_item_list ( ) {
2101
+ Some ( l) => attr:: list_contains_name ( & l, sym:: inline) ,
2102
+ None => false ,
2103
+ }
2104
+ } ) ;
2067
2105
2068
- if please_inline {
2069
- let mut visited = FxHashSet :: default ( ) ;
2106
+ if please_inline {
2107
+ let mut visited = FxHashSet :: default ( ) ;
2070
2108
2071
- let res = Res :: Def ( DefKind :: Mod , DefId { krate : self . cnum , index : CRATE_DEF_INDEX } ) ;
2109
+ let res = Res :: Def ( DefKind :: Mod , crate_def_id ) ;
2072
2110
2073
- if let Some ( items) = inline:: try_inline (
2074
- cx,
2075
- cx. tcx . parent_module ( self . hir_id ) . to_def_id ( ) ,
2076
- res,
2077
- self . name ,
2078
- Some ( self . attrs ) ,
2079
- & mut visited,
2080
- ) {
2081
- return items;
2082
- }
2111
+ if let Some ( items) = inline:: try_inline (
2112
+ cx,
2113
+ cx. tcx . parent_module ( krate. hir_id ) . to_def_id ( ) ,
2114
+ res,
2115
+ name,
2116
+ Some ( krate. attrs ) ,
2117
+ & mut visited,
2118
+ ) {
2119
+ return items;
2083
2120
}
2084
-
2085
- vec ! [ Item {
2086
- name: None ,
2087
- attrs: self . attrs. clean( cx) ,
2088
- source: self . span. clean( cx) ,
2089
- def_id: DefId { krate: self . cnum, index: CRATE_DEF_INDEX } ,
2090
- visibility: self . vis. clean( cx) ,
2091
- stability: None ,
2092
- deprecation: None ,
2093
- kind: ExternCrateItem ( self . name. clean( cx) , self . path. clone( ) ) ,
2094
- } ]
2095
2121
}
2122
+ let path = orig_name. map ( |x| x. to_string ( ) ) ;
2123
+ // FIXME: using `from_def_id_and_kind` breaks `rustdoc/masked` for some reason
2124
+ vec ! [ Item {
2125
+ name: None ,
2126
+ attrs: krate. attrs. clean( cx) ,
2127
+ source: krate. span. clean( cx) ,
2128
+ def_id: crate_def_id,
2129
+ visibility: krate. vis. clean( cx) ,
2130
+ stability: None ,
2131
+ deprecation: None ,
2132
+ kind: ExternCrateItem ( name. clean( cx) , path) ,
2133
+ } ]
2096
2134
}
2097
2135
2098
2136
impl Clean < Vec < Item > > for doctree:: Import < ' _ > {
@@ -2186,11 +2224,12 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
2186
2224
}
2187
2225
}
2188
2226
2189
- impl Clean < Item > for doctree :: ForeignItem < ' _ > {
2227
+ impl Clean < Item > for ( & hir :: ForeignItem < ' _ > , Option < Ident > ) {
2190
2228
fn clean ( & self , cx : & DocContext < ' _ > ) -> Item {
2191
- let kind = match self . kind {
2229
+ let ( item, renamed) = self ;
2230
+ let kind = match item. kind {
2192
2231
hir:: ForeignItemKind :: Fn ( ref decl, ref names, ref generics) => {
2193
- let abi = cx. tcx . hir ( ) . get_foreign_abi ( self . id ) ;
2232
+ let abi = cx. tcx . hir ( ) . get_foreign_abi ( item . hir_id ) ;
2194
2233
let ( generics, decl) =
2195
2234
enter_impl_trait ( cx, || ( generics. clean ( cx) , ( & * * decl, & names[ ..] ) . clean ( cx) ) ) ;
2196
2235
let ( all_types, ret_types) = get_all_types ( & generics, & decl, cx) ;
@@ -2207,15 +2246,13 @@ impl Clean<Item> for doctree::ForeignItem<'_> {
2207
2246
ret_types,
2208
2247
} )
2209
2248
}
2210
- hir:: ForeignItemKind :: Static ( ref ty, mutbl) => ForeignStaticItem ( Static {
2211
- type_ : ty. clean ( cx) ,
2212
- mutability : * mutbl,
2213
- expr : String :: new ( ) ,
2214
- } ) ,
2249
+ hir:: ForeignItemKind :: Static ( ref ty, mutability) => {
2250
+ ForeignStaticItem ( Static { type_ : ty. clean ( cx) , mutability, expr : String :: new ( ) } )
2251
+ }
2215
2252
hir:: ForeignItemKind :: Type => ForeignTypeItem ,
2216
2253
} ;
2217
2254
2218
- Item :: from_hir_id_and_parts ( self . id , Some ( self . name ) , kind, cx)
2255
+ Item :: from_hir_id_and_parts ( item . hir_id , Some ( renamed . unwrap_or ( item . ident ) . name ) , kind, cx)
2219
2256
}
2220
2257
}
2221
2258
@@ -2240,17 +2277,6 @@ impl Clean<Item> for doctree::Macro {
2240
2277
}
2241
2278
}
2242
2279
2243
- impl Clean < Item > for doctree:: ProcMacro {
2244
- fn clean ( & self , cx : & DocContext < ' _ > ) -> Item {
2245
- Item :: from_hir_id_and_parts (
2246
- self . id ,
2247
- Some ( self . name ) ,
2248
- ProcMacroItem ( ProcMacro { kind : self . kind , helpers : self . helpers . clean ( cx) } ) ,
2249
- cx,
2250
- )
2251
- }
2252
- }
2253
-
2254
2280
impl Clean < Deprecation > for attr:: Deprecation {
2255
2281
fn clean ( & self , _: & DocContext < ' _ > ) -> Deprecation {
2256
2282
Deprecation {
0 commit comments