Skip to content

Commit 6a88957

Browse files
committed
Auto merge of rust-lang#79372 - jyn514:more-cleanup, r=GuillaumeGomez
Cleanup more of rustdoc - Use `Item::from_def_id` for StructField - Use `from_def_id_and_parts` for primitives and keywords - Take `String` instead of `Symbol` in `from_def_id` - this avoids having to intern then immediately stringify the existing string. - Remove unused `get_stability` and `get_deprecation` - Remove unused `attrs` field from `primitives` - Remove unused `attrs` field from `keywords` This will probably conflict with rust-lang#79335 and I would prefer for that PR to land first - I'm anxious for rust-lang#77467 to land :) Makes rust-lang#76998 easier to add. r? `@GuillaumeGomez`
2 parents 774bce7 + 09a3bc1 commit 6a88957

File tree

6 files changed

+49
-67
lines changed

6 files changed

+49
-67
lines changed

src/librustdoc/clean/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ crate fn try_inline(
124124
let attrs = merge_attrs(cx, Some(parent_module), target_attrs, attrs_clone);
125125

126126
cx.renderinfo.borrow_mut().inlined.insert(did);
127-
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name), kind, cx);
127+
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name.clean(cx)), kind, cx);
128128
ret.push(clean::Item { attrs, ..what_rustc_thinks });
129129
Some(ret)
130130
}

src/librustdoc/clean/mod.rs

+20-22
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl Clean<ExternalCrate> for CrateNum {
122122
}
123123
}
124124
}
125-
return prim.map(|p| (def_id, p, attrs));
125+
return prim.map(|p| (def_id, p));
126126
}
127127
None
128128
};
@@ -144,9 +144,9 @@ impl Clean<ExternalCrate> for CrateNum {
144144
hir::ItemKind::Use(ref path, hir::UseKind::Single)
145145
if item.vis.node.is_pub() =>
146146
{
147-
as_primitive(path.res).map(|(_, prim, attrs)| {
147+
as_primitive(path.res).map(|(_, prim)| {
148148
// Pretend the primitive is local.
149-
(cx.tcx.hir().local_def_id(id.id).to_def_id(), prim, attrs)
149+
(cx.tcx.hir().local_def_id(id.id).to_def_id(), prim)
150150
})
151151
}
152152
_ => None,
@@ -177,7 +177,7 @@ impl Clean<ExternalCrate> for CrateNum {
177177
}
178178
}
179179
}
180-
return keyword.map(|p| (def_id, p, attrs));
180+
return keyword.map(|p| (def_id, p));
181181
}
182182
None
183183
};
@@ -199,8 +199,8 @@ impl Clean<ExternalCrate> for CrateNum {
199199
hir::ItemKind::Use(ref path, hir::UseKind::Single)
200200
if item.vis.node.is_pub() =>
201201
{
202-
as_keyword(path.res).map(|(_, prim, attrs)| {
203-
(cx.tcx.hir().local_def_id(id.id).to_def_id(), prim, attrs)
202+
as_keyword(path.res).map(|(_, prim)| {
203+
(cx.tcx.hir().local_def_id(id.id).to_def_id(), prim)
204204
})
205205
}
206206
_ => None,
@@ -1099,7 +1099,7 @@ impl Clean<Item> for hir::TraitItem<'_> {
10991099
AssocTypeItem(bounds.clean(cx), default.clean(cx))
11001100
}
11011101
};
1102-
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
1102+
Item::from_def_id_and_parts(local_did, Some(self.ident.name.clean(cx)), inner, cx)
11031103
})
11041104
}
11051105
}
@@ -1127,7 +1127,7 @@ impl Clean<Item> for hir::ImplItem<'_> {
11271127
TypedefItem(Typedef { type_, generics: Generics::default(), item_type }, true)
11281128
}
11291129
};
1130-
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
1130+
Item::from_def_id_and_parts(local_did, Some(self.ident.name.clean(cx)), inner, cx)
11311131
})
11321132
}
11331133
}
@@ -1284,7 +1284,7 @@ impl Clean<Item> for ty::AssocItem {
12841284
}
12851285
};
12861286

1287-
Item::from_def_id_and_parts(self.def_id, Some(self.ident.name), kind, cx)
1287+
Item::from_def_id_and_parts(self.def_id, Some(self.ident.name.clean(cx)), kind, cx)
12881288
}
12891289
}
12901290

