Skip to content

Commit

Permalink
Rollup merge of #84464 - jyn514:type-kind, r=CraftSpider
Browse files Browse the repository at this point in the history
rustdoc: Get rid of `clean::TypeKind`

It does exactly the same thing as ItemType.
  • Loading branch information
JohnTitor committed Apr 23, 2021
2 parents 6409149 + 6c066ab commit 3ed8bba
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 134 deletions.
32 changes: 16 additions & 16 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span;

use crate::clean::{self, Attributes, GetDefId, ToSource, TypeKind};
use crate::clean::{self, Attributes, GetDefId, ToSource};
use crate::core::DocContext;
use crate::formats::item_type::ItemType;

Expand Down Expand Up @@ -56,36 +56,36 @@ crate fn try_inline(

let kind = match res {
Res::Def(DefKind::Trait, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Trait);
record_extern_fqn(cx, did, ItemType::Trait);
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
clean::TraitItem(build_external_trait(cx, did))
}
Res::Def(DefKind::Fn, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Function);
record_extern_fqn(cx, did, ItemType::Function);
clean::FunctionItem(build_external_function(cx, did))
}
Res::Def(DefKind::Struct, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Struct);
record_extern_fqn(cx, did, ItemType::Struct);
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
clean::StructItem(build_struct(cx, did))
}
Res::Def(DefKind::Union, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Union);
record_extern_fqn(cx, did, ItemType::Union);
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
clean::UnionItem(build_union(cx, did))
}
Res::Def(DefKind::TyAlias, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Typedef);
record_extern_fqn(cx, did, ItemType::Typedef);
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
clean::TypedefItem(build_type_alias(cx, did), false)
}
Res::Def(DefKind::Enum, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Enum);
record_extern_fqn(cx, did, ItemType::Enum);
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
clean::EnumItem(build_enum(cx, did))
}
Res::Def(DefKind::ForeignTy, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Foreign);
record_extern_fqn(cx, did, ItemType::ForeignType);
build_impls(cx, Some(parent_module), did, attrs, &mut ret);
clean::ForeignTypeItem
}
Expand All @@ -95,24 +95,24 @@ crate fn try_inline(
// their constructors.
Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => return Some(Vec::new()),
Res::Def(DefKind::Mod, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Module);
record_extern_fqn(cx, did, ItemType::Module);
clean::ModuleItem(build_module(cx, did, visited))
}
Res::Def(DefKind::Static, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Static);
record_extern_fqn(cx, did, ItemType::Static);
clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did)))
}
Res::Def(DefKind::Const, did) => {
record_extern_fqn(cx, did, clean::TypeKind::Const);
record_extern_fqn(cx, did, ItemType::Constant);
clean::ConstantItem(build_const(cx, did))
}
Res::Def(DefKind::Macro(kind), did) => {
let mac = build_macro(cx, did, name);

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

let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| {
// extern blocks have an empty name
let s = elem.data.to_string();
if !s.is_empty() { Some(s) } else { None }
});
let fqn = if let clean::TypeKind::Macro = kind {
let fqn = if let ItemType::Macro = kind {
// Check to see if it is a macro 2.0 or built-in macro
if matches!(
cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())),
Expand Down
21 changes: 8 additions & 13 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use std::{mem, vec};

use crate::core::{self, DocContext, ImplTraitParam};
use crate::doctree;
use crate::formats::item_type::ItemType;

use utils::*;

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

impl Clean<TypeKind> for hir::def::DefKind {
fn clean(&self, _: &mut DocContext<'_>) -> TypeKind {
(*self).into()
}
}

impl Clean<Item> for hir::TraitItem<'_> {
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
let local_did = self.def_id.to_def_id();
Expand Down Expand Up @@ -1568,16 +1563,16 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
ty::Adt(def, substs) => {
let did = def.did;
let kind = match def.adt_kind() {
AdtKind::Struct => TypeKind::Struct,
AdtKind::Union => TypeKind::Union,
AdtKind::Enum => TypeKind::Enum,
AdtKind::Struct => ItemType::Struct,
AdtKind::Union => ItemType::Union,
AdtKind::Enum => ItemType::Enum,
};
inline::record_extern_fqn(cx, did, kind);
let path = external_path(cx, cx.tcx.item_name(did), None, false, vec![], substs);
ResolvedPath { path, param_names: None, did, is_generic: false }
}
ty::Foreign(did) => {
inline::record_extern_fqn(cx, did, TypeKind::Foreign);
inline::record_extern_fqn(cx, did, ItemType::ForeignType);
let path = external_path(
cx,
cx.tcx.item_name(did),
Expand All @@ -1602,7 +1597,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
_ => cx.tcx.intern_substs(&[]),
};

inline::record_extern_fqn(cx, did, TypeKind::Trait);
inline::record_extern_fqn(cx, did, ItemType::Trait);

let mut param_names = vec![];
if let Some(b) = reg.clean(cx) {
Expand All @@ -1612,7 +1607,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
let empty = cx.tcx.intern_substs(&[]);
let path =
external_path(cx, cx.tcx.item_name(did), Some(did), false, vec![], empty);
inline::record_extern_fqn(cx, did, TypeKind::Trait);
inline::record_extern_fqn(cx, did, ItemType::Trait);
let bound = GenericBound::TraitBound(
PolyTrait {
trait_: ResolvedPath {
Expand Down
58 changes: 1 addition & 57 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ impl GenericBound {
let did = cx.tcx.require_lang_item(LangItem::Sized, None);
let empty = cx.tcx.intern_substs(&[]);
let path = external_path(cx, cx.tcx.item_name(did), Some(did), false, vec![], empty);
inline::record_extern_fqn(cx, did, TypeKind::Trait);
inline::record_extern_fqn(cx, did, ItemType::Trait);
GenericBound::TraitBound(
PolyTrait {
trait_: ResolvedPath { path, param_names: None, did, is_generic: false },
Expand Down Expand Up @@ -1315,62 +1315,6 @@ crate enum PrimitiveType {
Never,
}

#[derive(Clone, PartialEq, Eq, Hash, Copy, Debug)]
crate enum TypeKind {
Enum,
Function,
Module,
Const,
Static,
Struct,
Union,
Trait,
Typedef,
Foreign,
Macro,
Attr,
Derive,
TraitAlias,
Primitive,
}

impl From<hir::def::DefKind> for TypeKind {
fn from(other: hir::def::DefKind) -> Self {
match other {
hir::def::DefKind::Enum => Self::Enum,
hir::def::DefKind::Fn => Self::Function,
hir::def::DefKind::Mod => Self::Module,
hir::def::DefKind::Const => Self::Const,
hir::def::DefKind::Static => Self::Static,
hir::def::DefKind::Struct => Self::Struct,
hir::def::DefKind::Union => Self::Union,
hir::def::DefKind::Trait => Self::Trait,
hir::def::DefKind::TyAlias => Self::Typedef,
hir::def::DefKind::TraitAlias => Self::TraitAlias,
hir::def::DefKind::Macro(_) => Self::Macro,
hir::def::DefKind::ForeignTy
| hir::def::DefKind::Variant
| hir::def::DefKind::AssocTy
| hir::def::DefKind::TyParam
| hir::def::DefKind::ConstParam
| hir::def::DefKind::Ctor(..)
| hir::def::DefKind::AssocFn
| hir::def::DefKind::AssocConst
| hir::def::DefKind::ExternCrate
| hir::def::DefKind::Use
| hir::def::DefKind::ForeignMod
| hir::def::DefKind::AnonConst
| hir::def::DefKind::OpaqueTy
| hir::def::DefKind::Field
| hir::def::DefKind::LifetimeParam
| hir::def::DefKind::GlobalAsm
| hir::def::DefKind::Impl
| hir::def::DefKind::Closure
| hir::def::DefKind::Generator => Self::Foreign,
}
}
}

crate trait GetDefId {
/// Use this method to get the [`DefId`] of a [`clean`] AST node.
/// This will return [`None`] when called on a primitive [`clean::Type`].
Expand Down
39 changes: 20 additions & 19 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use crate::clean::blanket_impl::BlanketImplFinder;
use crate::clean::{
inline, Clean, Crate, ExternalCrate, Generic, GenericArg, GenericArgs, ImportSource, Item,
ItemKind, Lifetime, MacroKind, Path, PathSegment, Primitive, PrimitiveType, ResolvedPath, Type,
TypeBinding, TypeKind,
TypeBinding,
};
use crate::core::DocContext;
use crate::formats::item_type::ItemType;

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

let (did, kind) = match res {
Res::Def(DefKind::Fn, i) => (i, TypeKind::Function),
Res::Def(DefKind::TyAlias, i) => (i, TypeKind::Typedef),
Res::Def(DefKind::Enum, i) => (i, TypeKind::Enum),
Res::Def(DefKind::Trait, i) => (i, TypeKind::Trait),
Res::Def(DefKind::Fn, i) => (i, ItemType::Function),
Res::Def(DefKind::TyAlias, i) => (i, ItemType::Typedef),
Res::Def(DefKind::Enum, i) => (i, ItemType::Enum),
Res::Def(DefKind::Trait, i) => (i, ItemType::Trait),
Res::Def(DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst, i) => {
(cx.tcx.parent(i).unwrap(), TypeKind::Trait)
(cx.tcx.parent(i).unwrap(), ItemType::Trait)
}
Res::Def(DefKind::Struct, i) => (i, TypeKind::Struct),
Res::Def(DefKind::Union, i) => (i, TypeKind::Union),
Res::Def(DefKind::Mod, i) => (i, TypeKind::Module),
Res::Def(DefKind::ForeignTy, i) => (i, TypeKind::Foreign),
Res::Def(DefKind::Const, i) => (i, TypeKind::Const),
Res::Def(DefKind::Static, i) => (i, TypeKind::Static),
Res::Def(DefKind::Struct, i) => (i, ItemType::Struct),
Res::Def(DefKind::Union, i) => (i, ItemType::Union),
Res::Def(DefKind::Mod, i) => (i, ItemType::Module),
Res::Def(DefKind::ForeignTy, i) => (i, ItemType::ForeignType),
Res::Def(DefKind::Const, i) => (i, ItemType::Constant),
Res::Def(DefKind::Static, i) => (i, ItemType::Static),
Res::Def(DefKind::Variant, i) => {
(cx.tcx.parent(i).expect("cannot get parent def id"), TypeKind::Enum)
(cx.tcx.parent(i).expect("cannot get parent def id"), ItemType::Enum)
}
Res::Def(DefKind::Macro(mac_kind), i) => match mac_kind {
MacroKind::Bang => (i, TypeKind::Macro),
MacroKind::Attr => (i, TypeKind::Attr),
MacroKind::Derive => (i, TypeKind::Derive),
MacroKind::Bang => (i, ItemType::Macro),
MacroKind::Attr => (i, ItemType::ProcAttribute),
MacroKind::Derive => (i, ItemType::ProcDerive),
},
Res::Def(DefKind::TraitAlias, i) => (i, TypeKind::TraitAlias),
Res::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
Res::Def(DefKind::TraitAlias, i) => (i, ItemType::TraitAlias),
Res::SelfTy(Some(def_id), _) => (def_id, ItemType::Trait),
Res::SelfTy(_, Some((impl_def_id, _))) => return impl_def_id,
_ => return res.def_id(),
};
if did.is_local() {
return did;
}
inline::record_extern_fqn(cx, did, kind);
if let TypeKind::Trait = kind {
if let ItemType::Trait = kind {
inline::record_extern_trait(cx, did);
}
did
Expand Down
58 changes: 39 additions & 19 deletions src/librustdoc/formats/item_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::fmt;

use serde::{Serialize, Serializer};

use rustc_hir::def::DefKind;
use rustc_span::hygiene::MacroKind;

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

impl From<clean::TypeKind> for ItemType {
fn from(kind: clean::TypeKind) -> ItemType {
match kind {
clean::TypeKind::Struct => ItemType::Struct,
clean::TypeKind::Union => ItemType::Union,
clean::TypeKind::Enum => ItemType::Enum,
clean::TypeKind::Function => ItemType::Function,
clean::TypeKind::Trait => ItemType::Trait,
clean::TypeKind::Module => ItemType::Module,
clean::TypeKind::Static => ItemType::Static,
clean::TypeKind::Const => ItemType::Constant,
clean::TypeKind::Typedef => ItemType::Typedef,
clean::TypeKind::Foreign => ItemType::ForeignType,
clean::TypeKind::Macro => ItemType::Macro,
clean::TypeKind::Attr => ItemType::ProcAttribute,
clean::TypeKind::Derive => ItemType::ProcDerive,
clean::TypeKind::TraitAlias => ItemType::TraitAlias,
clean::TypeKind::Primitive => ItemType::Primitive,
impl From<DefKind> for ItemType {
fn from(other: DefKind) -> Self {
match other {
DefKind::Enum => Self::Enum,
DefKind::Fn => Self::Function,
DefKind::Mod => Self::Module,
DefKind::Const => Self::Constant,
DefKind::Static => Self::Static,
DefKind::Struct => Self::Struct,
DefKind::Union => Self::Union,
DefKind::Trait => Self::Trait,
DefKind::TyAlias => Self::Typedef,
DefKind::TraitAlias => Self::TraitAlias,
DefKind::Macro(kind) => match kind {
MacroKind::Bang => ItemType::Macro,
MacroKind::Attr => ItemType::ProcAttribute,
MacroKind::Derive => ItemType::ProcDerive,
},
DefKind::ForeignTy
| DefKind::Variant
| DefKind::AssocTy
| DefKind::TyParam
| DefKind::ConstParam
| DefKind::Ctor(..)
| DefKind::AssocFn
| DefKind::AssocConst
| DefKind::ExternCrate
| DefKind::Use
| DefKind::ForeignMod
| DefKind::AnonConst
| DefKind::OpaqueTy
| DefKind::Field
| DefKind::LifetimeParam
| DefKind::GlobalAsm
| DefKind::Impl
| DefKind::Closure
| DefKind::Generator => Self::ForeignType,
}
}
}
Expand Down
Loading

0 comments on commit 3ed8bba

Please sign in to comment.