Skip to content

Commit 0043fc9

Browse files
committed
Get rid of doctree::Impl
1 parent e280ae8 commit 0043fc9

File tree

5 files changed

+60
-124
lines changed

5 files changed

+60
-124
lines changed

src/librustdoc/clean/mod.rs

+49-53
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,8 @@ impl Clean<Item> for doctree::Module<'_> {
234234
items.extend(self.fns.iter().map(|x| x.clean(cx)));
235235
items.extend(self.foreigns.iter().map(|x| x.clean(cx)));
236236
items.extend(self.mods.iter().map(|x| x.clean(cx)));
237-
items.extend(self.items.iter().map(|x| x.clean(cx)));
237+
items.extend(self.items.iter().map(|x| x.clean(cx)).flatten());
238238
items.extend(self.traits.iter().map(|x| x.clean(cx)));
239-
items.extend(self.impls.iter().flat_map(|x| x.clean(cx)));
240239
items.extend(self.macros.iter().map(|x| x.clean(cx)));
241240
items.extend(self.proc_macros.iter().map(|x| x.clean(cx)));
242241

@@ -1922,8 +1921,8 @@ impl Clean<BareFunctionDecl> for hir::BareFnTy<'_> {
19221921
}
19231922
}
19241923

1925-
impl Clean<Item> for (&hir::Item<'_>, Option<Ident>) {
1926-
fn clean(&self, cx: &DocContext<'_>) -> Item {
1924+
impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
1925+
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
19271926
use hir::ItemKind;
19281927

19291928
let (item, renamed) = self;
@@ -1977,10 +1976,11 @@ impl Clean<Item> for (&hir::Item<'_>, Option<Ident>) {
19771976
fields: variant_data.fields().clean(cx),
19781977
fields_stripped: false,
19791978
}),
1979+
ItemKind::Impl { .. } => return clean_impl(item, cx),
19801980
_ => unreachable!("not yet converted"),
19811981
};
19821982

1983-
Item::from_def_id_and_parts(def_id, Some(name), kind, cx)
1983+
vec![Item::from_def_id_and_parts(def_id, Some(name), kind, cx)]
19841984
}
19851985
}
19861986

@@ -2005,57 +2005,53 @@ impl Clean<ImplPolarity> for ty::ImplPolarity {
20052005
}
20062006
}
20072007