@@ -1769,7 +1769,7 @@ impl Clean<Item> for ty::FieldDef {
17691769
fn clean(&self, cx: &DocContext<'_>) -> Item {
17701770
let what_rustc_thinks = Item::from_def_id_and_parts(
17711771
self.did,
1772-
Some(self.ident.name),
1772+
Some(self.ident.name.clean(cx)),
17731773
StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
17741774
cx,
17751775
);
@@ -1844,22 +1844,20 @@ impl Clean<Item> for ty::VariantDef {
18441844
fields: self
18451845
.fields
18461846
.iter()
1847-
.map(|field| Item {
1848-
source: cx.tcx.def_span(field.did).clean(cx),
1849-
name: Some(field.ident.name.clean(cx)),
1850-
attrs: cx.tcx.get_attrs(field.did).clean(cx),
1851-
visibility: Visibility::Inherited,
1852-
def_id: field.did,
1853-
stability: get_stability(cx, field.did),
1854-
deprecation: get_deprecation(cx, field.did),
1855-
kind: StructFieldItem(cx.tcx.type_of(field.did).clean(cx)),
1847+
.map(|field| {
1848+
let name = Some(field.ident.name.clean(cx));
1849+
let kind = StructFieldItem(cx.tcx.type_of(field.did).clean(cx));
1850+
let what_rustc_thinks =
1851+
Item::from_def_id_and_parts(field.did, name, kind, cx);
1852+
// don't show `pub` for fields, which are always public
1853+
Item { visibility: Visibility::Inherited, ..what_rustc_thinks }
18561854
})
18571855
.collect(),
18581856
}),
18591857
};
18601858
let what_rustc_thinks = Item::from_def_id_and_parts(
18611859
self.def_id,
1862-
Some(self.ident.name),
1860+
Some(self.ident.name.clean(cx)),
18631861
VariantItem(Variant { kind }),
18641862
cx,
18651863
);
@@ -2057,7 +2055,7 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Ident>) {
20572055
_ => unreachable!("not yet converted"),
20582056
};
20592057

2060-
vec![Item::from_def_id_and_parts(def_id, Some(name), kind, cx)]
2058+
vec![Item::from_def_id_and_parts(def_id, Some(name.clean(cx)), kind, cx)]
20612059
})
20622060
}
20632061
}
@@ -2319,7 +2317,7 @@ impl Clean<Item> for doctree::Macro {
23192317
fn clean(&self, cx: &DocContext<'_>) -> Item {
23202318
Item::from_def_id_and_parts(
23212319
self.def_id,
2322-
Some(self.name),
2320+
Some(self.name.clean(cx)),
23232321
MacroItem(Macro {
23242322
source: format!(
23252323
"macro_rules! {} {{\n{}}}",

src/librustdoc/clean/types.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::clean::cfg::Cfg;
3434
use crate::clean::external_path;
3535
use crate::clean::inline;
3636
use crate::clean::types::Type::{QPath, ResolvedPath};
37+
use crate::clean::Clean;
3738
use crate::core::DocContext;
3839
use crate::doctree;
3940
use crate::formats::cache::cache;
@@ -54,7 +55,7 @@ crate struct Crate {
5455
crate src: FileName,
5556
crate module: Option<Item>,
5657
crate externs: Vec<(CrateNum, ExternalCrate)>,
57-
crate primitives: Vec<(DefId, PrimitiveType, Attributes)>,
58+
crate primitives: Vec<(DefId, PrimitiveType)>,
5859
// These are later on moved into `CACHEKEY`, leaving the map empty.
5960
// Only here so that they can be filtered through the rustdoc passes.
6061
crate external_traits: Rc<RefCell<FxHashMap<DefId, Trait>>>,
@@ -67,8 +68,8 @@ crate struct ExternalCrate {
6768
crate name: String,
6869
crate src: FileName,
6970
crate attrs: Attributes,
70-
crate primitives: Vec<(DefId, PrimitiveType, Attributes)>,
71-
crate keywords: Vec<(DefId, String, Attributes)>,
71+
crate primitives: Vec<(DefId, PrimitiveType)>,
72+
crate keywords: Vec<(DefId, String)>,
7273
}
7374

7475
/// Anything with a source location and set of attributes and, optionally, a
@@ -120,17 +121,20 @@ impl Item {
120121
kind: ItemKind,
121122
cx: &DocContext<'_>,
122123
) -> Item {
123-
Item::from_def_id_and_parts(cx.tcx.hir().local_def_id(hir_id).to_def_id(), name, kind, cx)
124+
Item::from_def_id_and_parts(
125+
cx.tcx.hir().local_def_id(hir_id).to_def_id(),
126+
name.clean(cx),
127+
kind,
128+
cx,
129+
)
124130
}
125131

126132
pub fn from_def_id_and_parts(
127133
def_id: DefId,
128-
name: Option<Symbol>,
134+
name: Option<String>,
129135
kind: ItemKind,
130136
cx: &DocContext<'_>,
131137
) -> Item {
132-
use super::Clean;
133-
134138
debug!("name={:?}, def_id={:?}", name, def_id);
135139

136140
// `span_if_local()` lies about functions and only gives the span of the function signature
@@ -145,7 +149,7 @@ impl Item {
145149
Item {
146150
def_id,
147151
kind,
148-
name: name.clean(cx),
152+
name,
149153
source: source.clean(cx),
150154
attrs: cx.tcx.get_attrs(def_id).clean(cx),
151155
visibility: cx.tcx.visibility(def_id).clean(cx),

src/librustdoc/clean/utils.rs

+13-32
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use crate::clean::auto_trait::AutoTraitFinder;
22
use crate::clean::blanket_impl::BlanketImplFinder;
33
use crate::clean::{
4-
inline, Clean, Crate, Deprecation, ExternalCrate, FnDecl, FnRetTy, Generic, GenericArg,
5-
GenericArgs, GenericBound, Generics, GetDefId, ImportSource, Item, ItemKind, Lifetime,
6-
MacroKind, Path, PathSegment, Primitive, PrimitiveType, ResolvedPath, Span, Type, TypeBinding,
7-
TypeKind, Visibility, WherePredicate,
4+
inline, Clean, Crate, ExternalCrate, FnDecl, FnRetTy, Generic, GenericArg, GenericArgs,
5+
GenericBound, Generics, GetDefId, ImportSource, Item, ItemKind, Lifetime, MacroKind, Path,
6+
PathSegment, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding, TypeKind,
7+
WherePredicate,
88
};
99
use crate::core::DocContext;
1010

1111
use itertools::Itertools;
12-
use rustc_attr::Stability;
1312
use rustc_data_structures::fx::FxHashSet;
1413
use rustc_hir as hir;
1514
use rustc_hir::def::{DefKind, Res};
@@ -66,25 +65,16 @@ crate fn krate(mut cx: &mut DocContext<'_>) -> Crate {
6665
ItemKind::ModuleItem(ref mut m) => m,
6766
_ => unreachable!(),
6867
};
69-
m.items.extend(primitives.iter().map(|&(def_id, prim, ref attrs)| Item {
70-
source: Span::empty(),
71-
name: Some(prim.to_url_str().to_string()),
72-
attrs: attrs.clone(),
73-
visibility: Visibility::Public,
74-
stability: get_stability(cx, def_id),
75-
deprecation: get_deprecation(cx, def_id),
76-
def_id,
77-
kind: ItemKind::PrimitiveItem(prim),
68+
m.items.extend(primitives.iter().map(|&(def_id, prim)| {
69+
Item::from_def_id_and_parts(
70+
def_id,
71+
Some(prim.to_url_str().to_owned()),
72+
ItemKind::PrimitiveItem(prim),
73+
cx,
74+
)
7875
}));
79-
m.items.extend(keywords.into_iter().map(|(def_id, kw, attrs)| Item {
80-
source: Span::empty(),
81-
name: Some(kw.clone()),
82-
attrs,
83-
visibility: Visibility::Public,
84-
stability: get_stability(cx, def_id),
85-
deprecation: get_deprecation(cx, def_id),
86-
def_id,
87-
kind: ItemKind::KeywordItem(kw),
76+
m.items.extend(keywords.into_iter().map(|(def_id, kw)| {
77+
Item::from_def_id_and_parts(def_id, Some(kw.clone()), ItemKind::KeywordItem(kw), cx)
8878
}));
8979
}
9080

@@ -101,15 +91,6 @@ crate fn krate(mut cx: &mut DocContext<'_>) -> Crate {
10191
}
10292
}
10393

104-
// extract the stability index for a node from tcx, if possible
105-
crate fn get_stability(cx: &DocContext<'_>, def_id: DefId) -> Option<Stability> {
106-
cx.tcx.lookup_stability(def_id).cloned()
107-
}
108-
109-
crate fn get_deprecation(cx: &DocContext<'_>, def_id: DefId) -> Option<Deprecation> {
110-
cx.tcx.lookup_deprecation(def_id).clean(cx)
111-
}
112-
11394
fn external_generic_args(
11495
cx: &DocContext<'_>,
11596
trait_did: Option<DefId>,

src/librustdoc/formats/cache.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ impl Cache {
187187
// Favor linking to as local extern as possible, so iterate all crates in
188188
// reverse topological order.
189189
for &(_, ref e) in krate.externs.iter().rev() {
190-
for &(def_id, prim, _) in &e.primitives {
190+
for &(def_id, prim) in &e.primitives {
191191
cache.primitive_locations.insert(prim, def_id);
192192
}
193193
}
194-
for &(def_id, prim, _) in &krate.primitives {
194+
for &(def_id, prim) in &krate.primitives {
195195
cache.primitive_locations.insert(prim, def_id);
196196
}
197197

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
+-------------------------------------+------------+------------+------------+------------+
22
| File | Documented | Percentage | Examples | Percentage |
33
+-------------------------------------+------------+------------+------------+------------+
4-
| ...st/rustdoc-ui/coverage/exotic.rs | 1 | 100.0% | 0 | 0.0% |
5-
| <anon> | 2 | 100.0% | 0 | 0.0% |
4+
| ...st/rustdoc-ui/coverage/exotic.rs | 3 | 100.0% | 0 | 0.0% |
65
+-------------------------------------+------------+------------+------------+------------+
76
| Total | 3 | 100.0% | 0 | 0.0% |
87
+-------------------------------------+------------+------------+------------+------------+

0 commit comments

Comments
 (0)