Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor librustdoc cleanup and refactoring. #36903

Merged
merged 8 commits into from
Oct 4, 2016
20 changes: 10 additions & 10 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,49 +73,49 @@ fn try_inline_def<'a, 'tcx>(cx: &DocContext, tcx: TyCtxt<'a, 'tcx, 'tcx>,
let did = def.def_id();
let inner = match def {
Def::Trait(did) => {
record_extern_fqn(cx, did, clean::TypeTrait);
record_extern_fqn(cx, did, clean::TypeKind::Trait);
ret.extend(build_impls(cx, tcx, did));
clean::TraitItem(build_external_trait(cx, tcx, did))
}
Def::Fn(did) => {
record_extern_fqn(cx, did, clean::TypeFunction);
record_extern_fqn(cx, did, clean::TypeKind::Function);
clean::FunctionItem(build_external_function(cx, tcx, did))
}
Def::Struct(did)
// If this is a struct constructor, we skip it
if tcx.def_key(did).disambiguated_data.data != DefPathData::StructCtor => {
record_extern_fqn(cx, did, clean::TypeStruct);
record_extern_fqn(cx, did, clean::TypeKind::Struct);
ret.extend(build_impls(cx, tcx, did));
clean::StructItem(build_struct(cx, tcx, did))
}
Def::Union(did) => {
record_extern_fqn(cx, did, clean::TypeUnion);
record_extern_fqn(cx, did, clean::TypeKind::Union);
ret.extend(build_impls(cx, tcx, did));
clean::UnionItem(build_union(cx, tcx, did))
}
Def::TyAlias(did) => {
record_extern_fqn(cx, did, clean::TypeTypedef);
record_extern_fqn(cx, did, clean::TypeKind::Typedef);
ret.extend(build_impls(cx, tcx, did));
clean::TypedefItem(build_type_alias(cx, tcx, did), false)
}
Def::Enum(did) => {
record_extern_fqn(cx, did, clean::TypeEnum);
record_extern_fqn(cx, did, clean::TypeKind::Enum);
ret.extend(build_impls(cx, tcx, did));
clean::EnumItem(build_enum(cx, tcx, did))
}
// Assume that the enum type is reexported next to the variant, and
// variants don't show up in documentation specially.
Def::Variant(..) => return Some(Vec::new()),
Def::Mod(did) => {
record_extern_fqn(cx, did, clean::TypeModule);
record_extern_fqn(cx, did, clean::TypeKind::Module);
clean::ModuleItem(build_module(cx, tcx, did))
}
Def::Static(did, mtbl) => {
record_extern_fqn(cx, did, clean::TypeStatic);
record_extern_fqn(cx, did, clean::TypeKind::Static);
clean::StaticItem(build_static(cx, tcx, did, mtbl))
}
Def::Const(did) | Def::AssociatedConst(did) => {
record_extern_fqn(cx, did, clean::TypeConst);
record_extern_fqn(cx, did, clean::TypeKind::Const);
clean::ConstantItem(build_const(cx, tcx, did))
}
_ => return None,
Expand Down Expand Up @@ -577,7 +577,7 @@ fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics)
_ => true,
}
});
return g;
g
}

/// Supertrait bounds for a trait are also listed in the generics coming from
Expand Down
114 changes: 56 additions & 58 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
//! that clean them.

