@@ -36,15 +36,11 @@ use crate::formats::item_type::ItemType;
36
36
///
37
37
/// The returned value is `None` if the definition could not be inlined,
38
38
/// and `Some` of a vector of items if it was successfully expanded.
39
- ///
40
- /// `parent_module` refers to the parent of the *re-export*, not the original item.
41
39
pub ( crate ) fn try_inline (
42
40
cx : & mut DocContext < ' _ > ,
43
- parent_module : DefId ,
44
- import_def_id : Option < DefId > ,
45
41
res : Res ,
46
42
name : Symbol ,
47
- attrs : Option < & [ ast:: Attribute ] > ,
43
+ attrs : Option < ( & [ ast:: Attribute ] , Option < DefId > ) > ,
48
44
visited : & mut DefIdSet ,
49
45
) -> Option < Vec < clean:: Item > > {
50
46
let did = res. opt_def_id ( ) ?;
@@ -55,38 +51,17 @@ pub(crate) fn try_inline(
55
51
56
52
debug ! ( "attrs={:?}" , attrs) ;
57
53
58
- let attrs_without_docs = attrs. map ( |attrs| {
59
- attrs. into_iter ( ) . filter ( |a| a. doc_str ( ) . is_none ( ) ) . cloned ( ) . collect :: < Vec < _ > > ( )
54
+ let attrs_without_docs = attrs. map ( |( attrs, def_id ) | {
55
+ ( attrs. into_iter ( ) . filter ( |a| a. doc_str ( ) . is_none ( ) ) . cloned ( ) . collect :: < Vec < _ > > ( ) , def_id )
60
56
} ) ;
61
- // We need this ugly code because:
62
- //
63
- // ```
64
- // attrs_without_docs.map(|a| a.as_slice())
65
- // ```
66
- //
67
- // will fail because it returns a temporary slice and:
68
- //
69
- // ```
70
- // attrs_without_docs.map(|s| {
71
- // vec = s.as_slice();
72
- // vec
73
- // })
74
- // ```
75
- //
76
- // will fail because we're moving an uninitialized variable into a closure.
77
- let vec;
78
- let attrs_without_docs = match attrs_without_docs {
79
- Some ( s) => {
80
- vec = s;
81
- Some ( vec. as_slice ( ) )
82
- }
83
- None => None ,
84
- } ;
57
+ let attrs_without_docs =
58
+ attrs_without_docs. as_ref ( ) . map ( |( attrs, def_id) | ( & attrs[ ..] , * def_id) ) ;
85
59
60
+ let import_def_id = attrs. and_then ( |( _, def_id) | def_id) ;
86
61
let kind = match res {
87
62
Res :: Def ( DefKind :: Trait , did) => {
88
63
record_extern_fqn ( cx, did, ItemType :: Trait ) ;
89
- build_impls ( cx, Some ( parent_module ) , did, attrs_without_docs, & mut ret) ;
64
+ build_impls ( cx, did, attrs_without_docs, & mut ret) ;
90
65
clean:: TraitItem ( Box :: new ( build_external_trait ( cx, did) ) )
91
66
}
92
67
Res :: Def ( DefKind :: Fn , did) => {
@@ -95,27 +70,27 @@ pub(crate) fn try_inline(
95
70
}
96
71
Res :: Def ( DefKind :: Struct , did) => {
97
72
record_extern_fqn ( cx, did, ItemType :: Struct ) ;
98
- build_impls ( cx, Some ( parent_module ) , did, attrs_without_docs, & mut ret) ;
73
+ build_impls ( cx, did, attrs_without_docs, & mut ret) ;
99
74
clean:: StructItem ( build_struct ( cx, did) )
100
75
}
101
76
Res :: Def ( DefKind :: Union , did) => {
102
77
record_extern_fqn ( cx, did, ItemType :: Union ) ;
103
- build_impls ( cx, Some ( parent_module ) , did, attrs_without_docs, & mut ret) ;
78
+ build_impls ( cx, did, attrs_without_docs, & mut ret) ;
104
79
clean:: UnionItem ( build_union ( cx, did) )
105
80
}
106
81
Res :: Def ( DefKind :: TyAlias , did) => {
107
82
record_extern_fqn ( cx, did, ItemType :: Typedef ) ;
108
- build_impls ( cx, Some ( parent_module ) , did, attrs_without_docs, & mut ret) ;
83
+ build_impls ( cx, did, attrs_without_docs, & mut ret) ;
109
84
clean:: TypedefItem ( build_type_alias ( cx, did) )
110
85
}
111
86
Res :: Def ( DefKind :: Enum , did) => {
112
87
record_extern_fqn ( cx, did, ItemType :: Enum ) ;
113
- build_impls ( cx, Some ( parent_module ) , did, attrs_without_docs, & mut ret) ;
88
+ build_impls ( cx, did, attrs_without_docs, & mut ret) ;
114
89
clean:: EnumItem ( build_enum ( cx, did) )
115
90
}
116
91
Res :: Def ( DefKind :: ForeignTy , did) => {
117
92
record_extern_fqn ( cx, did, ItemType :: ForeignType ) ;
118
- build_impls ( cx, Some ( parent_module ) , did, attrs_without_docs, & mut ret) ;
93
+ build_impls ( cx, did, attrs_without_docs, & mut ret) ;
119
94
clean:: ForeignTypeItem
120
95
}
121
96
// Never inline enum variants but leave them shown as re-exports.
@@ -149,7 +124,7 @@ pub(crate) fn try_inline(
149
124
_ => return None ,
150
125
} ;
151
126
152
- let ( attrs, cfg) = merge_attrs ( cx, Some ( parent_module ) , load_attrs ( cx, did) , attrs) ;
127
+ let ( attrs, cfg) = merge_attrs ( cx, load_attrs ( cx, did) , attrs) ;
153
128
cx. inlined . insert ( did. into ( ) ) ;
154
129
let mut item =
155
130
clean:: Item :: from_def_id_and_attrs_and_parts ( did, Some ( name) , kind, Box :: new ( attrs) , cfg) ;
@@ -316,17 +291,16 @@ fn build_type_alias(cx: &mut DocContext<'_>, did: DefId) -> Box<clean::Typedef>
316
291
/// Builds all inherent implementations of an ADT (struct/union/enum) or Trait item/path/reexport.
317
292
pub ( crate ) fn build_impls (
318
293
cx : & mut DocContext < ' _ > ,
319
- parent_module : Option < DefId > ,
320
294
did : DefId ,
321
- attrs : Option < & [ ast:: Attribute ] > ,
295
+ attrs : Option < ( & [ ast:: Attribute ] , Option < DefId > ) > ,
322
296
ret : & mut Vec < clean:: Item > ,
323
297
) {
324
298
let _prof_timer = cx. tcx . sess . prof . generic_activity ( "build_inherent_impls" ) ;
325
299
let tcx = cx. tcx ;
326
300
327
301
// for each implementation of an item represented by `did`, build the clean::Item for that impl
328
302
for & did in tcx. inherent_impls ( did) . iter ( ) {
329
- build_impl ( cx, parent_module , did, attrs, ret) ;
303
+ build_impl ( cx, did, attrs, ret) ;
330
304
}
331
305
332
306
// This pretty much exists expressly for `dyn Error` traits that exist in the `alloc` crate.
@@ -340,28 +314,26 @@ pub(crate) fn build_impls(
340
314
let type_ =
341
315
if tcx. is_trait ( did) { TraitSimplifiedType ( did) } else { AdtSimplifiedType ( did) } ;
342
316
for & did in tcx. incoherent_impls ( type_) {
343
- build_impl ( cx, parent_module , did, attrs, ret) ;
317
+ build_impl ( cx, did, attrs, ret) ;
344
318
}
345
319
}
346
320
}
347
321
348
- /// `parent_module` refers to the parent of the re-export, not the original item
349
322
pub ( crate ) fn merge_attrs (
350
323
cx : & mut DocContext < ' _ > ,
351
- parent_module : Option < DefId > ,
352
324
old_attrs : & [ ast:: Attribute ] ,
353
- new_attrs : Option < & [ ast:: Attribute ] > ,
325
+ new_attrs : Option < ( & [ ast:: Attribute ] , Option < DefId > ) > ,
354
326
) -> ( clean:: Attributes , Option < Arc < clean:: cfg:: Cfg > > ) {
355
327
// NOTE: If we have additional attributes (from a re-export),
356
328
// always insert them first. This ensure that re-export
357
329
// doc comments show up before the original doc comments
358
330
// when we render them.
359
- if let Some ( inner) = new_attrs {
331
+ if let Some ( ( inner, item_id ) ) = new_attrs {
360
332
let mut both = inner. to_vec ( ) ;
361
333
both. extend_from_slice ( old_attrs) ;
362
334
(
363
- if let Some ( new_id ) = parent_module {
364
- Attributes :: from_ast_with_additional ( old_attrs, ( inner, new_id ) )
335
+ if let Some ( item_id ) = item_id {
336
+ Attributes :: from_ast_with_additional ( old_attrs, ( inner, item_id ) )
365
337
} else {
366
338
Attributes :: from_ast ( & both)
367
339
} ,
@@ -375,9 +347,8 @@ pub(crate) fn merge_attrs(
375
347
/// Inline an `impl`, inherent or of a trait. The `did` must be for an `impl`.
376
348
pub ( crate ) fn build_impl (
377
349
cx : & mut DocContext < ' _ > ,
378
- parent_module : Option < DefId > ,
379
350
did : DefId ,
380
- attrs : Option < & [ ast:: Attribute ] > ,
351
+ attrs : Option < ( & [ ast:: Attribute ] , Option < DefId > ) > ,
381
352
ret : & mut Vec < clean:: Item > ,
382
353
) {
383
354
if !cx. inlined . insert ( did. into ( ) ) {
@@ -539,7 +510,7 @@ pub(crate) fn build_impl(
539
510
record_extern_trait ( cx, did) ;
540
511
}
541
512
542
- let ( merged_attrs, cfg) = merge_attrs ( cx, parent_module , load_attrs ( cx, did) , attrs) ;
513
+ let ( merged_attrs, cfg) = merge_attrs ( cx, load_attrs ( cx, did) , attrs) ;
543
514
trace ! ( "merged_attrs={:?}" , merged_attrs) ;
544
515
545
516
trace ! (
@@ -635,7 +606,7 @@ fn build_module_items(
635
606
cfg : None ,
636
607
inline_stmt_id : None ,
637
608
} ) ;
638
- } else if let Some ( i) = try_inline ( cx, did , None , res, item. ident . name , None , visited) {
609
+ } else if let Some ( i) = try_inline ( cx, res, item. ident . name , None , visited) {
639
610
items. extend ( i)
640
611
}
641
612
}
0 commit comments