@@ -44,7 +44,9 @@ pub struct RustdocVisitor<'a, 'tcx: 'a> {
44
44
pub attrs : hir:: HirVec < ast:: Attribute > ,
45
45
pub cx : & ' a core:: DocContext < ' a , ' tcx > ,
46
46
view_item_stack : FxHashSet < ast:: NodeId > ,
47
- inlining_from_glob : bool ,
47
+ inlining : bool ,
48
+ /// Is the current module and all of its parents public?
49
+ inside_public_path : bool ,
48
50
}
49
51
50
52
impl < ' a , ' tcx > RustdocVisitor < ' a , ' tcx > {
@@ -57,7 +59,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
57
59
attrs : hir:: HirVec :: new ( ) ,
58
60
cx : cx,
59
61
view_item_stack : stack,
60
- inlining_from_glob : false ,
62
+ inlining : false ,
63
+ inside_public_path : true ,
61
64
}
62
65
}
63
66
@@ -189,10 +192,14 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
189
192
om. stab = self . stability ( id) ;
190
193
om. depr = self . deprecation ( id) ;
191
194
om. id = id;
195
+ // Keep track of if there were any private modules in the path.
196
+ let orig_inside_public_path = self . inside_public_path ;
197
+ self . inside_public_path &= vis == hir:: Public ;
192
198
for i in & m. item_ids {
193
199
let item = self . cx . map . expect_item ( i. id ) ;
194
200
self . visit_item ( item, None , & mut om) ;
195
201
}
202
+ self . inside_public_path = orig_inside_public_path;
196
203
if let Some ( exports) = self . cx . export_map . get ( & id) {
197
204
for export in exports {
198
205
if let Def :: Macro ( def_id) = export. def {
@@ -336,8 +343,8 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
336
343
337
344
let ret = match tcx. map . get ( def_node_id) {
338
345
hir_map:: NodeItem ( it) => {
346
+ let prev = mem:: replace ( & mut self . inlining , true ) ;
339
347
if glob {
340
- let prev = mem:: replace ( & mut self . inlining_from_glob , true ) ;
341
348
match it. node {
342
349
hir:: ItemMod ( ref m) => {
343
350
for i in & m. item_ids {
@@ -348,10 +355,10 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
348
355
hir:: ItemEnum ( ..) => { }
349
356
_ => { panic ! ( "glob not mapped to a module or enum" ) ; }
350
357
}
351
- self . inlining_from_glob = prev;
352
358
} else {
353
359
self . visit_item ( it, renamed, om) ;
354
360
}
361
+ self . inlining = prev;
355
362
true
356
363
}
357
364
_ => false ,
@@ -365,6 +372,19 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
365
372
debug ! ( "Visiting item {:?}" , item) ;
366
373
let name = renamed. unwrap_or ( item. name ) ;
367
374
match item. node {
375
+ hir:: ItemForeignMod ( ref fm) => {
376
+ // If inlining we only want to include public functions.
377
+ om. foreigns . push ( if self . inlining {
378
+ hir:: ForeignMod {
379
+ abi : fm. abi ,
380
+ items : fm. items . iter ( ) . filter ( |i| i. vis == hir:: Public ) . cloned ( ) . collect ( ) ,
381
+ }
382
+ } else {
383
+ fm. clone ( )
384
+ } ) ;
385
+ }
386
+ // If we're inlining, skip private items.
387
+ _ if self . inlining && item. vis != hir:: Public => { }
368
388
hir:: ItemExternCrate ( ref p) => {
369
389
let cstore = & self . cx . sess ( ) . cstore ;
370
390
om. extern_crates . push ( ExternCrate {
@@ -379,7 +399,9 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
379
399
}
380
400
hir:: ItemUse ( ref vpath) => {
381
401
let node = vpath. node . clone ( ) ;
382
- let node = if item. vis == hir:: Public {
402
+ // If there was a private module in the current path then don't bother inlining
403
+ // anything as it will probably be stripped anyway.
404
+ let node = if item. vis == hir:: Public && self . inside_public_path {
383
405
let please_inline = item. attrs . iter ( ) . any ( |item| {
384
406
match item. meta_item_list ( ) {
385
407
Some ( list) if item. check_name ( "doc" ) => {
@@ -479,43 +501,41 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
479
501
} ;
480
502
om. traits . push ( t) ;
481
503
} ,
504
+
482
505
hir:: ItemImpl ( unsafety, polarity, ref gen, ref tr, ref ty, ref items) => {
483
- let i = Impl {
484
- unsafety : unsafety ,
485
- polarity : polarity ,
486
- generics : gen . clone ( ) ,
487
- trait_ : tr . clone ( ) ,
488
- for_ : ty . clone ( ) ,
489
- items : items . clone ( ) ,
490
- attrs : item . attrs . clone ( ) ,
491
- id : item . id ,
492
- whence : item . span ,
493
- vis : item. vis . clone ( ) ,
494
- stab : self . stability ( item. id ) ,
495
- depr : self . deprecation ( item. id ) ,
496
- } ;
497
- // Don't duplicate impls when inlining glob imports, we'll pick
498
- // them up regardless of where they're located.
499
- if ! self . inlining_from_glob {
506
+ // Don't duplicate impls when inlining, we'll pick them up
507
+ // regardless of where they're located.
508
+ if ! self . inlining {
509
+ let i = Impl {
510
+ unsafety : unsafety ,
511
+ polarity : polarity ,
512
+ generics : gen . clone ( ) ,
513
+ trait_ : tr . clone ( ) ,
514
+ for_ : ty . clone ( ) ,
515
+ items : items . clone ( ) ,
516
+ attrs : item. attrs . clone ( ) ,
517
+ id : item. id ,
518
+ whence : item. span ,
519
+ vis : item . vis . clone ( ) ,
520
+ stab : self . stability ( item . id ) ,
521
+ depr : self . deprecation ( item . id ) ,
522
+ } ;
500
523
om. impls . push ( i) ;
501
524
}
502
525
} ,
503
526
hir:: ItemDefaultImpl ( unsafety, ref trait_ref) => {
504
- let i = DefaultImpl {
505
- unsafety : unsafety ,
506
- trait_ : trait_ref . clone ( ) ,
507
- id : item . id ,
508
- attrs : item . attrs . clone ( ) ,
509
- whence : item. span ,
510
- } ;
511
- // see comment above about ItemImpl
512
- if ! self . inlining_from_glob {
527
+ // See comment above about ItemImpl.
528
+ if ! self . inlining {
529
+ let i = DefaultImpl {
530
+ unsafety : unsafety ,
531
+ trait_ : trait_ref . clone ( ) ,
532
+ id : item. id ,
533
+ attrs : item . attrs . clone ( ) ,
534
+ whence : item . span ,
535
+ } ;
513
536
om. def_traits . push ( i) ;
514
537
}
515
538
}
516
- hir:: ItemForeignMod ( ref fm) => {
517
- om. foreigns . push ( fm. clone ( ) ) ;
518
- }
519
539
}
520
540
}
521
541
0 commit comments