Skip to content

Commit e03cdb6

Browse files
authored
Rollup merge of #84464 - jyn514:type-kind, r=CraftSpider
rustdoc: Get rid of `clean::TypeKind` It does exactly the same thing as ItemType.
2 parents c2ce254 + ab54197 commit e03cdb6

File tree

7 files changed

+93
-134
lines changed

7 files changed

+93
-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

@@ -154,7 +155,7 @@ impl Clean<GenericBound> for hir::GenericBound<'_> {
154155
impl Clean<Type> for (ty::TraitRef<'_>, &[TypeBinding]) {
155156
fn clean(&self, cx: &mut DocContext<'_>) -> Type {
156157
let (trait_ref, bounds) = *self;
157-
inline::record_extern_fqn(cx, trait_ref.def_id, TypeKind::Trait);
158+
inline::record_extern_fqn(cx, trait_ref.def_id, ItemType::Trait);
158159
let path = external_path(
159160
cx,
160161
cx.tcx.item_name(trait_ref.def_id),
@@ -909,12 +910,6 @@ impl Clean<PolyTrait> for hir::PolyTraitRef<'_> {
909910
}
910911
}
911912

912-
impl Clean<TypeKind> for hir::def::DefKind {
913-
fn clean(&self, _: &mut DocContext<'_>) -> TypeKind {
914-
(*self).into()
915-
}
916-
}
917-
918913
impl Clean<Item> for hir::TraitItem<'_> {
919914
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
920915
let local_did = self.def_id.to_def_id();
@@ -1449,16 +1444,16 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
14491444
ty::Adt(def, substs) => {
14501445
let did = def.did;
14511446
let kind = match def.adt_kind() {
1452-
AdtKind::Struct => TypeKind::Struct,
1453-
AdtKind::Union => TypeKind::Union,
1454-
AdtKind::Enum => TypeKind::Enum,
1447+
AdtKind::Struct => ItemType::Struct,
1448+
AdtKind::Union => ItemType::Union,
1449+
AdtKind::Enum => ItemType::Enum,
14551450
};
14561451
inline::record_extern_fqn(cx, did, kind);
14571452
let path = external_path(cx, cx.tcx.item_name(did), None, false, vec![], substs);
14581453
ResolvedPath { path, param_names: None, did, is_generic: false }
14591454
}
14601455
ty::Foreign(did) => {
1461-
inline::record_extern_fqn(cx, did, TypeKind::Foreign);
1456+
inline::record_extern_fqn(cx, did, ItemType::ForeignType);
14621457
let path = external_path(
14631458
cx,
14641459
cx.tcx.item_name(did),
@@ -1483,7 +1478,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
14831478
_ => cx.tcx.intern_substs(&[]),
14841479
};
14851480

1486-
inline::record_extern_fqn(cx, did, TypeKind::Trait);
1481+
inline::record_extern_fqn(cx, did, ItemType::Trait);
14871482

14881483
let mut param_names = vec![];
14891484
if let Some(b) = reg.clean(cx) {
@@ -1493,7 +1488,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
14931488
let empty = cx.tcx.intern_substs(&[]);
14941489
let path =
14951490
external_path(cx, cx.tcx.item_name(did), Some(did), false, vec![], empty);
1496-
inline::record_extern_fqn(cx, did, TypeKind::Trait);
1491+
inline::record_extern_fqn(cx, did, ItemType::Trait);
14971492
let bound = GenericBound::TraitBound(
14981493
PolyTrait {
14991494
trait_: ResolvedPath {

src/librustdoc/clean/types.rs

+1-57
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ impl GenericBound {
10991099
let did = cx.tcx.require_lang_item(LangItem::Sized, None);
11001100
let empty = cx.tcx.intern_substs(&[]);
11011101
let path = external_path(cx, cx.tcx.item_name(did), Some(did), false, vec![], empty);
1102-
inline::record_extern_fqn(cx, did, TypeKind::Trait);
1102+
inline::record_extern_fqn(cx, did, ItemType::Trait);
11031103
GenericBound::TraitBound(
11041104
PolyTrait {
11051105
trait_: ResolvedPath { path, param_names: None, did, is_generic: false },
@@ -1438,62 +1438,6 @@ crate enum PrimitiveType {
14381438
Never,
14391439
}
14401440

1441-
#[derive(Clone, PartialEq, Eq, Hash, Copy, Debug)]
1442-
crate enum TypeKind {
1443-
Enum,
1444-
Function,
1445-
Module,
1446-
Const,
1447-
Static,
1448-
Struct,
1449-
Union,
1450-
Trait,
1451-
Typedef,
1452-
Foreign,
1453-
Macro,
1454-
Attr,
1455-
Derive,
1456-
TraitAlias,
1457-
Primitive,
1458-
}
1459-
1460-
impl From<hir::def::DefKind> for TypeKind {
1461-
fn from(other: hir::def::DefKind) -> Self {
1462-
match other {
1463-
hir::def::DefKind::Enum => Self::Enum,
1464-
hir::def::DefKind::Fn => Self::Function,
1465-
hir::def::DefKind::Mod => Self::Module,
1466-
hir::def::DefKind::Const => Self::Const,
1467-
hir::def::DefKind::Static => Self::Static,
1468-
hir::def::DefKind::Struct => Self::Struct,
1469-
hir::def::DefKind::Union => Self::Union,
1470-
hir::def::DefKind::Trait => Self::Trait,
1471-
hir::def::DefKind::TyAlias => Self::Typedef,
1472-
hir::def::DefKind::TraitAlias => Self::TraitAlias,
1473-
hir::def::DefKind::Macro(_) => Self::Macro,
1474-
hir::def::DefKind::ForeignTy
1475-
| hir::def::DefKind::Variant
1476-
| hir::def::DefKind::AssocTy
1477-
| hir::def::DefKind::TyParam
1478-
| hir::def::DefKind::ConstParam
1479-
| hir::def::DefKind::Ctor(..)
1480-
| hir::def::DefKind::AssocFn
1481-
| hir::def::DefKind::AssocConst
1482-
| hir::def::DefKind::ExternCrate
1483-
| hir::def::DefKind::Use
1484-
| hir::def::DefKind::ForeignMod
1485-
| hir::def::DefKind::AnonConst
1486-
| hir::def::DefKind::OpaqueTy
1487-
| hir::def::DefKind::Field
1488-
| hir::def::DefKind::LifetimeParam
1489-
| hir::def::DefKind::GlobalAsm
1490-
| hir::def::DefKind::Impl
1491-
| hir::def::DefKind::Closure
1492-
| hir::def::DefKind::Generator => Self::Foreign,
1493-
}
1494-
}
1495-
}
1496-
14971441
crate trait GetDefId {
14981442
/// Use this method to get the [`DefId`] of a [`clean`] AST node.
14991443
/// This will return [`None`] when called on a primitive [`clean::Type`].

src/librustdoc/clean/utils.rs

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

1010
use rustc_hir as hir;
1111
use rustc_hir::def::{DefKind, Res};
@@ -435,37 +435,37 @@ crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
435435
debug!("register_res({:?})", res);
436436

437437
let (did, kind) = match res {
438-
Res::Def(DefKind::Fn, i) => (i, TypeKind::Function),
439-
Res::Def(DefKind::TyAlias, i) => (i, TypeKind::Typedef),
440-
Res::Def(DefKind::Enum, i) => (i, TypeKind::Enum),
441-
Res::Def(DefKind::Trait, i) => (i, TypeKind::Trait),
438+
Res::Def(DefKind::Fn, i) => (i, ItemType::Function),
439+
Res::Def(DefKind::TyAlias, i) => (i, ItemType::Typedef),
440+
Res::Def(DefKind::Enum, i) => (i, ItemType::Enum),
441+
Res::Def(DefKind::Trait, i) => (i, ItemType::Trait),
442442
Res::Def(DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst, i) => {
443-
(cx.tcx.parent(i).unwrap(), TypeKind::Trait)
443+
(cx.tcx.parent(i).unwrap(), ItemType::Trait)
444444
}
445-
Res::Def(DefKind::Struct, i) => (i, TypeKind::Struct),
446-
Res::Def(DefKind::Union, i) => (i, TypeKind::Union),
447-
Res::Def(DefKind::Mod, i) => (i, TypeKind::Module),
448-
Res::Def(DefKind::ForeignTy, i) => (i, TypeKind::Foreign),
449-
Res::Def(DefKind::Const, i) => (i, TypeKind::Const),
450-
Res::Def(DefKind::Static, i) => (i, TypeKind::Static),
445+
Res::Def(DefKind::Struct, i) => (i, ItemType::Struct),
446+
Res::Def(DefKind::Union, i) => (i, ItemType::Union),
447+
Res::Def(DefKind::Mod, i) => (i, ItemType::Module),
448+
Res::Def(DefKind::ForeignTy, i) => (i, ItemType::ForeignType),
449+
Res::Def(DefKind::Const, i) => (i, ItemType::Constant),
450+
Res::Def(DefKind::Static, i) => (i, ItemType::Static),
451451
Res::Def(DefKind::Variant, i) => {
452-
(cx.tcx.parent(i).expect("cannot get parent def id"), TypeKind::Enum)
452+
(cx.tcx.parent(i).expect("cannot get parent def id"), ItemType::Enum)
453453
}
454454
Res::Def(DefKind::Macro(mac_kind), i) => match mac_kind {
455-
MacroKind::Bang => (i, TypeKind::Macro),
456-
MacroKind::Attr => (i, TypeKind::Attr),
457-
MacroKind::Derive => (i, TypeKind::Derive),
455+
MacroKind::Bang => (i, ItemType::Macro),
456+
MacroKind::Attr => (i, ItemType::ProcAttribute),
457+
MacroKind::Derive => (i, ItemType::ProcDerive),
458458
},
459-
Res::Def(DefKind::TraitAlias, i) => (i, TypeKind::TraitAlias),
460-
Res::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
459+
Res::Def(DefKind::TraitAlias, i) => (i, ItemType::TraitAlias),
460+
Res::SelfTy(Some(def_id), _) => (def_id, ItemType::Trait),
461461
Res::SelfTy(_, Some((impl_def_id, _))) => return impl_def_id,
462462
_ => return res.def_id(),
463463
};
464464
if did.is_local() {
465465
return did;
466466
}
467467
inline::record_extern_fqn(cx, did, kind);
468-
if let TypeKind::Trait = kind {
468+
if let ItemType::Trait = kind {
469469
inline::record_extern_trait(cx, did);
470470
}
471471
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)