11//! Support for inlining external documentation into the current AST.
22
33use std:: iter:: once;
4+ use std:: sync:: Arc ;
45
56use rustc_ast as ast;
67use rustc_data_structures:: fx:: FxHashSet ;
@@ -14,7 +15,7 @@ use rustc_span::hygiene::MacroKind;
1415use rustc_span:: symbol:: { kw, sym, Symbol } ;
1516use rustc_span:: Span ;
1617
17- use crate :: clean:: { self , Attributes , GetDefId , ToSource } ;
18+ use crate :: clean:: { self , Attributes , AttributesExt , GetDefId , ToSource } ;
1819use crate :: core:: DocContext ;
1920use crate :: formats:: item_type:: ItemType ;
2021
@@ -119,12 +120,16 @@ crate fn try_inline(
119120 _ => return None ,
120121 } ;
121122
122- let target_attrs = load_attrs ( cx, did) ;
123- let attrs = box merge_attrs ( cx, Some ( parent_module) , target_attrs, attrs_clone) ;
124-
123+ let ( attrs, cfg) = merge_attrs ( cx, Some ( parent_module) , load_attrs ( cx, did) , attrs_clone) ;
125124 cx. inlined . insert ( did) ;
126- let what_rustc_thinks = clean:: Item :: from_def_id_and_parts ( did, Some ( name) , kind, cx) ;
127- ret. push ( clean:: Item { attrs, ..what_rustc_thinks } ) ;
125+ ret. push ( clean:: Item :: from_def_id_and_attrs_and_parts (
126+ did,
127+ Some ( name) ,
128+ kind,
129+ box attrs,
130+ cx,
131+ cfg,
132+ ) ) ;
128133 Some ( ret)
129134}
130135
@@ -288,22 +293,24 @@ fn merge_attrs(
288293 parent_module : Option < DefId > ,
289294 old_attrs : Attrs < ' _ > ,
290295 new_attrs : Option < Attrs < ' _ > > ,
291- ) -> clean:: Attributes {
296+ ) -> ( clean:: Attributes , Option < Arc < clean :: cfg :: Cfg > > ) {
292297 // NOTE: If we have additional attributes (from a re-export),
293298 // always insert them first. This ensure that re-export
294299 // doc comments show up before the original doc comments
295300 // when we render them.
296301 if let Some ( inner) = new_attrs {
297- if let Some ( new_id) = parent_module {
298- let diag = cx. sess ( ) . diagnostic ( ) ;
299- Attributes :: from_ast ( diag, old_attrs, Some ( ( inner, new_id) ) )
300- } else {
301- let mut both = inner. to_vec ( ) ;
302- both. extend_from_slice ( old_attrs) ;
303- both. clean ( cx)
304- }
302+ let mut both = inner. to_vec ( ) ;
303+ both. extend_from_slice ( old_attrs) ;
304+ (
305+ if let Some ( new_id) = parent_module {
306+ Attributes :: from_ast ( old_attrs, Some ( ( inner, new_id) ) )
307+ } else {
308+ Attributes :: from_ast ( & both, None )
309+ } ,
310+ both. cfg ( cx. sess ( ) . diagnostic ( ) ) ,
311+ )
305312 } else {
306- old_attrs. clean ( cx)
313+ ( old_attrs. clean ( cx) , old_attrs . cfg ( cx . sess ( ) . diagnostic ( ) ) )
307314 }
308315}
309316
@@ -414,8 +421,8 @@ crate fn build_impl(
414421
415422 debug ! ( "build_impl: impl {:?} for {:?}" , trait_. def_id( ) , for_. def_id( ) ) ;
416423
417- let attrs = box merge_attrs ( cx, parent_module. into ( ) , load_attrs ( cx, did) , attrs) ;
418- debug ! ( "merged_attrs={:?}" , attrs ) ;
424+ let ( merged_attrs , cfg ) = merge_attrs ( cx, parent_module. into ( ) , load_attrs ( cx, did) , attrs) ;
425+ debug ! ( "merged_attrs={:?}" , merged_attrs ) ;
419426
420427 ret. push ( clean:: Item :: from_def_id_and_attrs_and_parts (
421428 did,
@@ -432,8 +439,9 @@ crate fn build_impl(
432439 synthetic : false ,
433440 blanket_impl : None ,
434441 } ) ,
435- attrs ,
442+ box merged_attrs ,
436443 cx,
444+ cfg,
437445 ) ) ;
438446}
439447
@@ -479,6 +487,7 @@ fn build_module(
479487 } ,
480488 true ,
481489 ) ) ,
490+ cfg : None ,
482491 } ) ;
483492 } else if let Some ( i) = try_inline ( cx, did, item. res , item. ident . name , None , visited) {
484493 items. extend ( i)
0 commit comments