Skip to content

Commit e2e9e34

Browse files
authored
Rollup merge of #81021 - CraftSpider:rustdoc-remove-import, r=jyn514
Remove doctree::Import Per the title. Part of cleaning up doctree
2 parents 11a63fb + a70813e commit e2e9e34

File tree

5 files changed

+91
-116
lines changed

5 files changed

+91
-116
lines changed

src/librustdoc/clean/mod.rs

+85-91
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ impl Clean<ExternalCrate> for CrateNum {
219219
impl Clean<Item> for doctree::Module<'_> {
220220
fn clean(&self, cx: &DocContext<'_>) -> Item {
221221
let mut items: Vec<Item> = vec![];
222-
items.extend(self.imports.iter().flat_map(|x| x.clean(cx)));
223222
items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
224223
items.extend(self.mods.iter().map(|x| x.clean(cx)));
225224
items.extend(self.items.iter().map(|x| x.clean(cx)).flatten());
@@ -2032,7 +2031,7 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
20322031
ItemKind::Fn(ref sig, ref generics, body_id) => {
20332032
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
20342033
}
2035-
hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
2034+
ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
20362035
let items = item_ids
20372036
.iter()
20382037
.map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx))
@@ -2051,6 +2050,9 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
20512050
ItemKind::ExternCrate(orig_name) => {
20522051
return clean_extern_crate(item, name, orig_name, cx);
20532052
}
2053+
ItemKind::Use(path, kind) => {
2054+
return clean_use_statement(item, name, path, kind, cx);
2055+
}
20542056
_ => unreachable!("not yet converted"),
20552057
};
20562058

@@ -2172,105 +2174,97 @@ fn clean_extern_crate(
21722174
}]
21732175
}
21742176

