Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c2ce254

Browse files
authoredApr 24, 2021
Rollup merge of rust-lang#84460 - jyn514:doctree-is-crate, r=camelid
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``
2 parents 4540c9f + e29f46c commit c2ce254

File tree

8 files changed

+26
-42
lines changed

8 files changed

+26
-42
lines changed
 

‎src/librustdoc/clean/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ fn build_module(
487487
}
488488
}
489489

490-
clean::Module { items, is_crate: false }
490+
clean::Module { items }
491491
}
492492

493493
crate fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {

‎src/librustdoc/clean/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,8 @@ impl Clean<Item> for doctree::Module<'_> {
112112
}
113113
};
114114

115-
let what_rustc_thinks = Item::from_hir_id_and_parts(
116-
self.id,
117-
Some(self.name),
118-
ModuleItem(Module { is_crate: self.is_crate, items }),
119-
cx,
120-
);
115+
let what_rustc_thinks =
116+
Item::from_hir_id_and_parts(self.id, Some(self.name), ModuleItem(Module { items }), cx);
121117
Item { span: span.clean(cx), ..what_rustc_thinks }
122118
}
123119
}

‎src/librustdoc/clean/types.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,9 @@ impl Item {
391391
}
392392

393393
crate fn is_crate(&self) -> bool {
394-
matches!(
395-
*self.kind,
396-
StrippedItem(box ModuleItem(Module { is_crate: true, .. }))
397-
| ModuleItem(Module { is_crate: true, .. })
398-
)
394+
self.is_mod() && self.def_id.index == CRATE_DEF_INDEX
399395
}
396+
400397
crate fn is_mod(&self) -> bool {
401398
self.type_() == ItemType::Module
402399
}
@@ -608,7 +605,6 @@ impl ItemKind {
608605
#[derive(Clone, Debug)]
609606
crate struct Module {
610607
crate items: Vec<Item>,
611-
crate is_crate: bool,
612608
}
613609

614610
crate struct ListAttributesIter<'a> {
@@ -1983,7 +1979,7 @@ crate enum Variant {
19831979

19841980
/// Small wrapper around [`rustc_span::Span]` that adds helper methods
19851981
/// and enforces calling [`rustc_span::Span::source_callsite()`].
1986-
#[derive(Clone, Debug)]
1982+
#[derive(Copy, Clone, Debug)]
19871983
crate struct Span(rustc_span::Span);
19881984

19891985
impl Span {

‎src/librustdoc/doctree.rs

-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ crate struct Module<'hir> {
1414
crate items: Vec<(&'hir hir::Item<'hir>, Option<Symbol>)>,
1515
crate foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>)>,
1616
crate macros: Vec<(&'hir hir::MacroDef<'hir>, Option<Symbol>)>,
17-
crate is_crate: bool,
1817
}
1918

2019
impl Module<'hir> {
@@ -28,7 +27,6 @@ impl Module<'hir> {
2827
items: Vec::new(),
2928
foreigns: Vec::new(),
3029
macros: Vec::new(),
31-
is_crate: false,
3230
}
3331
}
3432
}

‎src/librustdoc/fold.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ crate trait DocFolder: Sized {
8080
}
8181

8282
fn fold_mod(&mut self, m: Module) -> Module {
83-
Module {
84-
is_crate: m.is_crate,
85-
items: m.items.into_iter().filter_map(|i| self.fold_item(i)).collect(),
86-
}
83+
Module { items: m.items.into_iter().filter_map(|i| self.fold_item(i)).collect() }
8784
}
8885

