Skip to content

Commit e1d9b3c

Browse files
committed
Take ItemType instead of TypeKind in record_extern_fqn
1 parent 25c15cd commit e1d9b3c

File tree

5 files changed

+85
-49
lines changed

5 files changed

+85
-49
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 16 additions & 16 deletions
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

Lines changed: 10 additions & 9 deletions
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,8 +1029,8 @@ impl Clean<PolyTrait> for hir::PolyTraitRef<'_> {
10281029
}
10291030
}
10301031

1031-
impl Clean<TypeKind> for hir::def::DefKind {
1032-
fn clean(&self, _: &mut DocContext<'_>) -> TypeKind {
1032+
impl Clean<ItemType> for hir::def::DefKind {
1033+
fn clean(&self, _: &mut DocContext<'_>) -> ItemType {
10331034
(*self).into()
10341035
}
10351036
}
@@ -1568,16 +1569,16 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
15681569
ty::Adt(def, substs) => {
15691570
let did = def.did;
15701571
let kind = match def.adt_kind() {
1571-
AdtKind::Struct => TypeKind::Struct,
1572-
AdtKind::Union => TypeKind::Union,
1573-
AdtKind::Enum => TypeKind::Enum,
1572+
AdtKind::Struct => ItemType::Struct,
1573+
AdtKind::Union => ItemType::Union,
1574+
AdtKind::Enum => ItemType::Enum,
15741575
};
15751576
inline::record_extern_fqn(cx, did, kind);
15761577
let path = external_path(cx, cx.tcx.item_name(did), None, false, vec![], substs);
15771578
ResolvedPath { path, param_names: None, did, is_generic: false }
15781579
}
15791580
ty::Foreign(did) => {
1580-
inline::record_extern_fqn(cx, did, TypeKind::Foreign);
1581+
inline::record_extern_fqn(cx, did, ItemType::ForeignType);
15811582
let path = external_path(
15821583
cx,
15831584
cx.tcx.item_name(did),
@@ -1602,7 +1603,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
16021603
_ => cx.tcx.intern_substs(&[]),
16031604
};
16041605

1605-
inline::record_extern_fqn(cx, did, TypeKind::Trait);
1606+
inline::record_extern_fqn(cx, did, ItemType::Trait);
16061607

16071608
let mut param_names = vec![];
16081609
if let Some(b) = reg.clean(cx) {
@@ -1612,7 +1613,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
16121613
let empty = cx.tcx.intern_substs(&[]);
16131614
let path =
16141615
external_path(cx, cx.tcx.item_name(did), Some(did), false, vec![], empty);
1615-
inline::record_extern_fqn(cx, did, TypeKind::Trait);
1616+
inline::record_extern_fqn(cx, did, ItemType::Trait);
16161617
let bound = GenericBound::TraitBound(
16171618
PolyTrait {
16181619
trait_: ResolvedPath {

src/librustdoc/clean/types.rs

Lines changed: 1 addition & 3 deletions
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 },
@@ -1328,8 +1328,6 @@ crate enum TypeKind {
13281328
Typedef,
13291329
Foreign,
13301330
Macro,
1331-
Attr,
1332-
Derive,
13331331
TraitAlias,
13341332
Primitive,
13351333
}

src/librustdoc/clean/utils.rs

Lines changed: 20 additions & 19 deletions
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

Lines changed: 38 additions & 2 deletions
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 as hir;
78
use rustc_span::hygiene::MacroKind;
89

910
use crate::clean;
@@ -116,14 +117,49 @@ impl From<clean::TypeKind> for ItemType {
116117
clean::TypeKind::Typedef => ItemType::Typedef,
117118
clean::TypeKind::Foreign => ItemType::ForeignType,
118119
clean::TypeKind::Macro => ItemType::Macro,
119-
clean::TypeKind::Attr => ItemType::ProcAttribute,
120-
clean::TypeKind::Derive => ItemType::ProcDerive,
121120
clean::TypeKind::TraitAlias => ItemType::TraitAlias,
122121
clean::TypeKind::Primitive => ItemType::Primitive,
123122
}
124123
}
125124
}
126125

126+
impl From<hir::def::DefKind> for ItemType {
127+
fn from(other: hir::def::DefKind) -> Self {
128+
match other {
129+
hir::def::DefKind::Enum => Self::Enum,
130+
hir::def::DefKind::Fn => Self::Function,
131+
hir::def::DefKind::Mod => Self::Module,
132+
hir::def::DefKind::Const => Self::Constant,
133+
hir::def::DefKind::Static => Self::Static,
134+
hir::def::DefKind::Struct => Self::Struct,
135+
hir::def::DefKind::Union => Self::Union,
136+
hir::def::DefKind::Trait => Self::Trait,
137+
hir::def::DefKind::TyAlias => Self::Typedef,
138+
hir::def::DefKind::TraitAlias => Self::TraitAlias,
139+
hir::def::DefKind::Macro(_) => Self::Macro,
140+
hir::def::DefKind::ForeignTy
141+
| hir::def::DefKind::Variant
142+
| hir::def::DefKind::AssocTy
143+
| hir::def::DefKind::TyParam
144+
| hir::def::DefKind::ConstParam
145+
| hir::def::DefKind::Ctor(..)
146+
| hir::def::DefKind::AssocFn
147+
| hir::def::DefKind::AssocConst
148+
| hir::def::DefKind::ExternCrate
149+
| hir::def::DefKind::Use
150+
| hir::def::DefKind::ForeignMod
151+
| hir::def::DefKind::AnonConst
152+
| hir::def::DefKind::OpaqueTy
153+
| hir::def::DefKind::Field
154+
| hir::def::DefKind::LifetimeParam
155+
| hir::def::DefKind::GlobalAsm
156+
| hir::def::DefKind::Impl
157+
| hir::def::DefKind::Closure
158+
| hir::def::DefKind::Generator => Self::ForeignType,
159+
}
160+
}
161+
}
162+
127163
impl ItemType {
128164
crate fn as_str(&self) -> &'static str {
129165
match *self {

0 commit comments

Comments
 (0)