Skip to content

Commit a8c1812

Browse files
committed
rollup merge of #19503: lifthrasiir/xenophobic-rustdoc
This series of commits deals with broken links to the source code. It also refactors some repetitive codes from Rustdoc. The most important commit, 1cb1f00, describes the rationale; this will fix a half of #16289. Other commits are reasonably independent to each other and can be made into indiviudal PRs at the request. ### Notes on the broken source links As of bda97e8 (I've used this to check the PR works as intended), there are 281 (!) such broken links. They can be further classified as follows: * 178 links to incorrect item types. This is the first half of #16289, and this PR fixes all of them. * 89 links to redirect pages. They are not technically "broken" but still doesn't give a source code. I have a fix for this in mind, which would make a redirect page slightly *fat*. * 14 links to incorrect `DefId` in the `gotosrc` parameter. This is #15309, and affects many `liballoc` reexports in `libstd` but *nothing else* (curiously). I'm yet to track this down; might be a metadata bug (not sure). * 0 links to the crate reexported as a different name. This is the second half of #16289, and seems not hard to fix but I'm running out of time. Prevalence of this kind of bugs calls for a full link verifier integrated into the testing process. :S
2 parents fdb3956 + 1068855 commit a8c1812

File tree

4 files changed

+132
-125
lines changed

4 files changed

+132
-125
lines changed

src/librustdoc/html/format.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use syntax::ast_util;
2323

2424
use clean;
2525
use stability_summary::ModuleSummary;
26-
use html::item_type;
2726
use html::item_type::ItemType;
2827
use html::render;
2928
use html::render::{cache, CURRENT_LOCATION_KEY};
@@ -283,7 +282,7 @@ fn path(w: &mut fmt::Formatter, path: &clean::Path, print_all: bool,
283282
url.push_str("/");
284283
}
285284
match shortty {
286-
item_type::Module => {
285+
ItemType::Module => {
287286
url.push_str(fqp.last().unwrap().as_slice());
288287
url.push_str("/index.html");
289288
}

src/librustdoc/html/item_type.rs

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
//! Item types.
12-
pub use self::ItemType::*;
1312
1413
use std::fmt;
1514
use clean;
@@ -35,36 +34,70 @@ pub enum ItemType {
3534
Method = 10,
3635
StructField = 11,
3736
Variant = 12,
38-
ForeignFunction = 13,
39-
ForeignStatic = 14,
37+
// we used to have ForeignFunction and ForeignStatic. they are retired now.
4038
Macro = 15,
4139
Primitive = 16,
4240
AssociatedType = 17,
4341
Constant = 18,
4442
}
4543

4644
impl ItemType {
45+
pub fn from_item(item: &clean::Item) -> ItemType {
46+
match item.inner {
47+
clean::ModuleItem(..) => ItemType::Module,
48+
clean::StructItem(..) => ItemType::Struct,
49+
clean::EnumItem(..) => ItemType::Enum,
50+
clean::FunctionItem(..) => ItemType::Function,
51+
clean::TypedefItem(..) => ItemType::Typedef,
52+
clean::StaticItem(..) => ItemType::Static,
53+
clean::ConstantItem(..) => ItemType::Constant,
54+
clean::TraitItem(..) => ItemType::Trait,
55+
clean::ImplItem(..) => ItemType::Impl,
56+
clean::ViewItemItem(..) => ItemType::ViewItem,
57+
clean::TyMethodItem(..) => ItemType::TyMethod,
58+
clean::MethodItem(..) => ItemType::Method,
59+
clean::StructFieldItem(..) => ItemType::StructField,
60+
clean::VariantItem(..) => ItemType::Variant,
61+
clean::ForeignFunctionItem(..) => ItemType::Function, // no ForeignFunction
62+
clean::ForeignStaticItem(..) => ItemType::Static, // no ForeignStatic
63+
clean::MacroItem(..) => ItemType::Macro,
64+
clean::PrimitiveItem(..) => ItemType::Primitive,
65+
clean::AssociatedTypeItem(..) => ItemType::AssociatedType,
66+
}
67+
}
68+
69+
pub fn from_type_kind(kind: clean::TypeKind) -> ItemType {
70+
match kind {
71+
clean::TypeStruct => ItemType::Struct,
72+
clean::TypeEnum => ItemType::Enum,
73+
clean::TypeFunction => ItemType::Function,
74+
clean::TypeTrait => ItemType::Trait,
75+
clean::TypeModule => ItemType::Module,
76+
clean::TypeStatic => ItemType::Static,
77+
clean::TypeVariant => ItemType::Variant,
78+
clean::TypeTypedef => ItemType::Typedef,
79+
}
80+
}
81+
4782
pub fn to_static_str(&self) -> &'static str {
4883
match *self {
49-
Module => "mod",
50-
Struct => "struct",
51-
Enum => "enum",
52-
Function => "fn",
53-
Typedef => "type",
54-
Static => "static",
55-
Trait => "trait",
56-
Impl => "impl",
57-
ViewItem => "viewitem",
58-
TyMethod => "tymethod",
59-
Method => "method",
60-
StructField => "structfield",
61-
Variant => "variant",
62-
ForeignFunction => "ffi",
63-
ForeignStatic => "ffs",
64-
Macro => "macro",
65-
Primitive => "primitive",
66-
AssociatedType => "associatedtype",
67-
Constant => "constant",
84+
ItemType::Module => "mod",
85+
ItemType::Struct => "struct",
86+
ItemType::Enum => "enum",
87+
ItemType::Function => "fn",
88+
ItemType::Typedef => "type",
89+
ItemType::Static => "static",
90+
ItemType::Trait => "trait",
91+
ItemType::Impl => "impl",
92+
ItemType::ViewItem => "viewitem",
93+
ItemType::TyMethod => "tymethod",
94+
ItemType::Method => "method",
95+
ItemType::StructField => "structfield",
96+
ItemType::Variant => "variant",
97+
ItemType::Macro => "macro",
98+
ItemType::Primitive => "primitive",
99+
ItemType::AssociatedType => "associatedtype",
100+
ItemType::Constant => "constant",
68101
}
69102
}
70103
}
@@ -75,27 +108,3 @@ impl fmt::Show for ItemType {
75108
}
76109
}
77110

