Registry Mirror
Skip to content

File tree

‎src/librustdoc/clean/inline.rs‎

Lines changed: 128 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub(crate) fn try_inline(
142142
Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => return Some(Vec::new()),
143143
Res::Def(DefKind::Mod, did) => {
144144
record_extern_fqn(cx, did, ItemType::Module);
145-
clean::ModuleItem(build_module(cx, did, visited))
145+
clean::ModuleItem(build_module(cx, did, name, visited))
146146
}
147147
Res::Def(DefKind::Static { .. }, did) => {
148148
record_extern_fqn(cx, did, ItemType::Static);
@@ -207,6 +207,7 @@ pub(crate) fn try_inline_glob(
207207
let mut items = build_module_items(
208208
cx,
209209
did,
210+
cx.tcx.item_name(did),
210211
visited,
211212
inlined_names,
212213
Some(&reexports),
@@ -665,16 +666,43 @@ pub(crate) fn build_impl(
665666
));
666667
}
667668

668-
fn build_module(cx: &mut DocContext<'_>, did: DefId, visited: &mut DefIdSet) -> clean::Module {
669-
let items = build_module_items(cx, did, visited, &mut FxHashSet::default(), None, None);
669+
fn build_module(
670+
cx: &mut DocContext<'_>,
671+
did: DefId,
672+
name: Symbol,
673+
visited: &mut DefIdSet,
674+
) -> clean::Module {
675+
let items = build_module_items(cx, did, name, visited, &mut FxHashSet::default(), None, None);
670676

671677
let span = clean::Span::new(cx.tcx.def_span(did));
672678
clean::Module { items, span }
673679
}
674680

681+
// We are only interested into `Res::Def`. And in there, we only want "items" which get their own
682+
// rustdoc page. So not `DefKind::Ctor` for example (which is returned by `tcx.module_children()`).
683+
fn should_ignore_res(res: Res) -> bool {
684+
!matches!(
685+
res,
686+
Res::Def(DefKind::Trait, _)
687+
| Res::Def(DefKind::TraitAlias, _)
688+
| Res::Def(DefKind::Fn, _)
689+
| Res::Def(DefKind::Struct, _)
690+
| Res::Def(DefKind::Union, _)
691+
| Res::Def(DefKind::TyAlias, _)
692+
| Res::Def(DefKind::Enum, _)
693+
| Res::Def(DefKind::ForeignTy, _)
694+
| Res::Def(DefKind::Variant, _)
695+
| Res::Def(DefKind::Mod, _)
696+
| Res::Def(DefKind::Static { .. }, _)
697+
| Res::Def(DefKind::Const { .. }, _)
698+
| Res::Def(DefKind::Macro(_), _)
699+
)
700+
}
701+
675702
fn build_module_items(
676703
cx: &mut DocContext<'_>,
677-
did: DefId,
704+
module_def_id: DefId,
705+
module_name: Symbol,
678706
visited: &mut DefIdSet,
679707
inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
680708
allowed_def_ids: Option<&DefIdSet>,
@@ -685,61 +713,107 @@ fn build_module_items(
685713
// If we're re-exporting a re-export it may actually re-export something in
686714
// two namespaces, so the target may be listed twice. Make sure we only
687715
// visit each node at most once.
688-
for item in cx.tcx.module_children(did).iter() {
689-
if item.vis.is_public() {
690-
let res = item.res.expect_non_local();
691-
if let Some(def_id) = res.opt_def_id()
692-
&& let Some(allowed_def_ids) = allowed_def_ids
693-
&& !allowed_def_ids.contains(&def_id)
716+
for item in cx.tcx.module_children(module_def_id).iter() {
717+
if !item.vis.is_public() {
718+
continue;
719+
}
720+
let res = item.res.expect_non_local();
721+
if let Some(def_id) = res.opt_def_id()
722+
&& let Some(allowed_def_ids) = allowed_def_ids
723+
&& !allowed_def_ids.contains(&def_id)
724+
{
725+
continue;
726+
}
727+
if let Some(def_id) = res.mod_def_id() {
728+
// If we're inlining a glob import, it's possible to have
729+
// two distinct modules with the same name. We don't want to
730+
// inline it, or mark any of its contents as visited.
731+
if module_def_id == def_id
732+
|| inlined_names.contains(&(ItemType::Module, item.ident.name))
733+
|| !visited.insert(def_id)
694734
{
695735
continue;
696736
}
697-
if let Some(def_id) = res.mod_def_id() {
698-
// If we're inlining a glob import, it's possible to have
699-
// two distinct modules with the same name. We don't want to
700-
// inline it, or mark any of its contents as visited.
701-
if did == def_id
702-
|| inlined_names.contains(&(ItemType::Module, item.ident.name))
703-
|| !visited.insert(def_id)
704-
{
705-
continue;
706-
}
707-
}
708-
if let Res::PrimTy(p) = res {
709-
// Primitive types can't be inlined so generate an import instead.
710-
let prim_ty = clean::PrimitiveType::from(p);
711-
items.push(clean::Item {
712-
inner: Box::new(clean::ItemInner {
713-
name: None,
714-
// We can use the item's `DefId` directly since the only information ever
715-
// used from it is `DefId.krate`.
716-
item_id: ItemId::DefId(did),
717-
attrs: Default::default(),
718-
stability: None,
719-
kind: clean::ImportItem(clean::Import::new_simple(
720-
item.ident.name,
721-
clean::ImportSource {
722-
path: clean::Path {
723-
res,
724-
segments: thin_vec![clean::PathSegment {
725-
name: prim_ty.as_sym(),
726-
args: clean::GenericArgs::AngleBracketed {
727-
args: Default::default(),
728-
constraints: ThinVec::new(),
729-
},
730-
}],
731-
},
732-
did: None,
737+
}
738+
if let Res::PrimTy(p) = res {
739+
// Primitive types can't be inlined so generate an import instead.
740+
let prim_ty = clean::PrimitiveType::from(p);
741+
items.push(clean::Item {
742+
inner: Box::new(clean::ItemInner {
743+
name: None,
744+
// We can use the item's `DefId` directly since the only information ever
745+
// used from it is `DefId.krate`.
746+
item_id: ItemId::DefId(module_def_id),
747+
attrs: Default::default(),
748+
stability: None,
749+
kind: clean::ImportItem(clean::Import::new_simple(
750+
item.ident.name,
751+
clean::ImportSource {
752+
path: clean::Path {
753+
res,
754+
segments: thin_vec![clean::PathSegment {
755+
name: prim_ty.as_sym(),
756+
args: clean::GenericArgs::AngleBracketed {
757+
args: Default::default(),
758+
constraints: ThinVec::new(),
759+
},
760+
}],
733761
},
734-
true,
735-
)),
736-
cfg: None,
737-
inline_stmt_id: None,
738-
}),
739-
});
740-
} else if let Some(i) = try_inline(cx, res, item.ident.name, attrs, visited) {
741-
items.extend(i)
762+
did: None,
763+
},
764+
true,
765+
)),
766+
cfg: None,
767+
inline_stmt_id: None,
768+
}),
769+
});
770+
} else if let Some(def_id) = res.opt_def_id()
771+
&& let Some(reexport) = item.reexport_chain.first()
772+
&& let Some(reexport_def_id) = reexport.id()
773+
&& find_attr!(
774+
load_attrs(cx.tcx, reexport_def_id),
775+
Doc(d)
776+
if d.inline.first().is_some_and(|(inline, _)| *inline == hir::attrs::DocInline::NoInline)
777+
)
778+
{
779+
if should_ignore_res(res) {
780+
continue;
742781
}
782+
// This item is reexported as `no_inline` so it shouldn't be inlined.
783+
let item = Item::from_def_id_and_parts(
784+
module_def_id,
785+
None,
786+
clean::ImportItem(clean::Import::new_simple(
787+
item.ident.name,
788+
clean::ImportSource {
789+
path: clean::Path {
790+
res,
791+
segments: thin_vec![
792+
clean::PathSegment {
793+
name: module_name,
794+
args: clean::GenericArgs::AngleBracketed {
795+
args: Default::default(),
796+
constraints: ThinVec::new(),
797+
},
798+
},
799+
clean::PathSegment {
800+
name: cx.tcx.item_name(def_id),
801+
args: clean::GenericArgs::AngleBracketed {
802+
args: Default::default(),
803+
constraints: ThinVec::new(),
804+
},
805+
},
806+
],
807+
},
808+
did: None,
809+
},
810+
true,
811+
)),
812+
cx.tcx,
813+
);
814+
items.push(item);
815+
} else if let Some(i) = try_inline(cx, res, item.ident.name, attrs, visited) {
816+
items.extend(i)
743817
}
744818
}
745819

‎src/librustdoc/passes/stripper.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,14 @@ impl DocFolder for ImportStripper<'_> {
287287
clean::ImportItem(imp)
288288
if !self.document_hidden && self.import_should_be_hidden(&i, imp) =>
289289
{
290+
debug!("ImportStripper: stripping {:?}", i.name);
290291
None
291292
}
292293
// clean::ImportItem(_) if !self.document_hidden && i.is_doc_hidden() => None,
293294
clean::ExternCrateItem { .. } | clean::ImportItem(..)
294295
if i.visibility(self.tcx) != Some(Visibility::Public) =>
295296
{
297+
debug!("ImportStripper: stripping {:?}", i.name);
296298
None
297299
}
298300
_ => Some(self.fold_item_recur(i)),
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[doc(no_inline)]
2+
pub use crate::{
3+
future::{Future, FutureExt as _},
4+
};
5+
6+
mod future {
7+
pub struct Future;
8+
pub trait FutureExt {}
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This test ensures that an inlined foreign item with `#[doc(no_inline)]` is
2+
// not inlined.
3+
// This is a regression test for <https://github.com/rust-lang/rust/issues/92379>.
4+
5+
//@ aux-build: inline-foreign-no_inline.rs
6+
7+
#![crate_name = "foo"]
8+
9+
extern crate inline_foreign_no_inline;
10+
11+
// Since we cannot inline `inline_foreign_no_inline` because it has `no_inline`, there
12+
// should have no other items than "Module".
13+
//@ has 'foo/index.html'
14+
//@ count - '//*[@id="main-content"]/h2' 1
15+
//@ has - '//*[@id="main-content"]/h2' 'Module'
16+
//@ has - '//*[@id="main-content"]/dl[@class="item-table"]/dt' 'dep'
17+
18+
//@ has 'foo/dep/index.html'
19+
//@ has - '//*[@class="item-table reexports"]/dt' 'pub use dep::Future;'
20+
//@ has - '//*[@class="item-table reexports"]/dt' 'pub use dep::FutureExt as _;'
21+
#[doc(inline)]
22+
pub use inline_foreign_no_inline as dep;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This test checks that inlining a `no_inline` is done correctly.
2+
3+
#![crate_name = "foo"]
4+
5+
//@ has 'foo/index.html'
6+
// There should be `Re-exports` and `Structs`
7+
//@ count - '//*[@id="main-content"]/h2' 2
8+
//@ has - '//*[@id="main-content"]/h2[@class="section-header"]' 'Re-exports'
9+
//@ has - '//*[@id="main-content"]/h2[@class="section-header"]' 'Structs'
10+
11+
//@ has - '//*[@id="main-content"]/dl[@class="item-table reexports"]/dt' 'pub use self::bar::A;'
12+
//@ has - '//*[@id="main-content"]/dl[@class="item-table"]/dt' 'X'
13+
14+
mod bar {
15+
pub struct A;
16+
}
17+
18+
#[doc(no_inline)]
19+
pub use self::bar::A;
20+
#[doc(inline)]
21+
pub use self::A as X;

0 commit comments

Comments
 (0)