Skip to content

Commit 825e6b3

Browse files
authored
Rollup merge of rust-lang#36903 - frewsxcv:typekind, r=jseyfried
Minor librustdoc cleanup and refactoring.
2 parents d25aeb0 + 35d214a commit 825e6b3

File tree

11 files changed

+107
-108
lines changed

11 files changed

+107
-108
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,49 +73,49 @@ fn try_inline_def<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
7373
let did = def.def_id();
7474
let inner = match def {
7575
Def::Trait(did) => {
76-
record_extern_fqn(cx, did, clean::TypeTrait);
76+
record_extern_fqn(cx, did, clean::TypeKind::Trait);
7777
ret.extend(build_impls(cx, tcx, did));
7878
clean::TraitItem(build_external_trait(cx, tcx, did))
7979
}
8080
Def::Fn(did) => {
81-
record_extern_fqn(cx, did, clean::TypeFunction);
81+
record_extern_fqn(cx, did, clean::TypeKind::Function);
8282
clean::FunctionItem(build_external_function(cx, tcx, did))
8383
}
8484
Def::Struct(did)
8585
// If this is a struct constructor, we skip it
8686
if tcx.def_key(did).disambiguated_data.data != DefPathData::StructCtor => {
87-
record_extern_fqn(cx, did, clean::TypeStruct);
87+
record_extern_fqn(cx, did, clean::TypeKind::Struct);
8888
ret.extend(build_impls(cx, tcx, did));
8989
clean::StructItem(build_struct(cx, tcx, did))
9090
}
9191
Def::Union(did) => {
92-
record_extern_fqn(cx, did, clean::TypeUnion);
92+
record_extern_fqn(cx, did, clean::TypeKind::Union);
9393
ret.extend(build_impls(cx, tcx, did));
9494
clean::UnionItem(build_union(cx, tcx, did))
9595
}
9696
Def::TyAlias(did) => {
97-
record_extern_fqn(cx, did, clean::TypeTypedef);
97+
record_extern_fqn(cx, did, clean::TypeKind::Typedef);
9898
ret.extend(build_impls(cx, tcx, did));
9999
clean::TypedefItem(build_type_alias(cx, tcx, did), false)
100100
}
101101
Def::Enum(did) => {
102-
record_extern_fqn(cx, did, clean::TypeEnum);
102+
record_extern_fqn(cx, did, clean::TypeKind::Enum);
103103
ret.extend(build_impls(cx, tcx, did));
104104
clean::EnumItem(build_enum(cx, tcx, did))
105105
}
106106
// Assume that the enum type is reexported next to the variant, and
107107
// variants don't show up in documentation specially.
108108
Def::Variant(..) => return Some(Vec::new()),
109109
Def::Mod(did) => {
110-
record_extern_fqn(cx, did, clean::TypeModule);
110+
record_extern_fqn(cx, did, clean::TypeKind::Module);
111111
clean::ModuleItem(build_module(cx, tcx, did))
112112
}
113113
Def::Static(did, mtbl) => {
114-
record_extern_fqn(cx, did, clean::TypeStatic);
114+
record_extern_fqn(cx, did, clean::TypeKind::Static);
115115
clean::StaticItem(build_static(cx, tcx, did, mtbl))
116116
}
117117
Def::Const(did) | Def::AssociatedConst(did) => {
118-
record_extern_fqn(cx, did, clean::TypeConst);
118+
record_extern_fqn(cx, did, clean::TypeKind::Const);
119119
clean::ConstantItem(build_const(cx, tcx, did))
120120
}
121121
_ => return None,
@@ -577,7 +577,7 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics)
577577
_ => true,
578578
}
579579
});
580-
return g;
580+
g
581581
}
582582

583583
/// Supertrait bounds for a trait are also listed in the generics coming from