78-
pub fn shortty(item: &clean::Item) -> ItemType {
79-
match item.inner {
80-
clean::ModuleItem(..) => Module,
81-
clean::StructItem(..) => Struct,
82-
clean::EnumItem(..) => Enum,
83-
clean::FunctionItem(..) => Function,
84-
clean::TypedefItem(..) => Typedef,
85-
clean::StaticItem(..) => Static,
86-
clean::ConstantItem(..) => Constant,
87-
clean::TraitItem(..) => Trait,
88-
clean::ImplItem(..) => Impl,
89-
clean::ViewItemItem(..) => ViewItem,
90-
clean::TyMethodItem(..) => TyMethod,
91-
clean::MethodItem(..) => Method,
92-
clean::StructFieldItem(..) => StructField,
93-
clean::VariantItem(..) => Variant,
94-
clean::ForeignFunctionItem(..) => ForeignFunction,
95-
clean::ForeignStaticItem(..) => ForeignStatic,
96-
clean::MacroItem(..) => Macro,
97-
clean::PrimitiveItem(..) => Primitive,
98-
clean::AssociatedTypeItem(..) => AssociatedType,
99-
}
100-
}
101-

src/librustdoc/html/render.rs

Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ use fold::DocFolder;
6161
use html::format::{VisSpace, Method, FnStyleSpace, MutableSpace, Stability};
6262
use html::format::{ConciseStability, TyParamBounds, WhereClause};
6363
use html::highlight;
64-
use html::item_type::{ItemType, shortty};
65-
use html::item_type;
64+
use html::item_type::ItemType;
6665
use html::layout;
6766
use html::markdown::Markdown;
6867
use html::markdown;
@@ -314,19 +313,8 @@ pub fn run(mut krate: clean::Crate,
314313
let paths: HashMap<ast::DefId, (Vec<String>, ItemType)> =
315314
analysis.as_ref().map(|a| {
316315
let paths = a.external_paths.borrow_mut().take().unwrap();
317-
paths.into_iter().map(|(k, (v, t))| {
318-
(k, (v, match t {
319-
clean::TypeStruct => item_type::Struct,
320-
clean::TypeEnum => item_type::Enum,
321-
clean::TypeFunction => item_type::Function,
322-
clean::TypeTrait => item_type::Trait,
323-
clean::TypeModule => item_type::Module,
324-
clean::TypeStatic => item_type::Static,
325-
clean::TypeVariant => item_type::Variant,
326-
clean::TypeTypedef => item_type::Typedef,
327-
}))
328-
}).collect()
329-
}).unwrap_or(HashMap::new());
316+
paths.into_iter().map(|(k, (v, t))| (k, (v, ItemType::from_type_kind(t)))).collect()
317+
}).unwrap_or(HashMap::new());
330318
let mut cache = Cache {
331319
impls: HashMap::new(),
332320
external_paths: paths.iter().map(|(&k, v)| (k, v.ref0().clone()))
@@ -359,7 +347,7 @@ pub fn run(mut krate: clean::Crate,
359347
for &(n, ref e) in krate.externs.iter() {
360348
cache.extern_locations.insert(n, extern_location(e, &cx.dst));
361349
let did = ast::DefId { krate: n, node: ast::CRATE_NODE_ID };
362-
cache.paths.insert(did, (vec![e.name.to_string()], item_type::Module));
350+
cache.paths.insert(did, (vec![e.name.to_string()], ItemType::Module));
363351
}
364352

365353
// Cache where all known primitives have their documentation located.
@@ -642,6 +630,11 @@ fn mkdir(path: &Path) -> io::IoResult<()> {
642630
}
643631
}
644632

633+
/// Returns a documentation-level item type from the item.
634+
fn shortty(item: &clean::Item) -> ItemType {
635+
ItemType::from_item(item)
636+
}
637+
645638
/// Takes a path to a source file and cleans the path to it. This canonicalizes
646639
/// things like ".." to components which preserve the "top down" hierarchy of a
647640
/// static HTML tree.
@@ -855,13 +848,13 @@ impl DocFolder for Cache {
855848
let last = self.parent_stack.last().unwrap();
856849
let did = *last;
857850
let path = match self.paths.get(&did) {
858-
Some(&(_, item_type::Trait)) =>
851+
Some(&(_, ItemType::Trait)) =>
859852
Some(self.stack[..self.stack.len() - 1]),
860853
// The current stack not necessarily has correlation for
861854
// where the type was defined. On the other hand,
862855
// `paths` always has the right information if present.
863-
Some(&(ref fqp, item_type::Struct)) |
864-
Some(&(ref fqp, item_type::Enum)) =>
856+
Some(&(ref fqp, ItemType::Struct)) |
857+
Some(&(ref fqp, ItemType::Enum)) =>
865858
Some(fqp[..fqp.len() - 1]),
866859
Some(..) => Some(self.stack.as_slice()),
867860
None => None
@@ -929,7 +922,7 @@ impl DocFolder for Cache {
929922
clean::VariantItem(..) if !self.privmod => {
930923
let mut stack = self.stack.clone();
931924
stack.pop();
932-
self.paths.insert(item.def_id, (stack, item_type::Enum));
925+
self.paths.insert(item.def_id, (stack, ItemType::Enum));
933926
}
934927

935928
clean::PrimitiveItem(..) if item.visibility.is_some() => {
@@ -1251,6 +1244,10 @@ impl Context {
12511244
for item in m.items.iter() {
12521245
if self.ignore_private_item(item) { continue }
12531246

1247+
// avoid putting foreign items to the sidebar.
1248+
if let &clean::ForeignFunctionItem(..) = &item.inner { continue }
1249+
if let &clean::ForeignStaticItem(..) = &item.inner { continue }
1250+
12541251
let short = shortty(item).to_static_str();
12551252
let myname = match item.name {
12561253
None => continue,
@@ -1435,7 +1432,8 @@ impl<'a> fmt::Show for Item<'a> {
14351432
clean::TypedefItem(ref t) => item_typedef(fmt, self.item, t),
14361433
clean::MacroItem(ref m) => item_macro(fmt, self.item, m),
14371434
clean::PrimitiveItem(ref p) => item_primitive(fmt, self.item, p),
1438-
clean::StaticItem(ref i) => item_static(fmt, self.item, i),
1435+
clean::StaticItem(ref i) | clean::ForeignStaticItem(ref i) =>
1436+
item_static(fmt, self.item, i),
14391437
clean::ConstantItem(ref c) => item_constant(fmt, self.item, c),
14401438
_ => Ok(())
14411439
}
@@ -1490,45 +1488,48 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
14901488
!cx.ignore_private_item(&items[*i])
14911489
}).collect::<Vec<uint>>();
14921490

1491+
// the order of item types in the listing
1492+
fn reorder(ty: ItemType) -> u8 {
1493+
match ty {
1494+
ItemType::ViewItem => 0,
1495+
ItemType::Primitive => 1,
1496+
ItemType::Module => 2,
1497+
ItemType::Macro => 3,
1498+
ItemType::Struct => 4,
1499+
ItemType::Enum => 5,
1500+
ItemType::Constant => 6,
1501+
ItemType::Static => 7,
1502+
ItemType::Trait => 8,
1503+
ItemType::Function => 9,
1504+
ItemType::Typedef => 10,
1505+
_ => 11 + ty as u8,
1506+
}
1507+
}
1508+
14931509
fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: uint, idx2: uint) -> Ordering {
1494-
if shortty(i1) == shortty(i2) {
1510+
let ty1 = shortty(i1);
1511+
let ty2 = shortty(i2);
1512+
if ty1 == ty2 {
14951513
return i1.name.cmp(&i2.name);
14961514
}
1497-
match (&i1.inner, &i2.inner) {
1498-
(&clean::ViewItemItem(ref a), &clean::ViewItemItem(ref b)) => {
1499-
match (&a.inner, &b.inner) {
1500-
(&clean::ExternCrate(..), _) => Less,
1501-
(_, &clean::ExternCrate(..)) => Greater,
1502-
_ => idx1.cmp(&idx2),
1515+
1516+
let tycmp = reorder(ty1).cmp(&reorder(ty2));
1517+
if let Equal = tycmp {
1518+
// for reexports, `extern crate` takes precedence.
1519+
match (&i1.inner, &i2.inner) {
1520+
(&clean::ViewItemItem(ref a), &clean::ViewItemItem(ref b)) => {
1521+
match (&a.inner, &b.inner) {
1522+
(&clean::ExternCrate(..), _) => return Less,
1523+
(_, &clean::ExternCrate(..)) => return Greater,
1524+
_ => {}
1525+
}
15031526
}
1527+
(_, _) => {}
15041528
}
1505-
(&clean::ViewItemItem(..), _) => Less,
1506-
(_, &clean::ViewItemItem(..)) => Greater,
1507-
(&clean::PrimitiveItem(..), _) => Less,
1508-
(_, &clean::PrimitiveItem(..)) => Greater,
1509-
(&clean::ModuleItem(..), _) => Less,
1510-
(_, &clean::ModuleItem(..)) => Greater,
1511-
(&clean::MacroItem(..), _) => Less,
1512-
(_, &clean::MacroItem(..)) => Greater,
1513-
(&clean::StructItem(..), _) => Less,
1514-
(_, &clean::StructItem(..)) => Greater,
1515-
(&clean::EnumItem(..), _) => Less,
1516-
(_, &clean::EnumItem(..)) => Greater,
1517-
(&clean::ConstantItem(..), _) => Less,
1518-
(_, &clean::ConstantItem(..)) => Greater,
1519-
(&clean::StaticItem(..), _) => Less,
1520-
(_, &clean::StaticItem(..)) => Greater,
1521-
(&clean::ForeignFunctionItem(..), _) => Less,
1522-
(_, &clean::ForeignFunctionItem(..)) => Greater,
1523-
(&clean::ForeignStaticItem(..), _) => Less,
1524-
(_, &clean::ForeignStaticItem(..)) => Greater,
1525-
(&clean::TraitItem(..), _) => Less,
1526-
(_, &clean::TraitItem(..)) => Greater,
1527-
(&clean::FunctionItem(..), _) => Less,
1528-
(_, &clean::FunctionItem(..)) => Greater,
1529-
(&clean::TypedefItem(..), _) => Less,
1530-
(_, &clean::TypedefItem(..)) => Greater,
1531-
_ => idx1.cmp(&idx2),
1529+
1530+
idx1.cmp(&idx2)
1531+
} else {
1532+
tycmp
15321533
}
15331534
}
15341535

@@ -1545,26 +1546,24 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
15451546
try!(write!(w, "</table>"));
15461547
}
15471548
curty = myty;
1548-
let (short, name) = match myitem.inner {
1549-
clean::ModuleItem(..) => ("modules", "Modules"),
1550-
clean::StructItem(..) => ("structs", "Structs"),
1551-
clean::EnumItem(..) => ("enums", "Enums"),
1552-
clean::FunctionItem(..) => ("functions", "Functions"),
1553-
clean::TypedefItem(..) => ("types", "Type Definitions"),
1554-
clean::StaticItem(..) => ("statics", "Statics"),
1555-
clean::ConstantItem(..) => ("constants", "Constants"),
1556-
clean::TraitItem(..) => ("traits", "Traits"),
1557-
clean::ImplItem(..) => ("impls", "Implementations"),
1558-
clean::ViewItemItem(..) => ("reexports", "Reexports"),
1559-
clean::TyMethodItem(..) => ("tymethods", "Type Methods"),
1560-
clean::MethodItem(..) => ("methods", "Methods"),
1561-
clean::StructFieldItem(..) => ("fields", "Struct Fields"),
1562-
clean::VariantItem(..) => ("variants", "Variants"),
1563-
clean::ForeignFunctionItem(..) => ("ffi-fns", "Foreign Functions"),
1564-
clean::ForeignStaticItem(..) => ("ffi-statics", "Foreign Statics"),
1565-
clean::MacroItem(..) => ("macros", "Macros"),
1566-
clean::PrimitiveItem(..) => ("primitives", "Primitive Types"),
1567-
clean::AssociatedTypeItem(..) => ("associated-types", "Associated Types"),
1549+
let (short, name) = match myty.unwrap() {
1550+
ItemType::Module => ("modules", "Modules"),
1551+
ItemType::Struct => ("structs", "Structs"),
1552+
ItemType::Enum => ("enums", "Enums"),
1553+
ItemType::Function => ("functions", "Functions"),
1554+
ItemType::Typedef => ("types", "Type Definitions"),
1555+
ItemType::Static => ("statics", "Statics"),
1556+
ItemType::Constant => ("constants", "Constants"),
1557+
ItemType::Trait => ("traits", "Traits"),
1558+
ItemType::Impl => ("impls", "Implementations"),
1559+
ItemType::ViewItem => ("reexports", "Reexports"),
1560+
ItemType::TyMethod => ("tymethods", "Type Methods"),
1561+
ItemType::Method => ("methods", "Methods"),
1562+
ItemType::StructField => ("fields", "Struct Fields"),
1563+
ItemType::Variant => ("variants", "Variants"),
1564+
ItemType::Macro => ("macros", "Macros"),
1565+
ItemType::Primitive => ("primitives", "Primitive Types"),
1566+
ItemType::AssociatedType => ("associated-types", "Associated Types"),
15681567
};
15691568
try!(write!(w,
15701569
"<h2 id='{id}' class='section-header'>\

src/librustdoc/html/static/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,8 @@
566566
"method",
567567
"structfield",
568568
"variant",
569-
"ffi",
570-
"ffs",
569+
"ffi", // retained for backward compatibility
570+
"ffs", // retained for backward compatibility
571571
"macro",
572572
"primitive",
573573
"associatedtype",

0 commit comments

Comments
 (0)