2175-
impl Clean<Vec<Item>> for doctree::Import<'_> {
2176-
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
2177-
// We need this comparison because some imports (for std types for example)
2178-
// are "inserted" as well but directly by the compiler and they should not be
2179-
// taken into account.
2180-
if self.span.ctxt().outer_expn_data().kind == ExpnKind::AstPass(AstPass::StdImports) {
2181-
return Vec::new();
2182-
}
2183-
2184-
let (doc_meta_item, please_inline) = self.attrs.lists(sym::doc).get_word_attr(sym::inline);
2185-
let pub_underscore = self.vis.node.is_pub() && self.name == kw::Underscore;
2186-
2187-
if pub_underscore && please_inline {
2188-
rustc_errors::struct_span_err!(
2189-
cx.tcx.sess,
2190-
doc_meta_item.unwrap().span(),
2191-
E0780,
2192-
"anonymous imports cannot be inlined"
2193-
)
2194-
.span_label(self.span, "anonymous import")
2195-
.emit();
2196-
}
2177+
fn clean_use_statement(
2178+
import: &hir::Item<'_>,
2179+
name: Symbol,
2180+
path: &hir::Path<'_>,
2181+
kind: hir::UseKind,
2182+
cx: &DocContext<'_>,
2183+
) -> Vec<Item> {
2184+
// We need this comparison because some imports (for std types for example)
2185+
// are "inserted" as well but directly by the compiler and they should not be
2186+
// taken into account.
2187+
if import.span.ctxt().outer_expn_data().kind == ExpnKind::AstPass(AstPass::StdImports) {
2188+
return Vec::new();
2189+
}
2190+
2191+
let (doc_meta_item, please_inline) = import.attrs.lists(sym::doc).get_word_attr(sym::inline);
2192+
let pub_underscore = import.vis.node.is_pub() && name == kw::Underscore;
2193+
2194+
if pub_underscore && please_inline {
2195+
rustc_errors::struct_span_err!(
2196+
cx.tcx.sess,
2197+
doc_meta_item.unwrap().span(),
2198+
E0780,
2199+
"anonymous imports cannot be inlined"
2200+
)
2201+
.span_label(import.span, "anonymous import")
2202+
.emit();
2203+
}
21972204

2198-
// We consider inlining the documentation of `pub use` statements, but we
2199-
// forcefully don't inline if this is not public or if the
2200-
// #[doc(no_inline)] attribute is present.
2201-
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
2202-
let mut denied = !self.vis.node.is_pub()
2203-
|| pub_underscore
2204-
|| self.attrs.iter().any(|a| {
2205-
a.has_name(sym::doc)
2206-
&& match a.meta_item_list() {
2207-
Some(l) => {
2208-
attr::list_contains_name(&l, sym::no_inline)
2209-
|| attr::list_contains_name(&l, sym::hidden)
2210-
}
2211-
None => false,
2205+
// We consider inlining the documentation of `pub use` statements, but we
2206+
// forcefully don't inline if this is not public or if the
2207+
// #[doc(no_inline)] attribute is present.
2208+
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
2209+
let mut denied = !import.vis.node.is_pub()
2210+
|| pub_underscore
2211+
|| import.attrs.iter().any(|a| {
2212+
a.has_name(sym::doc)
2213+
&& match a.meta_item_list() {
2214+
Some(l) => {
2215+
attr::list_contains_name(&l, sym::no_inline)
2216+
|| attr::list_contains_name(&l, sym::hidden)
22122217
}
2213-
});
2214-
// Also check whether imports were asked to be inlined, in case we're trying to re-export a
2215-
// crate in Rust 2018+
2216-
let path = self.path.clean(cx);
2217-
let inner = if self.glob {
2218-
if !denied {
2219-
let mut visited = FxHashSet::default();
2220-
if let Some(items) = inline::try_inline_glob(cx, path.res, &mut visited) {
2221-
return items;
2218+
None => false,
22222219
}
2220+
});
2221+
2222+
// Also check whether imports were asked to be inlined, in case we're trying to re-export a
2223+
// crate in Rust 2018+
2224+
let def_id = cx.tcx.hir().local_def_id(import.hir_id).to_def_id();
2225+
let path = path.clean(cx);
2226+
let inner = if kind == hir::UseKind::Glob {
2227+
if !denied {
2228+
let mut visited = FxHashSet::default();
2229+
if let Some(items) = inline::try_inline_glob(cx, path.res, &mut visited) {
2230+
return items;
22232231
}
2224-
Import::new_glob(resolve_use_source(cx, path), true)
2225-
} else {
2226-
let name = self.name;
2227-
if !please_inline {
2228-
if let Res::Def(DefKind::Mod, did) = path.res {
2229-
if !did.is_local() && did.index == CRATE_DEF_INDEX {
2230-
// if we're `pub use`ing an extern crate root, don't inline it unless we
2231-
// were specifically asked for it
2232-
denied = true;
2233-
}
2232+
}
2233+
Import::new_glob(resolve_use_source(cx, path), true)
2234+
} else {
2235+
if !please_inline {
2236+
if let Res::Def(DefKind::Mod, did) = path.res {
2237+
if !did.is_local() && did.index == CRATE_DEF_INDEX {
2238+
// if we're `pub use`ing an extern crate root, don't inline it unless we
2239+
// were specifically asked for it
2240+
denied = true;
22342241
}
22352242
}
2236-
if !denied {
2237-
let mut visited = FxHashSet::default();
2243+
}
2244+
if !denied {
2245+
let mut visited = FxHashSet::default();
22382246

2239-
if let Some(mut items) = inline::try_inline(
2247+
if let Some(mut items) = inline::try_inline(
2248+
cx,
2249+
cx.tcx.parent_module(import.hir_id).to_def_id(),
2250+
path.res,
2251+
name,
2252+
Some(import.attrs),
2253+
&mut visited,
2254+
) {
2255+
items.push(Item::from_def_id_and_parts(
2256+
def_id,
2257+
None,
2258+
ImportItem(Import::new_simple(name, resolve_use_source(cx, path), false)),
22402259
cx,
2241-
cx.tcx.parent_module(self.id).to_def_id(),
2242-
path.res,
2243-
name,
2244-
Some(self.attrs),
2245-
&mut visited,
2246-
) {
2247-
items.push(Item {
2248-
name: None,
2249-
attrs: box self.attrs.clean(cx),
2250-
source: self.span.clean(cx),
2251-
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
2252-
visibility: self.vis.clean(cx),
2253-
kind: box ImportItem(Import::new_simple(
2254-
self.name,
2255-
resolve_use_source(cx, path),
2256-
false,
2257-
)),
2258-
});
2259-
return items;
2260-
}
2260+
));
2261+
return items;
22612262
}
2262-
Import::new_simple(name, resolve_use_source(cx, path), true)
2263-
};
2263+
}
2264+
Import::new_simple(name, resolve_use_source(cx, path), true)
2265+
};
22642266

2265-
vec![Item {
2266-
name: None,
2267-
attrs: box self.attrs.clean(cx),
2268-
source: self.span.clean(cx),
2269-
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
2270-
visibility: self.vis.clean(cx),
2271-
kind: box ImportItem(inner),
2272-
}]
2273-
}
2267+
vec![Item::from_def_id_and_parts(def_id, None, ImportItem(inner), cx)]
22742268
}
22752269

22762270
impl Clean<Item> for (&hir::ForeignItem<'_>, Option<Symbol>) {

src/librustdoc/doctree.rs

-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//! manner (and with prettier names) before cleaning.
33
crate use self::StructType::*;
44

5-
use rustc_ast as ast;
65
use rustc_span::{self, Span, Symbol};
76

87
use rustc_hir as hir;
@@ -11,7 +10,6 @@ crate struct Module<'hir> {
1110
crate name: Option<Symbol>,
1211
crate where_outer: Span,
1312
crate where_inner: Span,
14-
crate imports: Vec<Import<'hir>>,
1513
crate mods: Vec<Module<'hir>>,
1614
crate id: hir::HirId,
1715
// (item, renamed)
@@ -28,7 +26,6 @@ impl Module<'hir> {
2826
id: hir::CRATE_HIR_ID,
2927
where_outer: rustc_span::DUMMY_SP,
3028
where_inner: rustc_span::DUMMY_SP,
31-
imports: Vec::new(),
3229
mods: Vec::new(),
3330
items: Vec::new(),
3431
foreigns: Vec::new(),
@@ -54,17 +51,6 @@ crate struct Variant<'hir> {
5451
crate def: &'hir hir::VariantData<'hir>,
5552
}
5653

57-
#[derive(Debug)]
58-
crate struct Import<'hir> {
59-
crate name: Symbol,
60-
crate id: hir::HirId,
61-
crate vis: &'hir hir::Visibility<'hir>,
62-
crate attrs: &'hir [ast::Attribute],
63-
crate path: &'hir hir::Path<'hir>,
64-
crate glob: bool,
65-
crate span: Span,
66-
}
67-
6854
crate fn struct_type_from_def(vdata: &hir::VariantData<'_>) -> StructType {
6955
match *vdata {
7056
hir::VariantData::Struct(..) => Plain,

src/librustdoc/visit_ast.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
316316
}
317317
}
318318

319-
om.imports.push(Import {
320-
name,
321-
id: item.hir_id,
322-
vis: &item.vis,
323-
attrs: &item.attrs,
324-
path,
325-
glob: is_glob,
326-
span: item.span,
327-
});
319+
om.items.push((item, renamed))
328320
}
329321
hir::ItemKind::Mod(ref m) => {
330322
om.mods.push(self.visit_mod_contents(

src/test/rustdoc-json/compare.py

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
# and then create `yourtest.expected` by stripping unnecessary details from `yourtest.json`. If
88
# you're on windows, replace `\` with `/`.
99

10+
# WARNING: The error messages produced by this may be misleading, in the case of list re-ordering
11+
# it may point to apparently unrelated keys.
12+
1013
import copy
1114
import sys
1215
import json

src/test/rustdoc-json/nested.expected

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
"inner": {
4242
"is_crate": false,
4343
"items": [
44-
"0:7",
45-
"0:4"
44+
"0:4",
45+
"0:7"
4646
]
4747
},
4848
"kind": "module",

0 commit comments

Comments
 (0)