Skip to content

Commit 1368e81

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 4a48651 + 31b17f5 commit 1368e81

File tree

5 files changed

+91
-116
lines changed

5 files changed

+91
-116
lines changed

Diff for: 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());
@@ -2015,7 +2014,7 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
20152014
ItemKind::Fn(ref sig, ref generics, body_id) => {
20162015
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
20172016
}
2018-
hir::ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
2017+
ItemKind::Trait(is_auto, unsafety, ref generics, ref bounds, ref item_ids) => {
20192018
let items = item_ids
20202019
.iter()
20212020
.map(|ti| cx.tcx.hir().trait_item(ti.id).clean(cx))
@@ -2034,6 +2033,9 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
20342033
ItemKind::ExternCrate(orig_name) => {
20352034
return clean_extern_crate(item, name, orig_name, cx);
20362035
}
2036+
ItemKind::Use(path, kind) => {
2037+
return clean_use_statement(item, name, path, kind, cx);
2038+
}
20372039
_ => unreachable!("not yet converted"),
20382040
};
20392041

@@ -2155,105 +2157,97 @@ fn clean_extern_crate(
21552157
}]
21562158
}
21572159

2158-
impl Clean<Vec<Item>> for doctree::Import<'_> {
2159-
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
2160-
// We need this comparison because some imports (for std types for example)
2161-
// are "inserted" as well but directly by the compiler and they should not be
2162-
// taken into account.
2163-
if self.span.ctxt().outer_expn_data().kind == ExpnKind::AstPass(AstPass::StdImports) {
2164-
return Vec::new();
2165-
}
2166-
2167-
let (doc_meta_item, please_inline) = self.attrs.lists(sym::doc).get_word_attr(sym::inline);
2168-
let pub_underscore = self.vis.node.is_pub() && self.name == kw::Underscore;
2169-
2170-
if pub_underscore && please_inline {
2171-
rustc_errors::struct_span_err!(
2172-
cx.tcx.sess,
2173-
doc_meta_item.unwrap().span(),
2174-
E0780,
2175-
"anonymous imports cannot be inlined"
2176-
)
2177-
.span_label(self.span, "anonymous import")
2178-
.emit();
2179-
}
2160+
fn clean_use_statement(
2161+
import: &hir::Item<'_>,
2162+
name: Symbol,
2163+
path: &hir::Path<'_>,
2164+
kind: hir::UseKind,
2165+
cx: &DocContext<'_>,
2166+
) -> Vec<Item> {
2167+
// We need this comparison because some imports (for std types for example)
2168+
// are "inserted" as well but directly by the compiler and they should not be
2169+
// taken into account.
2170+
if import.span.ctxt().outer_expn_data().kind == ExpnKind::AstPass(AstPass::StdImports) {
2171+
return Vec::new();
2172+
}
2173+
2174+
let (doc_meta_item, please_inline) = import.attrs.lists(sym::doc).get_word_attr(sym::inline);
2175+
let pub_underscore = import.vis.node.is_pub() && name == kw::Underscore;
2176+
2177+
if pub_underscore && please_inline {
2178+
rustc_errors::struct_span_err!(
2179+
cx.tcx.sess,
2180+
doc_meta_item.unwrap().span(),
2181+
E0780,
2182+
"anonymous imports cannot be inlined"
2183+
)
2184+
.span_label(import.span, "anonymous import")
2185+
.emit();
2186+
}
21802187

2181-
// We consider inlining the documentation of `pub use` statements, but we
2182-
// forcefully don't inline if this is not public or if the
2183-
// #[doc(no_inline)] attribute is present.
2184-
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
2185-
let mut denied = !self.vis.node.is_pub()
2186-
|| pub_underscore
2187-
|| self.attrs.iter().any(|a| {
2188-
a.has_name(sym::doc)
2189-
&& match a.meta_item_list() {
2190-
Some(l) => {
2191-
attr::list_contains_name(&l, sym::no_inline)
2192-
|| attr::list_contains_name(&l, sym::hidden)
2193-
}
2194-
None => false,
2188+
// We consider inlining the documentation of `pub use` statements, but we
2189+
// forcefully don't inline if this is not public or if the
2190+
// #[doc(no_inline)] attribute is present.
2191+
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
2192+
let mut denied = !import.vis.node.is_pub()
2193+
|| pub_underscore
2194+
|| import.attrs.iter().any(|a| {
2195+
a.has_name(sym::doc)
2196+
&& match a.meta_item_list() {
2197+
Some(l) => {
2198+
attr::list_contains_name(&l, sym::no_inline)
2199+
|| attr::list_contains_name(&l, sym::hidden)
21952200
}
2196-
});
2197-
// Also check whether imports were asked to be inlined, in case we're trying to re-export a
2198-
// crate in Rust 2018+
2199-
let path = self.path.clean(cx);
2200-
let inner = if self.glob {
2201-
if !denied {
2202-
let mut visited = FxHashSet::default();
2203-
if let Some(items) = inline::try_inline_glob(cx, path.res, &mut visited) {
2204-
return items;
2201+
None => false,
22052202
}
2203+
});
2204+
2205+
// Also check whether imports were asked to be inlined, in case we're trying to re-export a
2206+
// crate in Rust 2018+
2207+
let def_id = cx.tcx.hir().local_def_id(import.hir_id).to_def_id();
2208+
let path = path.clean(cx);
2209+
let inner = if kind == hir::UseKind::Glob {
2210+
if !denied {
2211+
let mut visited = FxHashSet::default();
2212+
if let Some(items) = inline::try_inline_glob(cx, path.res, &mut visited) {
2213+
return items;
22062214
}
2207-
Import::new_glob(resolve_use_source(cx, path), true)
2208-
} else {
2209-
let name = self.name;
2210-
if !please_inline {
2211-
if let Res::Def(DefKind::Mod, did) = path.res {
2212-
if !did.is_local() && did.index == CRATE_DEF_INDEX {
2213-
// if we're `pub use`ing an extern crate root, don't inline it unless we
2214-
// were specifically asked for it
2215-
denied = true;
2216-
}
2215+
}
2216+
Import::new_glob(resolve_use_source(cx, path), true)
2217+
} else {
2218+
if !please_inline {
2219+
if let Res::Def(DefKind::Mod, did) = path.res {
2220+
if !did.is_local() && did.index == CRATE_DEF_INDEX {
2221+
// if we're `pub use`ing an extern crate root, don't inline it unless we
2222+
// were specifically asked for it
2223+
denied = true;
22172224
}
22182225
}
2219-
if !denied {
2220-
let mut visited = FxHashSet::default();
2226+
}
2227+
if !denied {
2228+
let mut visited = FxHashSet::default();
22212229

2222-
if let Some(mut items) = inline::try_inline(
2230+
if let Some(mut items) = inline::try_inline(
2231+
cx,
2232+
cx.tcx.parent_module(import.hir_id).to_def_id(),
2233+
path.res,
2234+
name,
2235+
Some(import.attrs),
2236+
&mut visited,
2237+
) {
2238+
items.push(Item::from_def_id_and_parts(
2239+
def_id,
2240+
None,
2241+
ImportItem(Import::new_simple(name, resolve_use_source(cx, path), false)),
22232242
cx,
2224-
cx.tcx.parent_module(self.id).to_def_id(),
2225-
path.res,
2226-
name,
2227-
Some(self.attrs),
2228-
&mut visited,
2229-
) {
2230-
items.push(Item {
2231-
name: None,
2232-
attrs: box self.attrs.clean(cx),
2233-
source: self.span.clean(cx),
2234-
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
2235-
visibility: self.vis.clean(cx),
2236-
kind: box ImportItem(Import::new_simple(
2237-
self.name,
2238-
resolve_use_source(cx, path),
2239-
false,
2240-
)),
2241-
});
2242-
return items;
2243-
}
2243+
));
2244+
return items;
22442245
}
2245-
Import::new_simple(name, resolve_use_source(cx, path), true)
2246-
};
2246+
}
2247+
Import::new_simple(name, resolve_use_source(cx, path), true)
2248+
};
22472249

2248-
vec![Item {
2249-
name: None,
2250-
attrs: box self.attrs.clean(cx),
2251-
source: self.span.clean(cx),
2252-
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
2253-
visibility: self.vis.clean(cx),
2254-
kind: box ImportItem(inner),
2255-
}]
2256-
}
2250+
vec![Item::from_def_id_and_parts(def_id, None, ImportItem(inner), cx)]
22572251
}
22582252

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

Diff for: 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(),
@@ -48,17 +45,6 @@ crate enum StructType {
4845
Unit,
4946
}
5047

51-
#[derive(Debug)]
52-
crate struct Import<'hir> {
53-
crate name: Symbol,
54-
crate id: hir::HirId,
55-
crate vis: &'hir hir::Visibility<'hir>,
56-
crate attrs: &'hir [ast::Attribute],
57-
crate path: &'hir hir::Path<'hir>,
58-
crate glob: bool,
59-
crate span: Span,
60-
}
61-
6248
crate fn struct_type_from_def(vdata: &hir::VariantData<'_>) -> StructType {
6349
match *vdata {
6450
hir::VariantData::Struct(..) => Plain,

Diff for: 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(

Diff for: 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

Diff for: 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)