src/librustdoc/clean/mod.rs

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
//! that clean them.
1313
1414
pub use self::Type::*;
15-
pub use self::TypeKind::*;
16-
pub use self::VariantKind::*;
1715
pub use self::Mutability::*;
18-
pub use self::Import::*;
1916
pub use self::ItemEnum::*;
2017
pub use self::Attribute::*;
2118
pub use self::TyParamBound::*;
@@ -319,7 +316,7 @@ impl Item {
319316
match self.inner {
320317
StructItem(ref _struct) => Some(_struct.fields_stripped),
321318
UnionItem(ref union) => Some(union.fields_stripped),
322-
VariantItem(Variant { kind: StructVariant(ref vstruct)} ) => {
319+
VariantItem(Variant { kind: VariantKind::Struct(ref vstruct)} ) => {
323320
Some(vstruct.fields_stripped)
324321
},
325322
_ => None,
@@ -688,7 +685,7 @@ impl Clean<TyParamBound> for ty::BuiltinBound {
688685
(tcx.lang_items.sync_trait().unwrap(),
689686
external_path(cx, "Sync", None, false, vec![], empty)),
690687
};
691-
inline::record_extern_fqn(cx, did, TypeTrait);
688+
inline::record_extern_fqn(cx, did, TypeKind::Trait);
692689
TraitBound(PolyTrait {
693690
trait_: ResolvedPath {
694691
path: path,
@@ -707,7 +704,7 @@ impl<'tcx> Clean<TyParamBound> for ty::TraitRef<'tcx> {
707704
Some(tcx) => tcx,
708705
None => return RegionBound(Lifetime::statik())
709706
};
710-
inline::record_extern_fqn(cx, self.def_id, TypeTrait);
707+
inline::record_extern_fqn(cx, self.def_id, TypeKind::Trait);
711708
let path = external_path(cx, &tcx.item_name(self.def_id).as_str(),
712709
Some(self.def_id), true, vec![], self.substs);
713710

@@ -765,7 +762,7 @@ impl Lifetime {
765762
pub fn get_ref<'a>(&'a self) -> &'a str {
766763
let Lifetime(ref s) = *self;
767764
let s: &'a str = s;
768-
return s;
765+
s
769766
}
770767

771768
pub fn statik() -> Lifetime {
@@ -1130,7 +1127,7 @@ pub struct FnDecl {
11301127

11311128
impl FnDecl {
11321129
pub fn has_self(&self) -> bool {
1133-
return self.inputs.values.len() > 0 && self.inputs.values[0].name == "self";
1130+
self.inputs.values.len() > 0 && self.inputs.values[0].name == "self"
11341131
}
11351132

11361133
pub fn self_type(&self) -> Option<SelfTy> {
@@ -1480,16 +1477,16 @@ pub enum PrimitiveType {
14801477

14811478
#[derive(Clone, RustcEncodable, RustcDecodable, Copy, Debug)]
14821479
pub enum TypeKind {
1483-
TypeEnum,
1484-
TypeFunction,
1485-
TypeModule,
1486-
TypeConst,
1487-
TypeStatic,
1488-
TypeStruct,
1489-
TypeUnion,
1490-
TypeTrait,
1491-
TypeVariant,
1492-
TypeTypedef,
1480+
Enum,
1481+
Function,
1482+
Module,
1483+
Const,
1484+
Static,
1485+
Struct,
1486+
Union,
1487+
Trait,
1488+
Variant,
1489+
Typedef,
14931490
}
14941491

14951492
pub trait GetDefId {
@@ -1572,7 +1569,7 @@ impl PrimitiveType {
15721569
None
15731570
}
15741571

1575-
pub fn to_string(&self) -> &'static str {
1572+
pub fn as_str(&self) -> &'static str {
15761573
match *self {
15771574
PrimitiveType::Isize => "isize",
15781575
PrimitiveType::I8 => "i8",
@@ -1597,7 +1594,7 @@ impl PrimitiveType {
15971594
}
15981595

15991596
pub fn to_url_str(&self) -> &'static str {
1600-
self.to_string()
1597+
self.as_str()
16011598
}
16021599

16031600
/// Creates a rustdoc-specific node id for primitive types.
@@ -1795,9 +1792,9 @@ impl<'tcx> Clean<Type> for ty::Ty<'tcx> {
17951792
ty::TyAdt(def, substs) => {
17961793
let did = def.did;
17971794
let kind = match def.adt_kind() {
1798-
AdtKind::Struct => TypeStruct,
1799-
AdtKind::Union => TypeUnion,
1800-
AdtKind::Enum => TypeEnum,
1795+
AdtKind::Struct => TypeKind::Struct,
1796+
AdtKind::Union => TypeKind::Union,
1797+
AdtKind::Enum => TypeKind::Enum,
18011798
};
18021799
inline::record_extern_fqn(cx, did, kind);
18031800
let path = external_path(cx, &cx.tcx().item_name(did).as_str(),
@@ -1811,7 +1808,7 @@ impl<'tcx> Clean<Type> for ty::Ty<'tcx> {
18111808
}
18121809
ty::TyTrait(ref obj) => {
18131810
let did = obj.principal.def_id();
1814-
inline::record_extern_fqn(cx, did, TypeTrait);
1811+
inline::record_extern_fqn(cx, did, TypeKind::Trait);
18151812

18161813
let mut typarams = vec![];
18171814
obj.region_bound.clean(cx).map(|b| typarams.push(RegionBound(b)));
@@ -2027,7 +2024,7 @@ impl Clean<Item> for doctree::Variant {
20272024
deprecation: self.depr.clean(cx),
20282025
def_id: cx.map.local_def_id(self.def.id()),
20292026
inner: VariantItem(Variant {
2030-
kind: struct_def_to_variant_kind(&self.def, cx),
2027+
kind: self.def.clean(cx),
20312028
}),
20322029
}
20332030
}
@@ -2036,14 +2033,14 @@ impl Clean<Item> for doctree::Variant {
20362033
impl<'tcx> Clean<Item> for ty::VariantDefData<'tcx, 'static> {
20372034
fn clean(&self, cx: &DocContext) -> Item {
20382035
let kind = match self.kind {
2039-
ty::VariantKind::Unit => CLikeVariant,
2036+
ty::VariantKind::Unit => VariantKind::CLike,
20402037
ty::VariantKind::Tuple => {
2041-
TupleVariant(
2038+
VariantKind::Tuple(
20422039
self.fields.iter().map(|f| f.unsubst_ty().clean(cx)).collect()
20432040
)
20442041
}
20452042
ty::VariantKind::Struct => {
2046-
StructVariant(VariantStruct {
2043+
VariantKind::Struct(VariantStruct {
20472044
struct_type: doctree::Plain,
20482045
fields_stripped: false,
20492046
fields: self.fields.iter().map(|field| {
@@ -2076,18 +2073,20 @@ impl<'tcx> Clean<Item> for ty::VariantDefData<'tcx, 'static> {
20762073

20772074
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
20782075
pub enum VariantKind {
2079-
CLikeVariant,
2080-
TupleVariant(Vec<Type>),
2081-
StructVariant(VariantStruct),
2076+
CLike,
2077+
Tuple(Vec<Type>),
2078+
Struct(VariantStruct),
20822079
}
20832080

2084-
fn struct_def_to_variant_kind(struct_def: &hir::VariantData, cx: &DocContext) -> VariantKind {
2085-
if struct_def.is_struct() {
2086-
StructVariant(struct_def.clean(cx))
2087-
} else if struct_def.is_unit() {
2088-
CLikeVariant
2089-
} else {
2090-
TupleVariant(struct_def.fields().iter().map(|x| x.ty.clean(cx)).collect())
2081+
impl Clean<VariantKind> for hir::VariantData {
2082+
fn clean(&self, cx: &DocContext) -> VariantKind {
2083+
if self.is_struct() {
2084+
VariantKind::Struct(self.clean(cx))
2085+
} else if self.is_unit() {
2086+
VariantKind::CLike
2087+
} else {
2088+
VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect())
2089+
}
20912090
}
20922091
}
20932092

@@ -2526,7 +2525,7 @@ impl Clean<Vec<Item>> for doctree::Import {
25262525
});
25272526
let (mut ret, inner) = match self.node {
25282527
hir::ViewPathGlob(ref p) => {
2529-
(vec![], GlobImport(resolve_use_source(cx, p.clean(cx), self.id)))
2528+
(vec![], Import::Glob(resolve_use_source(cx, p.clean(cx), self.id)))
25302529
}
25312530
hir::ViewPathList(ref p, ref list) => {
25322531
// Attempt to inline all reexported items, but be sure
@@ -2552,17 +2551,16 @@ impl Clean<Vec<Item>> for doctree::Import {
25522551
if remaining.is_empty() {
25532552
return ret;
25542553
}
2555-
(ret, ImportList(resolve_use_source(cx, p.clean(cx), self.id),
2556-
remaining))
2554+
(ret, Import::List(resolve_use_source(cx, p.clean(cx), self.id), remaining))
25572555
}
25582556
hir::ViewPathSimple(name, ref p) => {
25592557
if !denied {
25602558
if let Some(items) = inline::try_inline(cx, self.id, Some(name)) {
25612559
return items;
25622560
}
25632561
}
2564-
(vec![], SimpleImport(name.clean(cx),
2565-
resolve_use_source(cx, p.clean(cx), self.id)))
2562+
(vec![], Import::Simple(name.clean(cx),
2563+
resolve_use_source(cx, p.clean(cx), self.id)))
25662564
}
25672565
};
25682566
ret.push(Item {
@@ -2582,11 +2580,11 @@ impl Clean<Vec<Item>> for doctree::Import {
25822580
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
25832581
pub enum Import {
25842582
// use source as str;
2585-
SimpleImport(String, ImportSource),
2583+
Simple(String, ImportSource),
25862584
// use source::*;
2587-
GlobImport(ImportSource),
2585+
Glob(ImportSource),
25882586
// use source::{a, b, c};
2589-
ImportList(ImportSource, Vec<ViewListIdent>),
2587+
List(ImportSource, Vec<ViewListIdent>),
25902588
}
25912589

25922590
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
@@ -2761,24 +2759,24 @@ fn register_def(cx: &DocContext, def: Def) -> DefId {
27612759
let tcx = cx.tcx();
27622760

27632761
let (did, kind) = match def {
2764-
Def::Fn(i) => (i, TypeFunction),
2765-
Def::TyAlias(i) => (i, TypeTypedef),
2766-
Def::Enum(i) => (i, TypeEnum),
2767-
Def::Trait(i) => (i, TypeTrait),
2768-
Def::Struct(i) => (i, TypeStruct),
2769-
Def::Union(i) => (i, TypeUnion),
2770-
Def::Mod(i) => (i, TypeModule),
2771-
Def::Static(i, _) => (i, TypeStatic),
2772-
Def::Variant(i) => (tcx.parent_def_id(i).unwrap(), TypeEnum),
2773-
Def::SelfTy(Some(def_id), _) => (def_id, TypeTrait),
2762+
Def::Fn(i) => (i, TypeKind::Function),
2763+
Def::TyAlias(i) => (i, TypeKind::Typedef),
2764+
Def::Enum(i) => (i, TypeKind::Enum),
2765+
Def::Trait(i) => (i, TypeKind::Trait),
2766+
Def::Struct(i) => (i, TypeKind::Struct),
2767+
Def::Union(i) => (i, TypeKind::Union),
2768+
Def::Mod(i) => (i, TypeKind::Module),
2769+
Def::Static(i, _) => (i, TypeKind::Static),
2770+
Def::Variant(i) => (tcx.parent_def_id(i).unwrap(), TypeKind::Enum),
2771+
Def::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
27742772
Def::SelfTy(_, Some(impl_def_id)) => {
27752773
return impl_def_id
27762774
}
27772775
_ => return def.def_id()
27782776
};
27792777
if did.is_local() { return did }
27802778
inline::record_extern_fqn(cx, did, kind);
2781-
if let TypeTrait = kind {
2779+
if let TypeKind::Trait = kind {
27822780
let t = inline::build_external_trait(cx, tcx, did);
27832781
cx.external_traits.borrow_mut().insert(did, t);
27842782
}
@@ -2966,7 +2964,7 @@ fn lang_struct(cx: &DocContext, did: Option<DefId>,
29662964
Some(did) => did,
29672965
None => return fallback(box t.clean(cx)),
29682966
};
2969-
inline::record_extern_fqn(cx, did, TypeStruct);
2967+
inline::record_extern_fqn(cx, did, TypeKind::Struct);
29702968
ResolvedPath {
29712969
typarams: None,
29722970
did: did,

src/librustdoc/clean/simplify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ pub fn ty_params(mut params: Vec<clean::TyParam>) -> Vec<clean::TyParam> {
141141
for param in &mut params {
142142
param.bounds = ty_bounds(mem::replace(&mut param.bounds, Vec::new()));
143143
}
144-
return params;
144+
params
145145
}
146146

147147
fn ty_bounds(bounds: Vec<clean::TyParamBound>) -> Vec<clean::TyParamBound> {

src/librustdoc/fold.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ pub trait DocFolder : Sized {
7474
VariantItem(i) => {
7575
let i2 = i.clone(); // this clone is small
7676
match i.kind {
77-
StructVariant(mut j) => {
77+
VariantKind::Struct(mut j) => {
7878
let num_fields = j.fields.len();
7979
j.fields = j.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
8080
j.fields_stripped |= num_fields != j.fields.len() ||
8181
j.fields.iter().any(|f| f.is_stripped());
82-
VariantItem(Variant {kind: StructVariant(j), ..i2})
82+
VariantItem(Variant {kind: VariantKind::Struct(j), ..i2})
8383
},
8484
_ => VariantItem(i2)
8585
}

src/librustdoc/html/format.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ impl fmt::Display for clean::Type {
457457
tybounds(f, typarams)
458458
}
459459
clean::Infer => write!(f, "_"),
460-
clean::Primitive(prim) => primitive_link(f, prim, prim.to_string()),
460+
clean::Primitive(prim) => primitive_link(f, prim, prim.as_str()),
461461
clean::BareFunction(ref decl) => {
462462
write!(f, "{}{}fn{}{}",
463463
UnsafetySpace(decl.unsafety),
@@ -708,17 +708,17 @@ impl fmt::Display for ConstnessSpace {
708708
impl fmt::Display for clean::Import {
709709
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
710710
match *self {
711-
clean::SimpleImport(ref name, ref src) => {
711+
clean::Import::Simple(ref name, ref src) => {
712712
if *name == src.path.last_name() {
713713
write!(f, "use {};", *src)
714714
} else {
715715
write!(f, "use {} as {};", *src, *name)
716716
}
717717
}
718-
clean::GlobImport(ref src) => {
718+
clean::Import::Glob(ref src) => {
719719
write!(f, "use {}::*;", *src)
720720
}
721-
clean::ImportList(ref src, ref names) => {
721+
clean::Import::List(ref src, ref names) => {
722722
write!(f, "use {}::{{", *src)?;
723723
for (i, n) in names.iter().enumerate() {
724724
if i > 0 {

0 commit comments

Comments
 (0)