rustdoc: Remove unnecessary is_crate
field from doctree::Module and clean::Module
It can be calculated on-demand even without a TyCtxt. This also changed `from_item_kind` to take a whole item, which avoids having to add more and more parameters.
This commit is contained in:
parent
484c61943f
commit
e29f46ce37
8 changed files with 26 additions and 42 deletions
|
@ -487,7 +487,7 @@ fn build_module(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clean::Module { items, is_crate: false }
|
clean::Module { items }
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
|
crate fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
|
||||||
|
|
|
@ -112,12 +112,8 @@ impl Clean<Item> for doctree::Module<'_> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let what_rustc_thinks = Item::from_hir_id_and_parts(
|
let what_rustc_thinks =
|
||||||
self.id,
|
Item::from_hir_id_and_parts(self.id, Some(self.name), ModuleItem(Module { items }), cx);
|
||||||
Some(self.name),
|
|
||||||
ModuleItem(Module { is_crate: self.is_crate, items }),
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
Item { span: span.clean(cx), ..what_rustc_thinks }
|
Item { span: span.clean(cx), ..what_rustc_thinks }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -391,12 +391,9 @@ impl Item {
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn is_crate(&self) -> bool {
|
crate fn is_crate(&self) -> bool {
|
||||||
matches!(
|
self.is_mod() && self.def_id.index == CRATE_DEF_INDEX
|
||||||
*self.kind,
|
|
||||||
StrippedItem(box ModuleItem(Module { is_crate: true, .. }))
|
|
||||||
| ModuleItem(Module { is_crate: true, .. })
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
crate fn is_mod(&self) -> bool {
|
crate fn is_mod(&self) -> bool {
|
||||||
self.type_() == ItemType::Module
|
self.type_() == ItemType::Module
|
||||||
}
|
}
|
||||||
|
@ -608,7 +605,6 @@ impl ItemKind {
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
crate struct Module {
|
crate struct Module {
|
||||||
crate items: Vec<Item>,
|
crate items: Vec<Item>,
|
||||||
crate is_crate: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
crate struct ListAttributesIter<'a> {
|
crate struct ListAttributesIter<'a> {
|
||||||
|
@ -1983,7 +1979,7 @@ crate enum Variant {
|
||||||
|
|
||||||
/// Small wrapper around [`rustc_span::Span]` that adds helper methods
|
/// Small wrapper around [`rustc_span::Span]` that adds helper methods
|
||||||
/// and enforces calling [`rustc_span::Span::source_callsite()`].
|
/// and enforces calling [`rustc_span::Span::source_callsite()`].
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Copy, Clone, Debug)]
|
||||||
crate struct Span(rustc_span::Span);
|
crate struct Span(rustc_span::Span);
|
||||||
|
|
||||||
impl Span {
|
impl Span {
|
||||||
|
|
|
@ -14,7 +14,6 @@ crate struct Module<'hir> {
|
||||||
crate items: Vec<(&'hir hir::Item<'hir>, Option<Symbol>)>,
|
crate items: Vec<(&'hir hir::Item<'hir>, Option<Symbol>)>,
|
||||||
crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>)>,
|
crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>)>,
|
||||||
crate macros: Vec<(&'hir hir::MacroDef<'hir>, Option<Symbol>)>,
|
crate macros: Vec<(&'hir hir::MacroDef<'hir>, Option<Symbol>)>,
|
||||||
crate is_crate: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Module<'hir> {
|
impl Module<'hir> {
|
||||||
|
@ -28,7 +27,6 @@ impl Module<'hir> {
|
||||||
items: Vec::new(),
|
items: Vec::new(),
|
||||||
foreigns: Vec::new(),
|
foreigns: Vec::new(),
|
||||||
macros: Vec::new(),
|
macros: Vec::new(),
|
||||||
is_crate: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,10 +80,7 @@ crate trait DocFolder: Sized {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fold_mod(&mut self, m: Module) -> Module {
|
fn fold_mod(&mut self, m: Module) -> Module {
|
||||||
Module {
|
Module { items: m.items.into_iter().filter_map(|i| self.fold_item(i)).collect() }
|
||||||
is_crate: m.is_crate,
|
|
||||||
items: m.items.into_iter().filter_map(|i| self.fold_item(i)).collect(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fold_crate(&mut self, mut c: Crate) -> Crate {
|
fn fold_crate(&mut self, mut c: Crate) -> Crate {
|
||||||
|
|
|
@ -28,8 +28,8 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer)
|
||||||
// Write the breadcrumb trail header for the top
|
// Write the breadcrumb trail header for the top
|
||||||
buf.write_str("<h1 class=\"fqn\"><span class=\"in-band\">");
|
buf.write_str("<h1 class=\"fqn\"><span class=\"in-band\">");
|
||||||
let name = match *item.kind {
|
let name = match *item.kind {
|
||||||
clean::ModuleItem(ref m) => {
|
clean::ModuleItem(_) => {
|
||||||
if m.is_crate {
|
if item.is_crate() {
|
||||||
"Crate "
|
"Crate "
|
||||||
} else {
|
} else {
|
||||||
"Module "
|
"Module "
|
||||||
|
|
|
@ -10,7 +10,6 @@ use rustc_ast::ast;
|
||||||
use rustc_hir::def::CtorKind;
|
use rustc_hir::def::CtorKind;
|
||||||
use rustc_middle::ty::TyCtxt;
|
use rustc_middle::ty::TyCtxt;
|
||||||
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
|
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
|
||||||
use rustc_span::symbol::Symbol;
|
|
||||||
use rustc_span::Pos;
|
use rustc_span::Pos;
|
||||||
|
|
||||||
use rustdoc_json_types::*;
|
use rustdoc_json_types::*;
|
||||||
|
@ -34,10 +33,17 @@ impl JsonRenderer<'_> {
|
||||||
did.map(|did| (link.clone(), from_def_id(did)))
|
did.map(|did| (link.clone(), from_def_id(did)))
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
let clean::Item { span, name, attrs, kind, visibility, def_id } = item;
|
let docs = item.attrs.collapsed_doc_value();
|
||||||
let inner = match *kind {
|
let attrs = item
|
||||||
|
.attrs
|
||||||
|
.other_attrs
|
||||||
|
.iter()
|
||||||
|
.map(rustc_ast_pretty::pprust::attribute_to_string)
|
||||||
|
.collect();
|
||||||
|
let clean::Item { span, name, attrs: _, kind: _, visibility, def_id } = item;
|
||||||
|
let inner = match *item.kind {
|
||||||
clean::StrippedItem(_) => return None,
|
clean::StrippedItem(_) => return None,
|
||||||
kind => from_clean_item_kind(kind, self.tcx, &name),
|
_ => from_clean_item(item, self.tcx),
|
||||||
};
|
};
|
||||||
Some(Item {
|
Some(Item {
|
||||||
id: from_def_id(def_id),
|
id: from_def_id(def_id),
|
||||||
|
@ -45,12 +51,8 @@ impl JsonRenderer<'_> {
|
||||||
name: name.map(|sym| sym.to_string()),
|
name: name.map(|sym| sym.to_string()),
|
||||||
span: self.convert_span(span),
|
span: self.convert_span(span),
|
||||||
visibility: self.convert_visibility(visibility),
|
visibility: self.convert_visibility(visibility),
|
||||||
docs: attrs.collapsed_doc_value(),
|
docs,
|
||||||
attrs: attrs
|
attrs,
|
||||||
.other_attrs
|
|
||||||
.iter()
|
|
||||||
.map(rustc_ast_pretty::pprust::attribute_to_string)
|
|
||||||
.collect(),
|
|
||||||
deprecation: deprecation.map(from_deprecation),
|
deprecation: deprecation.map(from_deprecation),
|
||||||
inner,
|
inner,
|
||||||
links,
|
links,
|
||||||
|
@ -172,10 +174,12 @@ crate fn from_def_id(did: DefId) -> Id {
|
||||||
Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index)))
|
Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index)))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>, name: &Option<Symbol>) -> ItemEnum {
|
fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
|
||||||
use clean::ItemKind::*;
|
use clean::ItemKind::*;
|
||||||
match item {
|
let name = item.name;
|
||||||
ModuleItem(m) => ItemEnum::Module(m.into_tcx(tcx)),
|
let is_crate = item.is_crate();
|
||||||
|
match *item.kind {
|
||||||
|
ModuleItem(m) => ItemEnum::Module(Module { is_crate, items: ids(m.items) }),
|
||||||
ImportItem(i) => ItemEnum::Import(i.into_tcx(tcx)),
|
ImportItem(i) => ItemEnum::Import(i.into_tcx(tcx)),
|
||||||
StructItem(s) => ItemEnum::Struct(s.into_tcx(tcx)),
|
StructItem(s) => ItemEnum::Struct(s.into_tcx(tcx)),
|
||||||
UnionItem(u) => ItemEnum::Union(u.into_tcx(tcx)),
|
UnionItem(u) => ItemEnum::Union(u.into_tcx(tcx)),
|
||||||
|
@ -214,12 +218,6 @@ fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>, name: &Option<Sy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromWithTcx<clean::Module> for Module {
|
|
||||||
fn from_tcx(module: clean::Module, _tcx: TyCtxt<'_>) -> Self {
|
|
||||||
Module { is_crate: module.is_crate, items: ids(module.items) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromWithTcx<clean::Struct> for Struct {
|
impl FromWithTcx<clean::Struct> for Struct {
|
||||||
fn from_tcx(struct_: clean::Struct, tcx: TyCtxt<'_>) -> Self {
|
fn from_tcx(struct_: clean::Struct, tcx: TyCtxt<'_>) -> Self {
|
||||||
let clean::Struct { struct_type, generics, fields, fields_stripped } = struct_;
|
let clean::Struct { struct_type, generics, fields, fields_stripped } = struct_;
|
||||||
|
|
|
@ -78,7 +78,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
|
||||||
&krate.item,
|
&krate.item,
|
||||||
self.cx.tcx.crate_name,
|
self.cx.tcx.crate_name,
|
||||||
);
|
);
|
||||||
top_level_module.is_crate = true;
|
|
||||||
// Attach the crate's exported macros to the top-level module.
|
// Attach the crate's exported macros to the top-level module.
|
||||||
// In the case of macros 2.0 (`pub macro`), and for built-in `derive`s or attributes as
|
// In the case of macros 2.0 (`pub macro`), and for built-in `derive`s or attributes as
|
||||||
// well (_e.g._, `Copy`), these are wrongly bundled in there too, so we need to fix that by
|
// well (_e.g._, `Copy`), these are wrongly bundled in there too, so we need to fix that by
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue