Skip to content

Commit

Permalink
Rollup merge of rust-lang#84460 - jyn514:doctree-is-crate, r=camelid
Browse files Browse the repository at this point in the history
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 `json::conversions::from_item_kind` to take a whole item, which avoids
having to add more and more parameters.

Helps with rust-lang#76382.

r? ```@camelid```
  • Loading branch information
JohnTitor authored Apr 24, 2021
2 parents da8d948 + e29f46c commit 5315f50
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 2 additions & 6 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,8 @@ impl Clean<Item> for doctree::Module<'_> {
}
};

let what_rustc_thinks = Item::from_hir_id_and_parts(
self.id,
Some(self.name),
ModuleItem(Module { is_crate: self.is_crate, items }),
cx,
);
let what_rustc_thinks =
Item::from_hir_id_and_parts(self.id, Some(self.name), ModuleItem(Module { items }), cx);
Item { span: span.clean(cx), ..what_rustc_thinks }
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,9 @@ impl Item {
}

crate fn is_crate(&self) -> bool {
matches!(
*self.kind,
StrippedItem(box ModuleItem(Module { is_crate: true, .. }))
| ModuleItem(Module { is_crate: true, .. })
)
self.is_mod() && self.def_id.index == CRATE_DEF_INDEX
}

crate fn is_mod(&self) -> bool {
self.type_() == ItemType::Module
}
Expand Down Expand Up @@ -608,7 +605,6 @@ impl ItemKind {
#[derive(Clone, Debug)]
crate struct Module {
crate items: Vec<Item>,
crate is_crate: bool,
}

crate struct ListAttributesIter<'a> {
Expand Down Expand Up @@ -1983,7 +1979,7 @@ crate enum Variant {

/// Small wrapper around [`rustc_span::Span]` that adds helper methods
/// and enforces calling [`rustc_span::Span::source_callsite()`].
#[derive(Clone, Debug)]
#[derive(Copy, Clone, Debug)]
crate struct Span(rustc_span::Span);

impl Span {
Expand Down
2 changes: 0 additions & 2 deletions src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ crate struct Module<'hir> {
crate items: Vec<(&'hir hir::Item<'hir>, Option<Symbol>)>,
crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>)>,
crate macros: Vec<(&'hir hir::MacroDef<'hir>, Option<Symbol>)>,
crate is_crate: bool,
}

impl Module<'hir> {
Expand All @@ -28,7 +27,6 @@ impl Module<'hir> {
items: Vec::new(),
foreigns: Vec::new(),
macros: Vec::new(),
is_crate: false,
}
}
}
5 changes: 1 addition & 4 deletions src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ crate trait DocFolder: Sized {
}

fn fold_mod(&mut self, m: Module) -> Module {
Module {
is_crate: m.is_crate,
items: m.items.into_iter().filter_map(|i| self.fold_item(i)).collect(),
}
Module { items: m.items.into_iter().filter_map(|i| self.fold_item(i)).collect() }
}

fn fold_crate(&mut self, mut c: Crate) -> Crate {
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
buf.write_str("<h1 class=\"fqn\"><span class=\"in-band\">");
let name = match *item.kind {
clean::ModuleItem(ref m) => {
if m.is_crate {
clean::ModuleItem(_) => {
if item.is_crate() {
"Crate "
} else {
"Module "
Expand Down
36 changes: 17 additions & 19 deletions src/librustdoc/json/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use rustc_ast::ast;
use rustc_hir::def::CtorKind;
use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
use rustc_span::symbol::Symbol;
use rustc_span::Pos;

use rustdoc_json_types::*;
Expand All @@ -34,23 +33,26 @@ impl JsonRenderer<'_> {
did.map(|did| (link.clone(), from_def_id(did)))
})
.collect();
let clean::Item { span, name, attrs, kind, visibility, def_id } = item;
let inner = match *kind {
let docs = item.attrs.collapsed_doc_value();
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,
kind => from_clean_item_kind(kind, self.tcx, &name),
_ => from_clean_item(item, self.tcx),
};
Some(Item {
id: from_def_id(def_id),
crate_id: def_id.krate.as_u32(),
name: name.map(|sym| sym.to_string()),
span: self.convert_span(span),
visibility: self.convert_visibility(visibility),
docs: attrs.collapsed_doc_value(),
attrs: attrs
.other_attrs
.iter()
.map(rustc_ast_pretty::pprust::attribute_to_string)
.collect(),
docs,
attrs,
deprecation: deprecation.map(from_deprecation),
inner,
links,
Expand Down Expand Up @@ -172,10 +174,12 @@ crate fn from_def_id(did: DefId) -> Id {
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::*;
match item {
ModuleItem(m) => ItemEnum::Module(m.into_tcx(tcx)),
let name = item.name;
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)),
StructItem(s) => ItemEnum::Struct(s.into_tcx(tcx)),
UnionItem(u) => ItemEnum::Union(u.into_tcx(tcx)),
Expand Down Expand Up @@ -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 {
fn from_tcx(struct_: clean::Struct, tcx: TyCtxt<'_>) -> Self {
let clean::Struct { struct_type, generics, fields, fields_stripped } = struct_;
Expand Down
1 change: 0 additions & 1 deletion src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
&krate.item,
self.cx.tcx.crate_name,
);
top_level_module.is_crate = true;
// 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
// well (_e.g._, `Copy`), these are wrongly bundled in there too, so we need to fix that by
Expand Down

0 comments on commit 5315f50

Please sign in to comment.