Skip to content

Commit 71fb594

Browse files
authored
Rollup merge of #35867 - frewsxcv:rustdoc-cleanup, r=alexcrichton
Various refactorings in the rustdoc module. None
2 parents 4da6114 + 42e8ac8 commit 71fb594

File tree

4 files changed

+175
-149
lines changed

4 files changed

+175
-149
lines changed

src/librustdoc/clean/mod.rs

+134-104
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//! that clean them.
1313
1414
pub use self::Type::*;
15-
pub use self::PrimitiveType::*;
1615
pub use self::TypeKind::*;
1716
pub use self::VariantKind::*;
1817
pub use self::Mutability::*;
@@ -287,34 +286,34 @@ impl Item {
287286
}
288287
}
289288
pub fn is_mod(&self) -> bool {
290-
ItemType::from_item(self) == ItemType::Module
289+
ItemType::from(self) == ItemType::Module
291290
}
292291
pub fn is_trait(&self) -> bool {
293-
ItemType::from_item(self) == ItemType::Trait
292+
ItemType::from(self) == ItemType::Trait
294293
}
295294
pub fn is_struct(&self) -> bool {
296-
ItemType::from_item(self) == ItemType::Struct
295+
ItemType::from(self) == ItemType::Struct
297296
}
298297
pub fn is_enum(&self) -> bool {
299-
ItemType::from_item(self) == ItemType::Module
298+
ItemType::from(self) == ItemType::Module
300299
}
301300
pub fn is_fn(&self) -> bool {
302-
ItemType::from_item(self) == ItemType::Function
301+
ItemType::from(self) == ItemType::Function
303302
}
304303
pub fn is_associated_type(&self) -> bool {
305-
ItemType::from_item(self) == ItemType::AssociatedType
304+
ItemType::from(self) == ItemType::AssociatedType
306305
}
307306
pub fn is_associated_const(&self) -> bool {
308-
ItemType::from_item(self) == ItemType::AssociatedConst
307+
ItemType::from(self) == ItemType::AssociatedConst
309308
}
310309
pub fn is_method(&self) -> bool {
311-
ItemType::from_item(self) == ItemType::Method
310+
ItemType::from(self) == ItemType::Method
312311
}
313312
pub fn is_ty_method(&self) -> bool {
314-
ItemType::from_item(self) == ItemType::TyMethod
313+
ItemType::from(self) == ItemType::TyMethod
315314
}
316315
pub fn is_primitive(&self) -> bool {
317-
ItemType::from_item(self) == ItemType::Primitive
316+
ItemType::from(self) == ItemType::Primitive
318317
}
319318
pub fn is_stripped(&self) -> bool {
320319
match self.inner { StrippedItem(..) => true, _ => false }
@@ -380,6 +379,23 @@ pub enum ItemEnum {
380379
StrippedItem(Box<ItemEnum>),
381380
}
382381

382+
impl ItemEnum {
383+
pub fn generics(&self) -> Option<&Generics> {
384+
Some(match *self {
385+
ItemEnum::StructItem(ref s) => &s.generics,
386+
ItemEnum::EnumItem(ref e) => &e.generics,
387+
ItemEnum::FunctionItem(ref f) => &f.generics,
388+
ItemEnum::TypedefItem(ref t, _) => &t.generics,
389+
ItemEnum::TraitItem(ref t) => &t.generics,
390+
ItemEnum::ImplItem(ref i) => &i.generics,
391+
ItemEnum::TyMethodItem(ref i) => &i.generics,
392+
ItemEnum::MethodItem(ref i) => &i.generics,
393+
ItemEnum::ForeignFunctionItem(ref f) => &f.generics,
394+
_ => return None,
395+
})
396+
}
397+
}
398+
383399
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
384400
pub struct Module {
385401
pub items: Vec<Item>,
@@ -1469,8 +1485,8 @@ pub enum PrimitiveType {
14691485
Str,
14701486
Slice,
14711487
Array,
1472-
PrimitiveTuple,
1473-
PrimitiveRawPointer,
1488+
Tuple,
1489+
RawPointer,
14741490
}
14751491

14761492
#[derive(Clone, RustcEncodable, RustcDecodable, Copy, Debug)]
@@ -1500,12 +1516,12 @@ impl Type {
15001516
pub fn primitive_type(&self) -> Option<PrimitiveType> {
15011517
match *self {
15021518
Primitive(p) | BorrowedRef { type_: box Primitive(p), ..} => Some(p),
1503-
Vector(..) | BorrowedRef{ type_: box Vector(..), .. } => Some(Slice),
1519+
Vector(..) | BorrowedRef{ type_: box Vector(..), .. } => Some(PrimitiveType::Slice),
15041520
FixedVector(..) | BorrowedRef { type_: box FixedVector(..), .. } => {
1505-
Some(Array)
1521+
Some(PrimitiveType::Array)
15061522
}
1507-
Tuple(..) => Some(PrimitiveTuple),
1508-
RawPointer(..) => Some(PrimitiveRawPointer),
1523+
Tuple(..) => Some(PrimitiveType::Tuple),
1524+
RawPointer(..) => Some(PrimitiveType::RawPointer),
15091525
_ => None,
15101526
}
15111527
}
@@ -1530,25 +1546,25 @@ impl GetDefId for Type {
15301546
impl PrimitiveType {
15311547
fn from_str(s: &str) -> Option<PrimitiveType> {
15321548
match s {
1533-
"isize" => Some(Isize),
1534-
"i8" => Some(I8),
1535-
"i16" => Some(I16),
1536-
"i32" => Some(I32),
1537-
"i64" => Some(I64),
1538-
"usize" => Some(Usize),
1539-
"u8" => Some(U8),
1540-
"u16" => Some(U16),
1541-
"u32" => Some(U32),
1542-
"u64" => Some(U64),
1543-
"bool" => Some(Bool),
1544-
"char" => Some(Char),
1545-
"str" => Some(Str),
1546-
"f32" => Some(F32),
1547-
"f64" => Some(F64),
1548-
"array" => Some(Array),
1549-
"slice" => Some(Slice),
1550-
"tuple" => Some(PrimitiveTuple),
1551-
"pointer" => Some(PrimitiveRawPointer),
1549+
"isize" => Some(PrimitiveType::Isize),
1550+
"i8" => Some(PrimitiveType::I8),
1551+
"i16" => Some(PrimitiveType::I16),
1552+
"i32" => Some(PrimitiveType::I32),
1553+
"i64" => Some(PrimitiveType::I64),
1554+
"usize" => Some(PrimitiveType::Usize),
1555+
"u8" => Some(PrimitiveType::U8),
1556+
"u16" => Some(PrimitiveType::U16),
1557+
"u32" => Some(PrimitiveType::U32),
1558+
"u64" => Some(PrimitiveType::U64),
1559+
"bool" => Some(PrimitiveType::Bool),
1560+
"char" => Some(PrimitiveType::Char),
1561+
"str" => Some(PrimitiveType::Str),
1562+
"f32" => Some(PrimitiveType::F32),
1563+
"f64" => Some(PrimitiveType::F64),
1564+
"array" => Some(PrimitiveType::Array),
1565+
"slice" => Some(PrimitiveType::Slice),
1566+
"tuple" => Some(PrimitiveType::Tuple),
1567+
"pointer" => Some(PrimitiveType::RawPointer),
15521568
_ => None,
15531569
}
15541570
}
@@ -1568,25 +1584,25 @@ impl PrimitiveType {
15681584

15691585
pub fn to_string(&self) -> &'static str {
15701586
match *self {
1571-
Isize => "isize",
1572-
I8 => "i8",
1573-
I16 => "i16",
1574-
I32 => "i32",
1575-
I64 => "i64",
1576-
Usize => "usize",
1577-
U8 => "u8",
1578-
U16 => "u16",
1579-
U32 => "u32",
1580-
U64 => "u64",
1581-
F32 => "f32",
1582-
F64 => "f64",
1583-
Str => "str",
1584-
Bool => "bool",
1585-
Char => "char",
1586-
Array => "array",
1587-
Slice => "slice",
1588-
PrimitiveTuple => "tuple",
1589-
PrimitiveRawPointer => "pointer",
1587+
PrimitiveType::Isize => "isize",
1588+
PrimitiveType::I8 => "i8",
1589+
PrimitiveType::I16 => "i16",
1590+
PrimitiveType::I32 => "i32",
1591+
PrimitiveType::I64 => "i64",
1592+
PrimitiveType::Usize => "usize",
1593+
PrimitiveType::U8 => "u8",
1594+
PrimitiveType::U16 => "u16",
1595+
PrimitiveType::U32 => "u32",
1596+
PrimitiveType::U64 => "u64",
1597+
PrimitiveType::F32 => "f32",
1598+
PrimitiveType::F64 => "f64",
1599+
PrimitiveType::Str => "str",
1600+
PrimitiveType::Bool => "bool",
1601+
PrimitiveType::Char => "char",
1602+
PrimitiveType::Array => "array",
1603+
PrimitiveType::Slice => "slice",
1604+
PrimitiveType::Tuple => "tuple",
1605+
PrimitiveType::RawPointer => "pointer",
15901606
}
15911607
}
15921608

@@ -1603,6 +1619,38 @@ impl PrimitiveType {
16031619
}
16041620
}
16051621

1622+
impl From<ast::IntTy> for PrimitiveType {
1623+
fn from(int_ty: ast::IntTy) -> PrimitiveType {
1624+
match int_ty {
1625+
ast::IntTy::Is => PrimitiveType::Isize,
1626+
ast::IntTy::I8 => PrimitiveType::I8,
1627+
ast::IntTy::I16 => PrimitiveType::I16,
1628+
ast::IntTy::I32 => PrimitiveType::I32,
1629+
ast::IntTy::I64 => PrimitiveType::I64,
1630+
}
1631+
}
1632+
}
1633+
1634+
impl From<ast::UintTy> for PrimitiveType {
1635+
fn from(uint_ty: ast::UintTy) -> PrimitiveType {
1636+
match uint_ty {
1637+
ast::UintTy::Us => PrimitiveType::Usize,
1638+
ast::UintTy::U8 => PrimitiveType::U8,
1639+
ast::UintTy::U16 => PrimitiveType::U16,
1640+
ast::UintTy::U32 => PrimitiveType::U32,
1641+
ast::UintTy::U64 => PrimitiveType::U64,
1642+
}
1643+
}
1644+
}
1645+
1646+
impl From<ast::FloatTy> for PrimitiveType {
1647+
fn from(float_ty: ast::FloatTy) -> PrimitiveType {
1648+
match float_ty {
1649+
ast::FloatTy::F32 => PrimitiveType::F32,
1650+
ast::FloatTy::F64 => PrimitiveType::F64,
1651+
}
1652+
}
1653+
}
16061654

16071655
// Poor man's type parameter substitution at HIR level.
16081656
// Used to replace private type aliases in public signatures with their aliased types.
@@ -1754,21 +1802,12 @@ impl<'tcx> Clean<Type> for ty::Ty<'tcx> {
17541802
fn clean(&self, cx: &DocContext) -> Type {
17551803
match self.sty {
17561804
ty::TyNever => Never,
1757-
ty::TyBool => Primitive(Bool),
1758-
ty::TyChar => Primitive(Char),
1759-
ty::TyInt(ast::IntTy::Is) => Primitive(Isize),
1760-
ty::TyInt(ast::IntTy::I8) => Primitive(I8),
1761-
ty::TyInt(ast::IntTy::I16) => Primitive(I16),
1762-
ty::TyInt(ast::IntTy::I32) => Primitive(I32),
1763-
ty::TyInt(ast::IntTy::I64) => Primitive(I64),
1764-
ty::TyUint(ast::UintTy::Us) => Primitive(Usize),
1765-
ty::TyUint(ast::UintTy::U8) => Primitive(U8),
1766-
ty::TyUint(ast::UintTy::U16) => Primitive(U16),
1767-
ty::TyUint(ast::UintTy::U32) => Primitive(U32),
1768-
ty::TyUint(ast::UintTy::U64) => Primitive(U64),
1769-
ty::TyFloat(ast::FloatTy::F32) => Primitive(F32),
1770-
ty::TyFloat(ast::FloatTy::F64) => Primitive(F64),
1771-
ty::TyStr => Primitive(Str),
1805+
ty::TyBool => Primitive(PrimitiveType::Bool),
1806+
ty::TyChar => Primitive(PrimitiveType::Char),
1807+
ty::TyInt(int_ty) => Primitive(int_ty.into()),
1808+
ty::TyUint(uint_ty) => Primitive(uint_ty.into()),
1809+
ty::TyFloat(float_ty) => Primitive(float_ty.into()),
1810+
ty::TyStr => Primitive(PrimitiveType::Str),
17721811
ty::TyBox(t) => {
17731812
let box_did = cx.tcx_opt().and_then(|tcx| {
17741813
tcx.lang_items.owned_box()
@@ -2421,25 +2460,25 @@ fn build_deref_target_impls(cx: &DocContext,
24212460
}
24222461
};
24232462
let did = match primitive {
2424-
Isize => tcx.lang_items.isize_impl(),
2425-
I8 => tcx.lang_items.i8_impl(),
2426-
I16 => tcx.lang_items.i16_impl(),
2427-
I32 => tcx.lang_items.i32_impl(),
2428-
I64 => tcx.lang_items.i64_impl(),
2429-
Usize => tcx.lang_items.usize_impl(),
2430-
U8 => tcx.lang_items.u8_impl(),
2431-
U16 => tcx.lang_items.u16_impl(),
2432-
U32 => tcx.lang_items.u32_impl(),
2433-
U64 => tcx.lang_items.u64_impl(),
2434-
F32 => tcx.lang_items.f32_impl(),
2435-
F64 => tcx.lang_items.f64_impl(),
2436-
Char => tcx.lang_items.char_impl(),
2437-
Bool => None,
2438-
Str => tcx.lang_items.str_impl(),
2439-
Slice => tcx.lang_items.slice_impl(),
2440-
Array => tcx.lang_items.slice_impl(),
2441-
PrimitiveTuple => None,
2442-
PrimitiveRawPointer => tcx.lang_items.const_ptr_impl(),
2463+
PrimitiveType::Isize => tcx.lang_items.isize_impl(),
2464+
PrimitiveType::I8 => tcx.lang_items.i8_impl(),
2465+
PrimitiveType::I16 => tcx.lang_items.i16_impl(),
2466+
PrimitiveType::I32 => tcx.lang_items.i32_impl(),
2467+
PrimitiveType::I64 => tcx.lang_items.i64_impl(),
2468+
PrimitiveType::Usize => tcx.lang_items.usize_impl(),
2469+
PrimitiveType::U8 => tcx.lang_items.u8_impl(),
2470+
PrimitiveType::U16 => tcx.lang_items.u16_impl(),
2471+
PrimitiveType::U32 => tcx.lang_items.u32_impl(),
2472+
PrimitiveType::U64 => tcx.lang_items.u64_impl(),
2473+
PrimitiveType::F32 => tcx.lang_items.f32_impl(),
2474+
PrimitiveType::F64 => tcx.lang_items.f64_impl(),
2475+
PrimitiveType::Char => tcx.lang_items.char_impl(),
2476+
PrimitiveType::Bool => None,
2477+
PrimitiveType::Str => tcx.lang_items.str_impl(),
2478+
PrimitiveType::Slice => tcx.lang_items.slice_impl(),
2479+
PrimitiveType::Array => tcx.lang_items.slice_impl(),
2480+
PrimitiveType::Tuple => None,
2481+
PrimitiveType::RawPointer => tcx.lang_items.const_ptr_impl(),
24432482
};
24442483
if let Some(did) = did {
24452484
if !did.is_local() {
@@ -2722,21 +2761,12 @@ fn resolve_type(cx: &DocContext,
27222761

27232762
let is_generic = match def {
27242763
Def::PrimTy(p) => match p {
2725-
hir::TyStr => return Primitive(Str),
2726-
hir::TyBool => return Primitive(Bool),
2727-
hir::TyChar => return Primitive(Char),
2728-
hir::TyInt(ast::IntTy::Is) => return Primitive(Isize),
2729-
hir::TyInt(ast::IntTy::I8) => return Primitive(I8),
2730-
hir::TyInt(ast::IntTy::I16) => return Primitive(I16),
2731-
hir::TyInt(ast::IntTy::I32) => return Primitive(I32),
2732-
hir::TyInt(ast::IntTy::I64) => return Primitive(I64),
2733-
hir::TyUint(ast::UintTy::Us) => return Primitive(Usize),
2734-
hir::TyUint(ast::UintTy::U8) => return Primitive(U8),
2735-
hir::TyUint(ast::UintTy::U16) => return Primitive(U16),
2736-
hir::TyUint(ast::UintTy::U32) => return Primitive(U32),
2737-
hir::TyUint(ast::UintTy::U64) => return Primitive(U64),
2738-
hir::TyFloat(ast::FloatTy::F32) => return Primitive(F32),
2739-
hir::TyFloat(ast::FloatTy::F64) => return Primitive(F64),
2764+
hir::TyStr => return Primitive(PrimitiveType::Str),
2765+
hir::TyBool => return Primitive(PrimitiveType::Bool),
2766+
hir::TyChar => return Primitive(PrimitiveType::Char),
2767+
hir::TyInt(int_ty) => return Primitive(int_ty.into()),
2768+
hir::TyUint(uint_ty) => return Primitive(uint_ty.into()),
2769+
hir::TyFloat(float_ty) => return Primitive(float_ty.into()),
27402770
},
27412771
Def::SelfTy(..) if path.segments.len() == 1 => {
27422772
return Generic(keywords::SelfType.name().to_string());

0 commit comments

Comments
 (0)