8986
fn fold_crate(&mut self, mut c: Crate) -> Crate {

‎src/librustdoc/html/render/print_item.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer)
2828
// Write the breadcrumb trail header for the top
2929
buf.write_str("<h1 class=\"fqn\"><span class=\"in-band\">");
3030
let name = match *item.kind {
31-
clean::ModuleItem(ref m) => {
32-
if m.is_crate {
31+
clean::ModuleItem(_) => {
32+
if item.is_crate() {
3333
"Crate "
3434
} else {
3535
"Module "

‎src/librustdoc/json/conversions.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_ast::ast;
1010
use rustc_hir::def::CtorKind;
1111
use rustc_middle::ty::TyCtxt;
1212
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
13-
use rustc_span::symbol::Symbol;
1413
use rustc_span::Pos;
1514

1615
use rustdoc_json_types::*;
@@ -34,23 +33,26 @@ impl JsonRenderer<'_> {
3433
did.map(|did| (link.clone(), from_def_id(did)))
3534
})
3635
.collect();
37-
let clean::Item { span, name, attrs, kind, visibility, def_id } = item;
38-
let inner = match *kind {
36+
let docs = item.attrs.collapsed_doc_value();
37+
let attrs = item
38+
.attrs
39+
.other_attrs
40+
.iter()
41+
.map(rustc_ast_pretty::pprust::attribute_to_string)
42+
.collect();
43+
let clean::Item { span, name, attrs: _, kind: _, visibility, def_id } = item;
44+
let inner = match *item.kind {
3945
clean::StrippedItem(_) => return None,
40-
kind => from_clean_item_kind(kind, self.tcx, &name),
46+
_ => from_clean_item(item, self.tcx),
4147
};
4248
Some(Item {
4349
id: from_def_id(def_id),
4450
crate_id: def_id.krate.as_u32(),
4551
name: name.map(|sym| sym.to_string()),
4652
span: self.convert_span(span),
4753
visibility: self.convert_visibility(visibility),
48-
docs: attrs.collapsed_doc_value(),
49-
attrs: attrs
50-
.other_attrs
51-
.iter()
52-
.map(rustc_ast_pretty::pprust::attribute_to_string)
53-
.collect(),
54+
docs,
55+
attrs,
5456
deprecation: deprecation.map(from_deprecation),
5557
inner,
5658
links,
@@ -172,10 +174,12 @@ crate fn from_def_id(did: DefId) -> Id {
172174
Id(format!("{}:{}", did.krate.as_u32(), u32::from(did.index)))
173175
}
174176

175-
fn from_clean_item_kind(item: clean::ItemKind, tcx: TyCtxt<'_>, name: &Option<Symbol>) -> ItemEnum {
177+
fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
176178
use clean::ItemKind::*;
177-
match item {
178-
ModuleItem(m) => ItemEnum::Module(m.into_tcx(tcx)),
179+
let name = item.name;
180+
let is_crate = item.is_crate();
181+
match *item.kind {
182+
ModuleItem(m) => ItemEnum::Module(Module { is_crate, items: ids(m.items) }),
179183
ImportItem(i) => ItemEnum::Import(i.into_tcx(tcx)),
180184
StructItem(s) => ItemEnum::Struct(s.into_tcx(tcx)),
181185
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
214218
}
215219
}
216220

217-
impl FromWithTcx<clean::Module> for Module {
218-
fn from_tcx(module: clean::Module, _tcx: TyCtxt<'_>) -> Self {
219-
Module { is_crate: module.is_crate, items: ids(module.items) }
220-
}
221-
}
222-
223221
impl FromWithTcx<clean::Struct> for Struct {
224222
fn from_tcx(struct_: clean::Struct, tcx: TyCtxt<'_>) -> Self {
225223
let clean::Struct { struct_type, generics, fields, fields_stripped } = struct_;

‎src/librustdoc/visit_ast.rs

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
7979
&krate.item,
8080
self.cx.tcx.crate_name,
8181
);
82-
top_level_module.is_crate = true;
8382
// Attach the crate's exported macros to the top-level module.
8483
// In the case of macros 2.0 (`pub macro`), and for built-in `derive`s or attributes as
8584
// well (_e.g._, `Copy`), these are wrongly bundled in there too, so we need to fix that by

0 commit comments

Comments
 (0)
Please sign in to comment.