pub use self::Type::*;
pub use self::TypeKind::*;
pub use self::VariantKind::*;
pub use self::Mutability::*;
pub use self::Import::*;
pub use self::ItemEnum::*;
pub use self::Attribute::*;
pub use self::TyParamBound::*;
Expand Down Expand Up @@ -319,7 +316,7 @@ impl Item {
match self.inner {
StructItem(ref _struct) => Some(_struct.fields_stripped),
UnionItem(ref union) => Some(union.fields_stripped),
VariantItem(Variant { kind: StructVariant(ref vstruct)} ) => {
VariantItem(Variant { kind: VariantKind::Struct(ref vstruct)} ) => {
Some(vstruct.fields_stripped)
},
_ => None,
Expand Down Expand Up @@ -688,7 +685,7 @@ impl Clean<TyParamBound> for ty::BuiltinBound {
(tcx.lang_items.sync_trait().unwrap(),
external_path(cx, "Sync", None, false, vec![], empty)),
};
inline::record_extern_fqn(cx, did, TypeTrait);
inline::record_extern_fqn(cx, did, TypeKind::Trait);
TraitBound(PolyTrait {
trait_: ResolvedPath {
path: path,
Expand All @@ -707,7 +704,7 @@ impl<'tcx> Clean<TyParamBound> for ty::TraitRef<'tcx> {
Some(tcx) => tcx,
None => return RegionBound(Lifetime::statik())
};
inline::record_extern_fqn(cx, self.def_id, TypeTrait);
inline::record_extern_fqn(cx, self.def_id, TypeKind::Trait);
let path = external_path(cx, &tcx.item_name(self.def_id).as_str(),
Some(self.def_id), true, vec![], self.substs);

Expand Down Expand Up @@ -765,7 +762,7 @@ impl Lifetime {
pub fn get_ref<'a>(&'a self) -> &'a str {
let Lifetime(ref s) = *self;
let s: &'a str = s;
return s;
s
}

pub fn statik() -> Lifetime {
Expand Down Expand Up @@ -1130,7 +1127,7 @@ pub struct FnDecl {

impl FnDecl {
pub fn has_self(&self) -> bool {
return self.inputs.values.len() > 0 && self.inputs.values[0].name == "self";
self.inputs.values.len() > 0 && self.inputs.values[0].name == "self"
}

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

#[derive(Clone, RustcEncodable, RustcDecodable, Copy, Debug)]
pub enum TypeKind {
TypeEnum,
TypeFunction,
TypeModule,
TypeConst,
TypeStatic,
TypeStruct,
TypeUnion,
TypeTrait,
TypeVariant,
TypeTypedef,
Enum,
Function,
Module,
Const,
Static,
Struct,
Union,
Trait,
Variant,
Typedef,
}

pub trait GetDefId {
Expand Down Expand Up @@ -1572,7 +1569,7 @@ impl PrimitiveType {
None
}

pub fn to_string(&self) -> &'static str {
pub fn as_str(&self) -> &'static str {
match *self {
PrimitiveType::Isize => "isize",
PrimitiveType::I8 => "i8",
Expand All @@ -1597,7 +1594,7 @@ impl PrimitiveType {
}

pub fn to_url_str(&self) -> &'static str {
self.to_string()
self.as_str()
}

/// Creates a rustdoc-specific node id for primitive types.
Expand Down Expand Up @@ -1795,9 +1792,9 @@ impl<'tcx> Clean<Type> for ty::Ty<'tcx> {
ty::TyAdt(def, substs) => {
let did = def.did;
let kind = match def.adt_kind() {
AdtKind::Struct => TypeStruct,
AdtKind::Union => TypeUnion,
AdtKind::Enum => TypeEnum,
AdtKind::Struct => TypeKind::Struct,
AdtKind::Union => TypeKind::Union,
AdtKind::Enum => TypeKind::Enum,
};
inline::record_extern_fqn(cx, did, kind);
let path = external_path(cx, &cx.tcx().item_name(did).as_str(),
Expand All @@ -1811,7 +1808,7 @@ impl<'tcx> Clean<Type> for ty::Ty<'tcx> {
}
ty::TyTrait(ref obj) => {
let did = obj.principal.def_id();
inline::record_extern_fqn(cx, did, TypeTrait);
inline::record_extern_fqn(cx, did, TypeKind::Trait);

let mut typarams = vec![];
obj.region_bound.clean(cx).map(|b| typarams.push(RegionBound(b)));
Expand Down Expand Up @@ -2027,7 +2024,7 @@ impl Clean<Item> for doctree::Variant {
deprecation: self.depr.clean(cx),
def_id: cx.map.local_def_id(self.def.id()),
inner: VariantItem(Variant {
kind: struct_def_to_variant_kind(&self.def, cx),
kind: self.def.clean(cx),
}),
}
}
Expand All @@ -2036,14 +2033,14 @@ impl Clean<Item> for doctree::Variant {
impl<'tcx> Clean<Item> for ty::VariantDefData<'tcx, 'static> {
fn clean(&self, cx: &DocContext) -> Item {
let kind = match self.kind {
ty::VariantKind::Unit => CLikeVariant,
ty::VariantKind::Unit => VariantKind::CLike,
ty::VariantKind::Tuple => {
TupleVariant(
VariantKind::Tuple(
self.fields.iter().map(|f| f.unsubst_ty().clean(cx)).collect()
)
}
ty::VariantKind::Struct => {
StructVariant(VariantStruct {
VariantKind::Struct(VariantStruct {
struct_type: doctree::Plain,
fields_stripped: false,
fields: self.fields.iter().map(|field| {
Expand Down Expand Up @@ -2076,18 +2073,20 @@ impl<'tcx> Clean<Item> for ty::VariantDefData<'tcx, 'static> {

#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum VariantKind {
CLikeVariant,
TupleVariant(Vec<Type>),
StructVariant(VariantStruct),
CLike,
Tuple(Vec<Type>),
Struct(VariantStruct),
}

fn struct_def_to_variant_kind(struct_def: &hir::VariantData, cx: &DocContext) -> VariantKind {
if struct_def.is_struct() {
StructVariant(struct_def.clean(cx))
} else if struct_def.is_unit() {
CLikeVariant
} else {
TupleVariant(struct_def.fields().iter().map(|x| x.ty.clean(cx)).collect())
impl Clean<VariantKind> for hir::VariantData {
fn clean(&self, cx: &DocContext) -> VariantKind {
if self.is_struct() {
VariantKind::Struct(self.clean(cx))
} else if self.is_unit() {
VariantKind::CLike
} else {
VariantKind::Tuple(self.fields().iter().map(|x| x.ty.clean(cx)).collect())
}
}
}

Expand Down Expand Up @@ -2526,7 +2525,7 @@ impl Clean<Vec<Item>> for doctree::Import {
});
let (mut ret, inner) = match self.node {
hir::ViewPathGlob(ref p) => {
(vec![], GlobImport(resolve_use_source(cx, p.clean(cx), self.id)))
(vec![], Import::Glob(resolve_use_source(cx, p.clean(cx), self.id)))
}
hir::ViewPathList(ref p, ref list) => {
// Attempt to inline all reexported items, but be sure
Expand All @@ -2552,17 +2551,16 @@ impl Clean<Vec<Item>> for doctree::Import {
if remaining.is_empty() {
return ret;
}
(ret, ImportList(resolve_use_source(cx, p.clean(cx), self.id),
remaining))
(ret, Import::List(resolve_use_source(cx, p.clean(cx), self.id), remaining))
}
hir::ViewPathSimple(name, ref p) => {
if !denied {
if let Some(items) = inline::try_inline(cx, self.id, Some(name)) {
return items;
}
}
(vec![], SimpleImport(name.clean(cx),
resolve_use_source(cx, p.clean(cx), self.id)))
(vec![], Import::Simple(name.clean(cx),
resolve_use_source(cx, p.clean(cx), self.id)))
}
};
ret.push(Item {
Expand All @@ -2582,11 +2580,11 @@ impl Clean<Vec<Item>> for doctree::Import {
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
pub enum Import {
// use source as str;
SimpleImport(String, ImportSource),
Simple(String, ImportSource),
// use source::*;
GlobImport(ImportSource),
Glob(ImportSource),
// use source::{a, b, c};
ImportList(ImportSource, Vec<ViewListIdent>),
List(ImportSource, Vec<ViewListIdent>),
}

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

let (did, kind) = match def {
Def::Fn(i) => (i, TypeFunction),
Def::TyAlias(i) => (i, TypeTypedef),
Def::Enum(i) => (i, TypeEnum),
Def::Trait(i) => (i, TypeTrait),
Def::Struct(i) => (i, TypeStruct),
Def::Union(i) => (i, TypeUnion),
Def::Mod(i) => (i, TypeModule),
Def::Static(i, _) => (i, TypeStatic),
Def::Variant(i) => (tcx.parent_def_id(i).unwrap(), TypeEnum),
Def::SelfTy(Some(def_id), _) => (def_id, TypeTrait),
Def::Fn(i) => (i, TypeKind::Function),
Def::TyAlias(i) => (i, TypeKind::Typedef),
Def::Enum(i) => (i, TypeKind::Enum),
Def::Trait(i) => (i, TypeKind::Trait),
Def::Struct(i) => (i, TypeKind::Struct),
Def::Union(i) => (i, TypeKind::Union),
Def::Mod(i) => (i, TypeKind::Module),
Def::Static(i, _) => (i, TypeKind::Static),
Def::Variant(i) => (tcx.parent_def_id(i).unwrap(), TypeKind::Enum),
Def::SelfTy(Some(def_id), _) => (def_id, TypeKind::Trait),
Def::SelfTy(_, Some(impl_def_id)) => {
return impl_def_id
}
_ => return def.def_id()
};
if did.is_local() { return did }
inline::record_extern_fqn(cx, did, kind);
if let TypeTrait = kind {
if let TypeKind::Trait = kind {
let t = inline::build_external_trait(cx, tcx, did);
cx.external_traits.borrow_mut().insert(did, t);
}
Expand Down Expand Up @@ -2966,7 +2964,7 @@ fn lang_struct(cx: &DocContext, did: Option<DefId>,
Some(did) => did,
None => return fallback(box t.clean(cx)),
};
inline::record_extern_fqn(cx, did, TypeStruct);
inline::record_extern_fqn(cx, did, TypeKind::Struct);
ResolvedPath {
typarams: None,
did: did,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub fn ty_params(mut params: Vec<clean::TyParam>) -> Vec<clean::TyParam> {
for param in &mut params {
param.bounds = ty_bounds(mem::replace(&mut param.bounds, Vec::new()));
}
return params;
params
}

fn ty_bounds(bounds: Vec<clean::TyParamBound>) -> Vec<clean::TyParamBound> {
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ pub trait DocFolder : Sized {
VariantItem(i) => {
let i2 = i.clone(); // this clone is small
match i.kind {
StructVariant(mut j) => {
VariantKind::Struct(mut j) => {
let num_fields = j.fields.len();
j.fields = j.fields.into_iter().filter_map(|x| self.fold_item(x)).collect();
j.fields_stripped |= num_fields != j.fields.len() ||
j.fields.iter().any(|f| f.is_stripped());
VariantItem(Variant {kind: StructVariant(j), ..i2})
VariantItem(Variant {kind: VariantKind::Struct(j), ..i2})
},
_ => VariantItem(i2)
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ impl fmt::Display for clean::Type {
tybounds(f, typarams)
}
clean::Infer => write!(f, "_"),
clean::Primitive(prim) => primitive_link(f, prim, prim.to_string()),
clean::Primitive(prim) => primitive_link(f, prim, prim.as_str()),
clean::BareFunction(ref decl) => {
write!(f, "{}{}fn{}{}",
UnsafetySpace(decl.unsafety),
Expand Down Expand Up @@ -708,17 +708,17 @@ impl fmt::Display for ConstnessSpace {
impl fmt::Display for clean::Import {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
clean::SimpleImport(ref name, ref src) => {
clean::Import::Simple(ref name, ref src) => {
if *name == src.path.last_name() {
write!(f, "use {};", *src)
} else {
write!(f, "use {} as {};", *src, *name)
}
}
clean::GlobImport(ref src) => {
clean::Import::Glob(ref src) => {
write!(f, "use {}::*;", *src)
}
clean::ImportList(ref src, ref names) => {
clean::Import::List(ref src, ref names) => {
write!(f, "use {}::{{", *src)?;
for (i, n) in names.iter().enumerate() {
if i > 0 {
Expand Down
Loading