2008-
impl Clean<Vec<Item>> for doctree::Impl<'_> {
2009-
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
2010-
let mut ret = Vec::new();
2011-
let trait_ = self.trait_.clean(cx);
2012-
let items = self.items.iter().map(|ii| ii.clean(cx)).collect::<Vec<_>>();
2013-
let def_id = cx.tcx.hir().local_def_id(self.id);
2014-
2015-
// If this impl block is an implementation of the Deref trait, then we
2016-
// need to try inlining the target's inherent impl blocks as well.
2017-
if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
2018-
build_deref_target_impls(cx, &items, &mut ret);
2008+
fn clean_impl(impl_: &hir::Item<'_>, cx: &DocContext<'_>) -> Vec<Item> {
2009+
let mut ret = Vec::new();
2010+
let (trait_, items, for_, unsafety, generics) = match &impl_.kind {
2011+
hir::ItemKind::Impl { of_trait, items, self_ty, unsafety, generics, .. } => {
2012+
(of_trait, items, self_ty, *unsafety, generics)
20192013
}
2020-
2021-
let provided: FxHashSet<String> = trait_
2022-
.def_id()
2023-
.map(|did| {
2024-
cx.tcx.provided_trait_methods(did).map(|meth| meth.ident.to_string()).collect()
2025-
})
2026-
.unwrap_or_default();
2027-
2028-
let for_ = self.for_.clean(cx);
2029-
let type_alias = for_.def_id().and_then(|did| match cx.tcx.def_kind(did) {
2030-
DefKind::TyAlias => Some(cx.tcx.type_of(did).clean(cx)),
2031-
_ => None,
2014+
_ => unreachable!(),
2015+
};
2016+
let trait_ = trait_.clean(cx);
2017+
let items = items.iter().map(|ii| cx.tcx.hir().impl_item(ii.id).clean(cx)).collect::<Vec<_>>();
2018+
let def_id = cx.tcx.hir().local_def_id(impl_.hir_id);
2019+
2020+
// If this impl block is an implementation of the Deref trait, then we
2021+
// need to try inlining the target's inherent impl blocks as well.
2022+
if trait_.def_id() == cx.tcx.lang_items().deref_trait() {
2023+
build_deref_target_impls(cx, &items, &mut ret);
2024+
}
2025+
2026+
let provided: FxHashSet<String> = trait_
2027+
.def_id()
2028+
.map(|did| cx.tcx.provided_trait_methods(did).map(|meth| meth.ident.to_string()).collect())
2029+
.unwrap_or_default();
2030+
2031+
let for_ = for_.clean(cx);
2032+
let type_alias = for_.def_id().and_then(|did| match cx.tcx.def_kind(did) {
2033+
DefKind::TyAlias => Some(cx.tcx.type_of(did).clean(cx)),
2034+
_ => None,
2035+
});
2036+
let make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| {
2037+
let kind = ImplItem(Impl {
2038+
unsafety,
2039+
generics: generics.clean(cx),
2040+
provided_trait_methods: provided.clone(),
2041+
trait_,
2042+
for_,
2043+
items,
2044+
polarity: Some(cx.tcx.impl_polarity(def_id).clean(cx)),
2045+
synthetic: false,
2046+
blanket_impl: None,
20322047
});
2033-
let make_item = |trait_: Option<Type>, for_: Type, items: Vec<Item>| Item {
2034-
name: None,
2035-
attrs: self.attrs.clean(cx),
2036-
source: self.span.clean(cx),
2037-
def_id: def_id.to_def_id(),
2038-
visibility: self.vis.clean(cx),
2039-
stability: cx.stability(self.id),
2040-
deprecation: cx.deprecation(self.id).clean(cx),
2041-
kind: ImplItem(Impl {
2042-
unsafety: self.unsafety,
2043-
generics: self.generics.clean(cx),
2044-
provided_trait_methods: provided.clone(),
2045-
trait_,
2046-
for_,
2047-
items,
2048-
polarity: Some(cx.tcx.impl_polarity(def_id).clean(cx)),
2049-
synthetic: false,
2050-
blanket_impl: None,
2051-
}),
2052-
};
2053-
if let Some(type_alias) = type_alias {
2054-
ret.push(make_item(trait_.clone(), type_alias, items.clone()));
2055-
}
2056-
ret.push(make_item(trait_, for_, items));
2057-
ret
2048+
Item::from_hir_id_and_parts(impl_.hir_id, None, kind, cx)
2049+
};
2050+
if let Some(type_alias) = type_alias {
2051+
ret.push(make_item(trait_.clone(), type_alias, items.clone()));
20582052
}
2053+
ret.push(make_item(trait_, for_, items));
2054+
ret
20592055
}
20602056

20612057
impl Clean<Vec<Item>> for doctree::ExternCrate<'_> {

src/librustdoc/core.rs

-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use rustc_attr as attr;
21
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
32
use rustc_data_structures::sync::{self, Lrc};
43
use rustc_driver::abort_on_err;
@@ -156,21 +155,6 @@ impl<'tcx> DocContext<'tcx> {
156155
def_id.as_local().map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id))
157156
}
158157
}
159-
160-
crate fn stability(&self, id: HirId) -> Option<attr::Stability> {
161-
self.tcx
162-
.hir()
163-
.opt_local_def_id(id)
164-
.and_then(|def_id| self.tcx.lookup_stability(def_id.to_def_id()))
165-
.cloned()
166-
}
167-
168-
crate fn deprecation(&self, id: HirId) -> Option<attr::Deprecation> {
169-
self.tcx
170-
.hir()
171-
.opt_local_def_id(id)
172-
.and_then(|def_id| self.tcx.lookup_deprecation(def_id.to_def_id()))
173-
}
174158
}
175159

176160
/// Creates a new diagnostic `Handler` that can be used to emit warnings and errors.

src/librustdoc/doctree.rs

