Skip to content

Commit f75bb0c

Browse files
Rollup merge of rust-lang#84464 - jyn514:type-kind, r=CraftSpider
rustdoc: Get rid of `clean::TypeKind` It does exactly the same thing as ItemType.
2 parents d3b4655 + 6c066ab commit f75bb0c

File tree

7 files changed

+94
-134
lines changed

7 files changed

+94
-134
lines changed

src/librustdoc/clean/inline.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_span::hygiene::MacroKind;
1515
use rustc_span::symbol::{kw, sym, Symbol};
1616
use rustc_span::Span;
1717

18-
use crate::clean::{self, Attributes, GetDefId, ToSource, TypeKind};
18+
use crate::clean::{self, Attributes, GetDefId, ToSource};
1919
use crate::core::DocContext;
2020
use crate::formats::item_type::ItemType;
2121

@@ -56,36 +56,36 @@ crate fn try_inline(
5656

5757
let kind = match res {
5858
Res::Def(DefKind::Trait, did) => {
59-
record_extern_fqn(cx, did, clean::TypeKind::Trait);
59+
record_extern_fqn(cx, did, ItemType::Trait);
6060
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
6161
clean::TraitItem(build_external_trait(cx, did))
6262
}
6363
Res::Def(DefKind::Fn, did) => {
64-
record_extern_fqn(cx, did, clean::TypeKind::Function);
64+
record_extern_fqn(cx, did, ItemType::Function);
6565
clean::FunctionItem(build_external_function(cx, did))
6666
}
6767
Res::Def(DefKind::Struct, did) => {
68-
record_extern_fqn(cx, did, clean::TypeKind::Struct);
68+
record_extern_fqn(cx, did, ItemType::Struct);
6969
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
7070
clean::StructItem(build_struct(cx, did))
7171
}
7272
Res::Def(DefKind::Union, did) => {
73-
record_extern_fqn(cx, did, clean::TypeKind::Union);
73+
record_extern_fqn(cx, did, ItemType::Union);
7474
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
7575
clean::UnionItem(build_union(cx, did))
7676
}
7777
Res::Def(DefKind::TyAlias, did) => {
78-
record_extern_fqn(cx, did, clean::TypeKind::Typedef);
78+
record_extern_fqn(cx, did, ItemType::Typedef);
7979
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
8080
clean::TypedefItem(build_type_alias(cx, did), false)
8181
}
8282
Res::Def(DefKind::Enum, did) => {
83-
record_extern_fqn(cx, did, clean::TypeKind::Enum);
83+
record_extern_fqn(cx, did, ItemType::Enum);
8484
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
8585
clean::EnumItem(build_enum(cx, did))
8686
}
8787
Res::Def(DefKind::ForeignTy, did) => {
88-
record_extern_fqn(cx, did, clean::TypeKind::Foreign);
88+
record_extern_fqn(cx, did, ItemType::ForeignType);
8989
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
9090
clean::ForeignTypeItem
9191
}
@@ -95,24 +95,24 @@ crate fn try_inline(
9595
// their constructors.
9696
Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => return Some(Vec::new()),
9797
Res::Def(DefKind::Mod, did) => {
98-
record_extern_fqn(cx, did, clean::TypeKind::Module);
98+
record_extern_fqn(cx, did, ItemType::Module);
9999
clean::ModuleItem(build_module(cx, did, visited))
100100
}
101101
Res::Def(DefKind::Static, did) => {
102-
record_extern_fqn(cx, did, clean::TypeKind::Static);
102+
record_extern_fqn(cx, did, ItemType::Static);
103103
clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did)))
104104
}
105105
Res::Def(DefKind::Const, did) => {
106-
record_extern_fqn(cx, did, clean::TypeKind::Const);
106+
record_extern_fqn(cx, did, ItemType::Constant);
107107
clean::ConstantItem(build_const(cx, did))
108108
}
109109
Res::Def(DefKind::Macro(kind), did) => {
110110
let mac = build_macro(cx, did, name);
111111

112112
let type_kind = match kind {
113-
MacroKind::Bang => TypeKind::Macro,
114-
MacroKind::Attr => TypeKind::Attr,
115-
MacroKind::Derive => TypeKind::Derive,
113+
MacroKind::Bang => ItemType::Macro,
114+
MacroKind::Attr => ItemType::ProcAttribute,
115+
MacroKind::Derive => ItemType::ProcDerive,
116116
};
117117
record_extern_fqn(cx, did, type_kind);
118118
mac
@@ -157,15 +157,15 @@ crate fn load_attrs<'hir>(cx: &DocContext<'hir>, did: DefId) -> Attrs<'hir> {
157157
///
158158
/// These names are used later on by HTML rendering to generate things like
159159
/// source links back to the original item.
160-
crate fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: clean::TypeKind) {
160+
crate fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: ItemType) {
161161
let crate_name = cx.tcx.crate_name(did.krate).to_string();
162162

163163
let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| {
164164
// extern blocks have an empty name
165165
let s = elem.data.to_string();
166166
if !s.is_empty() { Some(s) } else { None }
167167
});
168-
let fqn = if let clean::TypeKind::Macro = kind {
168+
let fqn = if let ItemType::Macro = kind {
169169
// Check to see if it is a macro 2.0 or built-in macro
170170
if matches!(
171171
cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())),

src/librustdoc/clean/mod.rs

+8-13
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use std::{mem, vec};
3636

3737
use crate::core::{self, DocContext, ImplTraitParam};
3838
use crate::doctree;
39+
use crate::formats::item_type::ItemType;
3940

4041
use utils::*;
4142

@@ -273,7 +274,7 @@ impl Clean<GenericBound> for hir::GenericBound<'_> {
273274
impl Clean<Type> for (ty::TraitRef<'_>, &[TypeBinding]) {
274275
fn clean(&self, cx: &mut DocContext<'_>) -> Type {
275276
let (trait_ref, bounds) = *self;
276-
inline::record_extern_fqn(cx, trait_ref.def_id, TypeKind::Trait);
277+
inline::record_extern_fqn(cx, trait_ref.def_id, ItemType::Trait);
277278
let path = external_path(
278279
cx,
279280
cx.tcx.item_name(trait_ref.def_id),
@@ -1028,12 +1029,6 @@ impl Clean<PolyTrait> for hir::PolyTraitRef<'_> {
10281029
}
10291030
}
10301031

1031-
impl Clean<TypeKind> for hir::def::DefKind {
1032-
fn clean(&self, _: &mut DocContext<'_>) -> TypeKind {
1033-
(*self).into()
1034-
}
1035-
}
1036-
10371032
impl Clean<Item> for hir::TraitItem<'_> {
10381033
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
10391034
let local_did = self.def_id.to_def_id();
@@ -1568,16 +1563,16 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
15681563
ty::Adt(def, substs) => {
15691564
let did = def.did;
15701565
let kind = match def.adt_kind() {
1571-
AdtKind::Struct => TypeKind::Struct,
1572-
AdtKind::Union => TypeKind::Union,
1573-
AdtKind::Enum => TypeKind::Enum,
1566+
AdtKind::Struct => ItemType::Struct,
1567+
AdtKind::Union => ItemType::Union,
1568+
AdtKind::Enum => ItemType::Enum,
15741569
};
15751570
inline::record_extern_fqn(cx, did, kind);
15761571
let path = external_path(cx, cx.tcx.item_name(did), None, false, vec![], substs);
15771572
ResolvedPath { path, param_names: None, did, is_generic: false }
15781573
}
15791574
ty::Foreign(did) => {
1580-
inline::record_extern_fqn(cx, did, TypeKind::Foreign);
1575+
inline::record_extern_fqn(cx, did, ItemType::ForeignType);
15811576
let path = external_path(
15821577
cx,
15831578
cx.tcx.item_name(did),
@@ -1602,7 +1597,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
16021597
_ => cx.tcx.intern_substs(&[]),
16031598
};
16041599

1605-
inline::record_extern_fqn(cx, did, TypeKind::Trait);
1600+
inline::record_extern_fqn(cx, did, ItemType::Trait);
16061601

16071602
let mut param_names = vec![];
16081603
if let Some(b) = reg.clean(cx) {
@@ -1612,7 +1607,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
16121607
let empty = cx.tcx.intern_substs(&[]);
16131608
let path =
16141609
external_path(cx, cx.tcx.item_name(did), Some(did), false, vec![], empty);
1615-
inline::record_extern_fqn(cx, did, TypeKind::Trait);
1610+
inline::record_extern_fqn(cx, did, ItemType::Trait);
16161611
let bound = GenericBound::TraitBound(
16171612
PolyTrait {
16181613
trait_: ResolvedPath {

src/librustdoc/clean/types.rs

+1-57
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ impl GenericBound {
976976
let did = cx.tcx.require_lang_item(LangItem::Sized, None);
977977
let empty = cx.tcx.intern_substs(&[]);
978978
let path = external_path(cx, cx.tcx.item_name(did), Some(did), false, vec![], empty);
979-
inline::record_extern_fqn(cx, did, TypeKind::Trait);
979+
inline::record_extern_fqn(cx, did, ItemType::Trait);
980980
GenericBound::TraitBound(
981981
PolyTrait {
982982
trait_: ResolvedPath { path, param_names: None, did, is_generic: false },
@@ -1315,62 +1315,6 @@ crate enum PrimitiveType {
13151315
Never,
13161316
}
13171317

1318-
#[derive(Clone, PartialEq, Eq, Hash, Copy, Debug)]
1319-
crate enum TypeKind {
1320-
Enum,
1321-
Function,
1322-
Module,
1323-
Const,
1324-
Static,
1325-
Struct,
1326-
Union,
1327-
Trait,
1328-
Typedef,
1329-
Foreign,
1330-
Macro,
1331-
Attr,
1332-
Derive,
1333-
TraitAlias,
1334-
Primitive,
1335-
}
1336-
1337-
impl From<hir::def::DefKind> for TypeKind {
1338-
fn from(other: hir::def::DefKind) -> Self {
1339-
match other {
1340-
hir::def::DefKind::Enum => Self::Enum,
1341-
hir::def::DefKind::Fn => Self::Function,
1342-
hir::def::DefKind::Mod => Self::Module,
1343-
hir::def::DefKind::Const => Self::Const,
1344-
hir::def::DefKind::Static => Self::Static,
1345-
hir::def::DefKind::Struct => Self::Struct,
1346-
hir::def::DefKind::Union => Self::Union,
1347-
hir::def::DefKind::Trait => Self::Trait,
1348-
hir::def::DefKind::TyAlias => Self::Typedef,
1349-
hir::def::DefKind::TraitAlias => Self::TraitAlias,
1350-
hir::def::DefKind::Macro(_) => Self::Macro,
1351-
hir::def::DefKind::ForeignTy
1352-
| hir::def::DefKind::Variant
1353-
| hir::def::DefKind::AssocTy
1354-
| hir::def::DefKind::TyParam
1355-
| hir::def::DefKind::ConstParam
1356-
| hir::def::DefKind::Ctor(..)
1357-
| hir::def::DefKind::AssocFn
1358-
| hir::def::DefKind::AssocConst
1359-
| hir::def::DefKind::ExternCrate
1360-
| hir::def::DefKind::Use
1361-
| hir::def::DefKind::ForeignMod
1362-
| hir::def::DefKind::AnonConst
1363-
| hir::def::DefKind::OpaqueTy
1364-
| hir::def::DefKind::Field
1365-
| hir::def::DefKind::LifetimeParam
1366-
| hir::def::DefKind::GlobalAsm
1367-
| hir::def::DefKind::Impl
1368-
| hir::def::DefKind::Closure
1369-
| hir::def::DefKind::Generator => Self::Foreign,
1370-
}
1371-
}
1372-
}
1373-
13741318
crate trait GetDefId {
13751319
/// Use this method to get the [`DefId`] of a [`clean`] AST node.
13761320
/// This will return [`None`] when called on a primitive [`clean::Type`].

src/librustdoc/clean/utils.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use crate::clean::blanket_impl::BlanketImplFinder;
33
use crate::clean::{
44
inline, Clean, Crate, ExternalCrate, Generic, GenericArg, GenericArgs, ImportSource, Item,
55
ItemKind, Lifetime, MacroKind, Path, PathSegment, Primitive, PrimitiveType, ResolvedPath, Type,
6-
TypeBinding, TypeKind,
6+
TypeBinding,
77
};
88
use crate::core::DocContext;
9+
use crate::formats::item_type::ItemType;
910

1011
use rustc_hir as hir;
1112
use rustc_hir::def::{DefKind, Res};
@@ -431,37 +432,37 @@ crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
431432
debug!("register_res({:?})", res);
432433

433434
let (did, kind) = match res {
434-
Res::Def(DefKind::Fn, i) => (i, TypeKind::Function),
435-
Res::Def(DefKind::TyAlias, i) => (i, TypeKind::Typedef),
436-
Res::Def(DefKind::Enum, i) => (i, TypeKind::Enum),
437-
Res::Def(DefKind::Trait, i) => (i, TypeKind::Trait),
435+
Res::Def(DefKind::Fn, i) => (i, ItemType::Function),
436+
Res::Def(DefKind::TyAlias, i) => (i, ItemType::Typedef),
437+
Res::Def(DefKind::Enum, i) => (i, ItemType::Enum),
438+
Res::Def(DefKind::Trait, i) => (i, ItemType::Trait),
438439
Res::Def(DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst, i) => {
439-
(cx.tcx.parent(i).unwrap(), TypeKind::Trait)
440+
(cx.tcx.parent(i).unwrap(), ItemType::Trait)
440441
}
441-
Res::Def(DefKind::Struct, i) => (i, TypeKind::Struct),
442-
Res::Def(DefKind::Union, i) => (i, TypeKind::Union),
443-
Res::Def(DefKind::Mod, i) => (i, TypeKind::Module),
444-
Res::Def(DefKind::ForeignTy, i) => (i, TypeKind::Foreign),
445-
Res::Def(DefKind::Const, i) => (i, TypeKind::Const),
446-
Res::Def(DefKind::Static, i) => (i, TypeKind::Static),
442+
Res::Def(DefKind::Struct, i) => (i, ItemType::Struct),
443+
Res::Def(DefKind::Union, i) => (i, ItemType::Union),
444+
Res::Def(DefKind::Mod, i) => (i, ItemType::Module),
445+
Res::Def(DefKind::ForeignTy, i) => (i, ItemType::ForeignType),
446+
Res::Def(DefKind::Const, i) => (i, ItemType::Constant),
447+
Res::Def(DefKind::Static, i) => (i, ItemType::Static),
447448
Res::Def(DefKind::Variant, i) => {
448-
(cx.tcx.parent(i).expect("cannot get parent def id"), TypeKind::Enum)
449+
(cx.tcx.parent(i).expect("cannot get parent def id"), ItemType::Enum)
449450
}
450451
Res::Def(DefKind::Macro(mac_kind), i) => match mac_kind {
451-
MacroKind::Bang => (i, TypeKind::Macro),
452-
MacroKind::Attr => (i, TypeKind::Attr),
453-
MacroKind::Derive => (i, TypeKind::Derive),
452+
MacroKind::Bang => (i, ItemType::Macro),
453+
MacroKind::Attr => (i, ItemType::ProcAttribute),
454+
MacroKind::Derive => (i, ItemType::ProcDerive),
454455
},
455-
Res::Def(DefKind::TraitAlias, i) => (i, TypeKind::TraitAlias),
456-
Res::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
456+
Res::Def(DefKind::TraitAlias, i) => (i, ItemType::TraitAlias),
457+
Res::SelfTy(Some(def_id), _) => (def_id, ItemType::Trait),
457458
Res::SelfTy(_, Some((impl_def_id, _))) => return impl_def_id,
458459
_ => return res.def_id(),
459460
};
460461
if did.is_local() {
461462
return did;
462463
}
463464
inline::record_extern_fqn(cx, did, kind);
464-
if let TypeKind::Trait = kind {
465+
if let ItemType::Trait = kind {
465466
inline::record_extern_trait(cx, did);
466467
}
467468
did

src/librustdoc/formats/item_type.rs

+39-19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::fmt;
44

55
use serde::{Serialize, Serializer};
66

7+
use rustc_hir::def::DefKind;
78
use rustc_span::hygiene::MacroKind;
89

910
use crate::clean;
@@ -19,7 +20,7 @@ use crate::clean;
1920
/// module headings. If you are adding to this enum and want to ensure that the sidebar also prints
2021
/// a heading, edit the listing in `html/render.rs`, function `sidebar_module`. This uses an
2122
/// ordering based on a helper function inside `item_module`, in the same file.
22-
#[derive(Copy, PartialEq, Eq, Clone, Debug, PartialOrd, Ord)]
23+
#[derive(Copy, PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)]
2324
crate enum ItemType {
2425
Module = 0,
2526
ExternCrate = 1,
@@ -102,24 +103,43 @@ impl<'a> From<&'a clean::Item> for ItemType {
102103
}
103104
}
104105

105-
impl From<clean::TypeKind> for ItemType {
106-
fn from(kind: clean::TypeKind) -> ItemType {
107-
match kind {
108-
clean::TypeKind::Struct => ItemType::Struct,
109-
clean::TypeKind::Union => ItemType::Union,
110-
clean::TypeKind::Enum => ItemType::Enum,
111-
clean::TypeKind::Function => ItemType::Function,
112-
clean::TypeKind::Trait => ItemType::Trait,
113-
clean::TypeKind::Module => ItemType::Module,
114-
clean::TypeKind::Static => ItemType::Static,
115-
clean::TypeKind::Const => ItemType::Constant,
116-
clean::TypeKind::Typedef => ItemType::Typedef,
117-
clean::TypeKind::Foreign => ItemType::ForeignType,
118-
clean::TypeKind::Macro => ItemType::Macro,
119-
clean::TypeKind::Attr => ItemType::ProcAttribute,
120-
clean::TypeKind::Derive => ItemType::ProcDerive,
121-
clean::TypeKind::TraitAlias => ItemType::TraitAlias,
122-
clean::TypeKind::Primitive => ItemType::Primitive,
106+
impl From<DefKind> for ItemType {
107+
fn from(other: DefKind) -> Self {
108+
match other {
109+
DefKind::Enum => Self::Enum,
110+
DefKind::Fn => Self::Function,
111+
DefKind::Mod => Self::Module,
112+
DefKind::Const => Self::Constant,
113+
DefKind::Static => Self::Static,
114+
DefKind::Struct => Self::Struct,
115+
DefKind::Union => Self::Union,
116+
DefKind::Trait => Self::Trait,
117+
DefKind::TyAlias => Self::Typedef,
118+
DefKind::TraitAlias => Self::TraitAlias,
119+
DefKind::Macro(kind) => match kind {
120+
MacroKind::Bang => ItemType::Macro,
121+
MacroKind::Attr => ItemType::ProcAttribute,
122+
MacroKind::Derive => ItemType::ProcDerive,
123+
},
124+
DefKind::ForeignTy
125+
| DefKind::Variant
126+
| DefKind::AssocTy
127+
| DefKind::TyParam
128+
| DefKind::ConstParam
129+
| DefKind::Ctor(..)
130+
| DefKind::AssocFn
131+
| DefKind::AssocConst
132+
| DefKind::ExternCrate
133+
| DefKind::Use
134+
| DefKind::ForeignMod
135+
| DefKind::AnonConst
136+
| DefKind::OpaqueTy
137+
| DefKind::Field
138+
| DefKind::LifetimeParam
139+
| DefKind::GlobalAsm
140+
| DefKind::Impl
141+
| DefKind::Closure
142+
| DefKind::Generator => Self::ForeignType,
123143
}
124144
}
125145
}

0 commit comments

Comments
 (0)