Skip to content

Commit 57bcabc

Browse files
Generate alias file
1 parent 654cb84 commit 57bcabc

File tree

6 files changed

+87
-7
lines changed

6 files changed

+87
-7
lines changed

src/librustdoc/html/item_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use clean;
1919
/// discriminants. JavaScript then is used to decode them into the original value.
2020
/// Consequently, every change to this type should be synchronized to
2121
/// the `itemTypes` mapping table in `static/main.js`.
22-
#[derive(Copy, PartialEq, Clone)]
22+
#[derive(Copy, PartialEq, Clone, Debug)]
2323
pub enum ItemType {
2424
Module = 0,
2525
ExternCrate = 1,

src/librustdoc/html/layout.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
145145
</script>\
146146
<script src=\"{root_path}main{suffix}.js\"></script>\
147147
<script defer src=\"{root_path}search-index.js\"></script>\
148+
<script defer src=\"{root_path}aliases.js\"></script>\
148149
</body>\
149150
</html>",
150151
css_extension = if css_file_extension {

src/librustdoc/html/render.rs

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ pub struct Cache {
329329
// yet when its implementation methods are being indexed. Caches such methods
330330
// and their parent id here and indexes them at the end of crate parsing.
331331
orphan_impl_items: Vec<(DefId, clean::Item)>,
332+
333+
/// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias,
334+
/// we need the alias element to have an array of items.
335+
aliases: FxHashMap<String, Vec<IndexItem>>,
332336
}
333337

334338
/// Temporary storage for data obtained during `RustdocVisitor::clean()`.
@@ -369,6 +373,7 @@ struct Sidebar<'a> { cx: &'a Context, item: &'a clean::Item, }
369373

370374
/// Struct representing one entry in the JS search index. These are all emitted
371375
/// by hand to a large JS file at the end of cache-creation.
376+
#[derive(Debug)]
372377
struct IndexItem {
373378
ty: ItemType,
374379
name: String,
@@ -396,6 +401,7 @@ impl ToJson for IndexItem {
396401
}
397402

398403
/// A type used for the search index.
404+
#[derive(Debug)]
399405
struct Type {
400406
name: Option<String>,
401407
generics: Option<Vec<String>>,
@@ -418,9 +424,10 @@ impl ToJson for Type {
418424
}
419425

420426
/// Full type of functions/methods in the search index.
427+
#[derive(Debug)]
421428
struct IndexItemFunctionType {
422429
inputs: Vec<Type>,
423-
output: Option<Type>
430+
output: Option<Type>,
424431
}
425432

426433
impl ToJson for IndexItemFunctionType {
@@ -609,6 +616,7 @@ pub fn run(mut krate: clean::Crate,
609616
owned_box_did,
610617
masked_crates: mem::replace(&mut krate.masked_crates, FxHashSet()),
611618
typarams: external_typarams,
619+
aliases: FxHashMap(),
612620
};
613621

614622
// Cache where all our extern crates are located
@@ -847,8 +855,7 @@ themePicker.onclick = function() {{
847855
write(cx.dst.join("COPYRIGHT.txt"),
848856
include_bytes!("static/COPYRIGHT.txt"))?;
849857

850-
fn collect(path: &Path, krate: &str,
851-
key: &str) -> io::Result<Vec<String>> {
858+
fn collect(path: &Path, krate: &str, key: &str) -> io::Result<Vec<String>> {
852859
let mut ret = Vec::new();
853860
if path.exists() {
854861
for line in BufReader::new(File::open(path)?).lines() {
@@ -865,6 +872,36 @@ themePicker.onclick = function() {{
865872
Ok(ret)
866873
}
867874

875+
fn show_item(item: &IndexItem, krate: &str) -> String {
876+
format!("{{'crate':'{}','ty':'{}','name':'{}','path':'{}','parent':{}}}",
877+
krate, item.ty, item.name, item.path,
878+
if let Some(p) = item.parent_idx { p.to_string() } else { "null".to_owned() })
879+
}
880+
881+
let dst = cx.dst.join("aliases.js");
882+
{
883+
let mut all_aliases = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst);
884+
let mut w = try_err!(File::create(&dst), &dst);
885+
let mut output = String::with_capacity(100);
886+
for (alias, items) in &cache.aliases {
887+
if items.is_empty() {
888+
continue
889+
}
890+
output.push_str(&format!("\"{}\":[{}],",
891+
alias,
892+
items.iter()
893+
.map(|v| show_item(v, &krate.name))
894+
.collect::<Vec<_>>()
895+
.join(",")));
896+
}
897+
all_aliases.push(format!("ALIASES['{}'] = {{{}}};", krate.name, output));
898+
all_aliases.sort();
899+
try_err!(writeln!(&mut w, "var ALIASES = {{}};"), &dst);
900+
for aliases in &all_aliases {
901+
try_err!(writeln!(&mut w, "{}", aliases), &dst);
902+
}
903+
}
904+
868905
// Update the search index
869906
let dst = cx.dst.join("search-index.js");
870907
let mut all_indexes = try_err!(collect(&dst, &krate.name, "searchIndex"), &dst);
@@ -1251,13 +1288,13 @@ impl DocFolder for Cache {
12511288
// `public_items` map, so we can skip inserting into the
12521289
// paths map if there was already an entry present and we're
12531290
// not a public item.
1254-
if
1255-
!self.paths.contains_key(&item.def_id) ||
1256-
self.access_levels.is_public(item.def_id)
1291+
if !self.paths.contains_key(&item.def_id) ||
1292+
self.access_levels.is_public(item.def_id)
12571293
{
12581294
self.paths.insert(item.def_id,
12591295
(self.stack.clone(), item.type_()));
12601296
}
1297+
self.add_aliases(&item);
12611298
}
12621299
// Link variants to their parent enum because pages aren't emitted
12631300
// for each variant.
@@ -1268,6 +1305,7 @@ impl DocFolder for Cache {
12681305
}
12691306

12701307
clean::PrimitiveItem(..) if item.visibility.is_some() => {
1308+
self.add_aliases(&item);
12711309
self.paths.insert(item.def_id, (self.stack.clone(),
12721310
item.type_()));
12731311
}
@@ -1372,6 +1410,36 @@ impl<'a> Cache {
13721410
}
13731411
}
13741412
}
1413+
1414+
fn add_aliases(&mut self, item: &clean::Item) {
1415+
if item.def_id.index == CRATE_DEF_INDEX {
1416+
return
1417+
}
1418+
if let Some(ref item_name) = item.name {
1419+
let path = self.paths.get(&item.def_id)
1420+
.map(|p| p.0.join("::").to_string())
1421+
.unwrap_or("std".to_owned());
1422+
for alias in item.attrs.lists("doc")
1423+
.filter(|a| a.check_name("alias"))
1424+
.filter_map(|a| a.value_str()
1425+
.map(|s| s.to_string().replace("\"", "")))
1426+
.filter(|v| !v.is_empty())
1427+
.collect::<FxHashSet<_>>()
1428+
.into_iter() {
1429+
self.aliases.entry(alias)
1430+
.or_insert(Vec::with_capacity(1))
1431+
.push(IndexItem {
1432+
ty: item.type_(),
1433+
name: item_name.to_string(),
1434+
path: path.clone(),
1435+
desc: String::new(),
1436+
parent: None,
1437+
parent_idx: None,
1438+
search_type: get_index_search_type(&item),
1439+
});
1440+
}
1441+
}
1442+
}
13751443
}
13761444

13771445
#[derive(Debug, Eq, PartialEq, Hash)]

src/libstd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@
316316
#![feature(doc_spotlight)]
317317
#![cfg_attr(test, feature(update_panic_count))]
318318
#![cfg_attr(windows, feature(used))]
319+
#![feature(doc_alias)]
319320

320321
#![default_lib_allocator]
321322

src/libstd/primitive_docs.rs

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

1111
#[doc(primitive = "bool")]
12+
#[doc(alias = "true")]
13+
#[doc(alias = "false")]
1214
//
1315
/// The boolean type.
1416
///
@@ -68,6 +70,7 @@
6870
mod prim_bool { }
6971

7072
#[doc(primitive = "never")]
73+
#[doc(alias = "!")]
7174
//
7275
/// The `!` type, also called "never".
7376
///

src/libsyntax/feature_gate.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,9 @@ declare_features! (
460460
(active, proc_macro_mod, "1.27.0", None, None),
461461
(active, proc_macro_expr, "1.27.0", None, None),
462462
(active, proc_macro_non_items, "1.27.0", None, None),
463+
464+
// #[doc(alias = "...")]
465+
(active, doc_alias, "1.27.0", None, None),
463466
);
464467

465468
declare_features! (
@@ -1455,6 +1458,10 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
14551458
gate_feature_post!(&self, doc_spotlight, attr.span,
14561459
"#[doc(spotlight)] is experimental"
14571460
);
1461+
} else if content.iter().any(|c| c.check_name("alias")) {
1462+
gate_feature_post!(&self, doc_alias, attr.span,
1463+
"#[doc(alias = \"...\")] is experimental"
1464+
);
14581465
}
14591466
}
14601467
}

0 commit comments

Comments
 (0)