-18
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ crate struct Module<'hir> {
2323
// (item, renamed)
2424
crate items: Vec<(&'hir hir::Item<'hir>, Option<Ident>)>,
2525
crate traits: Vec<Trait<'hir>>,
26-
crate impls: Vec<Impl<'hir>>,
2726
crate foreigns: Vec<ForeignItem<'hir>>,
2827
crate macros: Vec<Macro>,
2928
crate proc_macros: Vec<ProcMacro>,
@@ -44,7 +43,6 @@ impl Module<'hir> {
4443
mods: Vec::new(),
4544
items: Vec::new(),
4645
traits: Vec::new(),
47-
impls: Vec::new(),
4846
foreigns: Vec::new(),
4947
macros: Vec::new(),
5048
proc_macros: Vec::new(),
@@ -89,22 +87,6 @@ crate struct Trait<'hir> {
8987
crate id: hir::HirId,
9088
}
9189

92-
#[derive(Debug)]
93-
crate struct Impl<'hir> {
94-
crate unsafety: hir::Unsafety,
95-
crate polarity: hir::ImplPolarity,
96-
crate defaultness: hir::Defaultness,
97-
crate constness: hir::Constness,
98-
crate generics: &'hir hir::Generics<'hir>,
99-
crate trait_: &'hir Option<hir::TraitRef<'hir>>,
100-
crate for_: &'hir hir::Ty<'hir>,
101-
crate items: Vec<&'hir hir::ImplItem<'hir>>,
102-
crate attrs: &'hir [ast::Attribute],
103-
crate span: Span,
104-
crate vis: &'hir hir::Visibility<'hir>,
105-
crate id: hir::HirId,
106-
}
107-
10890
crate struct ForeignItem<'hir> {
10991
crate id: hir::HirId,
11092
crate name: Symbol,

src/librustdoc/visit_ast.rs

+2-28
Original file line numberDiff line numberDiff line change
@@ -401,37 +401,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
401401
};
402402
om.traits.push(t);
403403
}
404-
hir::ItemKind::Impl {
405-
unsafety,
406-
polarity,
407-
defaultness,
408-
constness,
409-
defaultness_span: _,
410-
ref generics,
411-
ref of_trait,
412-
self_ty,
413-
ref items,
414-
} => {
404+
hir::ItemKind::Impl { ref of_trait, .. } => {
415405
// Don't duplicate impls when inlining or if it's implementing a trait, we'll pick
416406
// them up regardless of where they're located.
417407
if !self.inlining && of_trait.is_none() {
418-
let items =
419-
items.iter().map(|item| self.cx.tcx.hir().impl_item(item.id)).collect();
420-
let i = Impl {
421-
unsafety,
422-
polarity,
423-
defaultness,
424-
constness,
425-
generics,
426-
trait_: of_trait,
427-
for_: self_ty,
428-
items,
429-
attrs: &item.attrs,
430-
id: item.hir_id,
431-
span: item.span,
432-
vis: &item.vis,
433-
};
434-
om.impls.push(i);
408+
om.items.push((item, None));
435409
}
436410
}
437411
}

src/test/rustdoc-ui/intra-link-errors.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ LL | /// [S!]
106106
| this link resolves to the struct `S`, which is not in the macro namespace
107107
| help: to link to the struct, prefix with `struct@`: `struct@S`
108108

109+
error: unresolved link to `S::h`
110+
--> $DIR/intra-link-errors.rs:78:6
111+
|
112+
LL | /// [type@S::h]
113+
| ^^^^^^^^^
114+
| |
115+
| this link resolves to the associated function `h`, which is not in the type namespace
116+
| help: to link to the associated function, add parentheses: `S::h()`
117+
109118
error: unresolved link to `T::g`
110119
--> $DIR/intra-link-errors.rs:86:6
111120
|
@@ -121,15 +130,6 @@ error: unresolved link to `T::h`
121130
LL | /// [T::h!]
122131
| ^^^^^ the trait `T` has no macro named `h`
123132

124-
error: unresolved link to `S::h`
125-
--> $DIR/intra-link-errors.rs:78:6
126-
|
127-
LL | /// [type@S::h]
128-
| ^^^^^^^^^
129-
| |
130-
| this link resolves to the associated function `h`, which is not in the type namespace
131-
| help: to link to the associated function, add parentheses: `S::h()`
132-
133133
error: unresolved link to `m`
134134
--> $DIR/intra-link-errors.rs:98:6
135135
|

0 commit comments

Comments
 (0)