1
Fork 0

Take ItemType instead of TypeKind in record_extern_fqn

This commit is contained in:
Joshua Nelson 2021-04-22 21:23:11 -04:00
parent 484c61943f
commit 1481af9f59
5 changed files with 84 additions and 49 deletions

View file

@ -15,7 +15,7 @@ use rustc_span::hygiene::MacroKind;
use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span; 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::core::DocContext;
use crate::formats::item_type::ItemType; use crate::formats::item_type::ItemType;
@ -56,36 +56,36 @@ crate fn try_inline(
let kind = match res { let kind = match res {
Res::Def(DefKind::Trait, did) => { 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); build_impls(cx, Some(parent_module), did, attrs, &mut ret);
clean::TraitItem(build_external_trait(cx, did)) clean::TraitItem(build_external_trait(cx, did))
} }
Res::Def(DefKind::Fn, 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)) clean::FunctionItem(build_external_function(cx, did))
} }
Res::Def(DefKind::Struct, 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); build_impls(cx, Some(parent_module), did, attrs, &mut ret);
clean::StructItem(build_struct(cx, did)) clean::StructItem(build_struct(cx, did))
} }
Res::Def(DefKind::Union, 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); build_impls(cx, Some(parent_module), did, attrs, &mut ret);
clean::UnionItem(build_union(cx, did)) clean::UnionItem(build_union(cx, did))
} }
Res::Def(DefKind::TyAlias, 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); build_impls(cx, Some(parent_module), did, attrs, &mut ret);
clean::TypedefItem(build_type_alias(cx, did), false) clean::TypedefItem(build_type_alias(cx, did), false)
} }
Res::Def(DefKind::Enum, did) => { 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); build_impls(cx, Some(parent_module), did, attrs, &mut ret);
clean::EnumItem(build_enum(cx, did)) clean::EnumItem(build_enum(cx, did))
} }
Res::Def(DefKind::ForeignTy, 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); build_impls(cx, Some(parent_module), did, attrs, &mut ret);
clean::ForeignTypeItem clean::ForeignTypeItem
} }
@ -95,24 +95,24 @@ crate fn try_inline(
// their constructors. // their constructors.
Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => return Some(Vec::new()), Res::Def(DefKind::Ctor(..), _) | Res::SelfCtor(..) => return Some(Vec::new()),
Res::Def(DefKind::Mod, did) => { 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)) clean::ModuleItem(build_module(cx, did, visited))
} }
Res::Def(DefKind::Static, did) => { 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))) clean::StaticItem(build_static(cx, did, cx.tcx.is_mutable_static(did)))
} }
Res::Def(DefKind::Const, 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)) clean::ConstantItem(build_const(cx, did))
} }
Res::Def(DefKind::Macro(kind), did) => { Res::Def(DefKind::Macro(kind), did) => {
let mac = build_macro(cx, did, name); let mac = build_macro(cx, did, name);
let type_kind = match kind { let type_kind = match kind {
MacroKind::Bang => TypeKind::Macro, MacroKind::Bang => ItemType::Macro,
MacroKind::Attr => TypeKind::Attr, MacroKind::Attr => ItemType::ProcAttribute,
MacroKind::Derive => TypeKind::Derive, MacroKind::Derive => ItemType::ProcDerive,
}; };
record_extern_fqn(cx, did, type_kind); record_extern_fqn(cx, did, type_kind);
mac mac
@ -157,7 +157,7 @@ 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 /// These names are used later on by HTML rendering to generate things like
/// source links back to the original item. /// 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 crate_name = cx.tcx.crate_name(did.krate).to_string();
let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| { let relative = cx.tcx.def_path(did).data.into_iter().filter_map(|elem| {
@ -165,7 +165,7 @@ crate fn record_extern_fqn(cx: &mut DocContext<'_>, did: DefId, kind: clean::Typ
let s = elem.data.to_string(); let s = elem.data.to_string();
if !s.is_empty() { Some(s) } else { None } 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 // Check to see if it is a macro 2.0 or built-in macro
if matches!( if matches!(
cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())), cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())),

View file

@ -36,6 +36,7 @@ use std::{mem, vec};
use crate::core::{self, DocContext, ImplTraitParam}; use crate::core::{self, DocContext, ImplTraitParam};
use crate::doctree; use crate::doctree;
use crate::formats::item_type::ItemType;
use utils::*; use utils::*;
@ -158,7 +159,7 @@ impl Clean<GenericBound> for hir::GenericBound<'_> {
impl Clean<Type> for (ty::TraitRef<'_>, &[TypeBinding]) { impl Clean<Type> for (ty::TraitRef<'_>, &[TypeBinding]) {
fn clean(&self, cx: &mut DocContext<'_>) -> Type { fn clean(&self, cx: &mut DocContext<'_>) -> Type {
let (trait_ref, bounds) = *self; 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( let path = external_path(
cx, cx,
cx.tcx.item_name(trait_ref.def_id), cx.tcx.item_name(trait_ref.def_id),
@ -913,8 +914,8 @@ impl Clean<PolyTrait> for hir::PolyTraitRef<'_> {
} }
} }
impl Clean<TypeKind> for hir::def::DefKind { impl Clean<ItemType> for hir::def::DefKind {
fn clean(&self, _: &mut DocContext<'_>) -> TypeKind { fn clean(&self, _: &mut DocContext<'_>) -> ItemType {
(*self).into() (*self).into()
} }
} }
@ -1453,16 +1454,16 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
ty::Adt(def, substs) => { ty::Adt(def, substs) => {
let did = def.did; let did = def.did;
let kind = match def.adt_kind() { let kind = match def.adt_kind() {
AdtKind::Struct => TypeKind::Struct, AdtKind::Struct => ItemType::Struct,
AdtKind::Union => TypeKind::Union, AdtKind::Union => ItemType::Union,
AdtKind::Enum => TypeKind::Enum, AdtKind::Enum => ItemType::Enum,
}; };
inline::record_extern_fqn(cx, did, kind); inline::record_extern_fqn(cx, did, kind);
let path = external_path(cx, cx.tcx.item_name(did), None, false, vec![], substs); let path = external_path(cx, cx.tcx.item_name(did), None, false, vec![], substs);
ResolvedPath { path, param_names: None, did, is_generic: false } ResolvedPath { path, param_names: None, did, is_generic: false }
} }
ty::Foreign(did) => { ty::Foreign(did) => {
inline::record_extern_fqn(cx, did, TypeKind::Foreign); inline::record_extern_fqn(cx, did, ItemType::ForeignType);
let path = external_path( let path = external_path(
cx, cx,
cx.tcx.item_name(did), cx.tcx.item_name(did),
@ -1487,7 +1488,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
_ => cx.tcx.intern_substs(&[]), _ => 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![]; let mut param_names = vec![];
if let Some(b) = reg.clean(cx) { if let Some(b) = reg.clean(cx) {
@ -1497,7 +1498,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
let empty = cx.tcx.intern_substs(&[]); let empty = cx.tcx.intern_substs(&[]);
let path = let path =
external_path(cx, cx.tcx.item_name(did), Some(did), false, vec![], empty); 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( let bound = GenericBound::TraitBound(
PolyTrait { PolyTrait {
trait_: ResolvedPath { trait_: ResolvedPath {

View file

@ -1103,7 +1103,7 @@ impl GenericBound {
let did = cx.tcx.require_lang_item(LangItem::Sized, None); let did = cx.tcx.require_lang_item(LangItem::Sized, None);
let empty = cx.tcx.intern_substs(&[]); let empty = cx.tcx.intern_substs(&[]);
let path = external_path(cx, cx.tcx.item_name(did), Some(did), false, vec![], empty); 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( GenericBound::TraitBound(
PolyTrait { PolyTrait {
trait_: ResolvedPath { path, param_names: None, did, is_generic: false }, trait_: ResolvedPath { path, param_names: None, did, is_generic: false },
@ -1455,8 +1455,6 @@ crate enum TypeKind {
Typedef, Typedef,
Foreign, Foreign,
Macro, Macro,
Attr,
Derive,
TraitAlias, TraitAlias,
Primitive, Primitive,
} }

View file

@ -3,9 +3,9 @@ use crate::clean::blanket_impl::BlanketImplFinder;
use crate::clean::{ use crate::clean::{
inline, Clean, Crate, Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime, inline, Clean, Crate, Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime,
MacroKind, Path, PathSegment, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding, MacroKind, Path, PathSegment, Primitive, PrimitiveType, ResolvedPath, Type, TypeBinding,
TypeKind,
}; };
use crate::core::DocContext; use crate::core::DocContext;
use crate::formats::item_type::ItemType;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
@ -435,29 +435,29 @@ crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
debug!("register_res({:?})", res); debug!("register_res({:?})", res);
let (did, kind) = match res { let (did, kind) = match res {
Res::Def(DefKind::Fn, i) => (i, TypeKind::Function), Res::Def(DefKind::Fn, i) => (i, ItemType::Function),
Res::Def(DefKind::TyAlias, i) => (i, TypeKind::Typedef), Res::Def(DefKind::TyAlias, i) => (i, ItemType::Typedef),
Res::Def(DefKind::Enum, i) => (i, TypeKind::Enum), Res::Def(DefKind::Enum, i) => (i, ItemType::Enum),
Res::Def(DefKind::Trait, i) => (i, TypeKind::Trait), Res::Def(DefKind::Trait, i) => (i, ItemType::Trait),
Res::Def(DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst, i) => { 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::Struct, i) => (i, ItemType::Struct),
Res::Def(DefKind::Union, i) => (i, TypeKind::Union), Res::Def(DefKind::Union, i) => (i, ItemType::Union),
Res::Def(DefKind::Mod, i) => (i, TypeKind::Module), Res::Def(DefKind::Mod, i) => (i, ItemType::Module),
Res::Def(DefKind::ForeignTy, i) => (i, TypeKind::Foreign), Res::Def(DefKind::ForeignTy, i) => (i, ItemType::ForeignType),
Res::Def(DefKind::Const, i) => (i, TypeKind::Const), Res::Def(DefKind::Const, i) => (i, ItemType::Constant),
Res::Def(DefKind::Static, i) => (i, TypeKind::Static), Res::Def(DefKind::Static, i) => (i, ItemType::Static),
Res::Def(DefKind::Variant, i) => { 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 { Res::Def(DefKind::Macro(mac_kind), i) => match mac_kind {
MacroKind::Bang => (i, TypeKind::Macro), MacroKind::Bang => (i, ItemType::Macro),
MacroKind::Attr => (i, TypeKind::Attr), MacroKind::Attr => (i, ItemType::ProcAttribute),
MacroKind::Derive => (i, TypeKind::Derive), MacroKind::Derive => (i, ItemType::ProcDerive),
}, },
Res::Def(DefKind::TraitAlias, i) => (i, TypeKind::TraitAlias), Res::Def(DefKind::TraitAlias, i) => (i, ItemType::TraitAlias),
Res::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait), Res::SelfTy(Some(def_id), _) => (def_id, ItemType::Trait),
Res::SelfTy(_, Some((impl_def_id, _))) => return impl_def_id, Res::SelfTy(_, Some((impl_def_id, _))) => return impl_def_id,
_ => return res.def_id(), _ => return res.def_id(),
}; };
@ -465,7 +465,7 @@ crate fn register_res(cx: &mut DocContext<'_>, res: Res) -> DefId {
return did; return did;
} }
inline::record_extern_fqn(cx, did, kind); inline::record_extern_fqn(cx, did, kind);
if let TypeKind::Trait = kind { if let ItemType::Trait = kind {
inline::record_extern_trait(cx, did); inline::record_extern_trait(cx, did);
} }
did did

View file

@ -4,6 +4,7 @@ use std::fmt;
use serde::{Serialize, Serializer}; use serde::{Serialize, Serializer};
use rustc_hir as hir;
use rustc_span::hygiene::MacroKind; use rustc_span::hygiene::MacroKind;
use crate::clean; use crate::clean;
@ -116,14 +117,49 @@ impl From<clean::TypeKind> for ItemType {
clean::TypeKind::Typedef => ItemType::Typedef, clean::TypeKind::Typedef => ItemType::Typedef,
clean::TypeKind::Foreign => ItemType::ForeignType, clean::TypeKind::Foreign => ItemType::ForeignType,
clean::TypeKind::Macro => ItemType::Macro, clean::TypeKind::Macro => ItemType::Macro,
clean::TypeKind::Attr => ItemType::ProcAttribute,
clean::TypeKind::Derive => ItemType::ProcDerive,
clean::TypeKind::TraitAlias => ItemType::TraitAlias, clean::TypeKind::TraitAlias => ItemType::TraitAlias,
clean::TypeKind::Primitive => ItemType::Primitive, clean::TypeKind::Primitive => ItemType::Primitive,
} }
} }
} }
impl From<hir::def::DefKind> for ItemType {
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::Constant,
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::ForeignType,
}
}
}
impl ItemType { impl ItemType {
crate fn as_str(&self) -> &'static str { crate fn as_str(&self) -> &'static str {
match *self { match *self {