From 654cb84852a0485146f95bbebe14ac75b7ee111a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 7 Apr 2018 14:04:03 +0200 Subject: [PATCH 01/10] Add specific never search --- src/librustdoc/html/static/main.js | 7 ++++++- src/test/rustdoc-js/never.js | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/test/rustdoc-js/never.js diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 2546a9410a9f6..fd8bd52cfab4c 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1062,6 +1062,11 @@ type = matches[1].replace(/^const$/, 'constant'); query = query.substring(matches[0].length); } + // In case we just get a '!' as input, we can assume that the user is looking for the + // `Never` primitive type. + if (raw === '!') { + query = 'Never'; + } return { raw: raw, @@ -1369,7 +1374,7 @@ function search(e) { var params = getQueryStringParams(); - var query = getQuery(document.getElementsByClassName('search-input')[0].value); + var query = getQuery(document.getElementsByClassName('search-input')[0].value.trim()); if (e) { e.preventDefault(); diff --git a/src/test/rustdoc-js/never.js b/src/test/rustdoc-js/never.js new file mode 100644 index 0000000000000..d9e1ca5f760d5 --- /dev/null +++ b/src/test/rustdoc-js/never.js @@ -0,0 +1,17 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +const QUERY = '!'; + +const EXPECTED = { + 'others': [ + { 'path': 'std', 'name': 'never' }, + ], +}; From 57bcabc1082c948e2cfda3f005c41d8236ead7a4 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 19 Apr 2018 17:46:13 +0200 Subject: [PATCH 02/10] Generate alias file --- src/librustdoc/html/item_type.rs | 2 +- src/librustdoc/html/layout.rs | 1 + src/librustdoc/html/render.rs | 80 +++++++++++++++++++++++++++++--- src/libstd/lib.rs | 1 + src/libstd/primitive_docs.rs | 3 ++ src/libsyntax/feature_gate.rs | 7 +++ 6 files changed, 87 insertions(+), 7 deletions(-) diff --git a/src/librustdoc/html/item_type.rs b/src/librustdoc/html/item_type.rs index e9c6488c49c6c..537828de2c7f3 100644 --- a/src/librustdoc/html/item_type.rs +++ b/src/librustdoc/html/item_type.rs @@ -19,7 +19,7 @@ use clean; /// discriminants. JavaScript then is used to decode them into the original value. /// Consequently, every change to this type should be synchronized to /// the `itemTypes` mapping table in `static/main.js`. -#[derive(Copy, PartialEq, Clone)] +#[derive(Copy, PartialEq, Clone, Debug)] pub enum ItemType { Module = 0, ExternCrate = 1, diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 583c9f2b67144..1880baeddf4da 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -145,6 +145,7 @@ pub fn render( \ \ \ + \ \ ", css_extension = if css_file_extension { diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 651319743aadd..8fe8fe671dd75 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -329,6 +329,10 @@ pub struct Cache { // yet when its implementation methods are being indexed. Caches such methods // and their parent id here and indexes them at the end of crate parsing. orphan_impl_items: Vec<(DefId, clean::Item)>, + + /// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias, + /// we need the alias element to have an array of items. + aliases: FxHashMap>, } /// Temporary storage for data obtained during `RustdocVisitor::clean()`. @@ -369,6 +373,7 @@ struct Sidebar<'a> { cx: &'a Context, item: &'a clean::Item, } /// Struct representing one entry in the JS search index. These are all emitted /// by hand to a large JS file at the end of cache-creation. +#[derive(Debug)] struct IndexItem { ty: ItemType, name: String, @@ -396,6 +401,7 @@ impl ToJson for IndexItem { } /// A type used for the search index. +#[derive(Debug)] struct Type { name: Option, generics: Option>, @@ -418,9 +424,10 @@ impl ToJson for Type { } /// Full type of functions/methods in the search index. +#[derive(Debug)] struct IndexItemFunctionType { inputs: Vec, - output: Option + output: Option, } impl ToJson for IndexItemFunctionType { @@ -609,6 +616,7 @@ pub fn run(mut krate: clean::Crate, owned_box_did, masked_crates: mem::replace(&mut krate.masked_crates, FxHashSet()), typarams: external_typarams, + aliases: FxHashMap(), }; // Cache where all our extern crates are located @@ -847,8 +855,7 @@ themePicker.onclick = function() {{ write(cx.dst.join("COPYRIGHT.txt"), include_bytes!("static/COPYRIGHT.txt"))?; - fn collect(path: &Path, krate: &str, - key: &str) -> io::Result> { + fn collect(path: &Path, krate: &str, key: &str) -> io::Result> { let mut ret = Vec::new(); if path.exists() { for line in BufReader::new(File::open(path)?).lines() { @@ -865,6 +872,36 @@ themePicker.onclick = function() {{ Ok(ret) } + fn show_item(item: &IndexItem, krate: &str) -> String { + format!("{{'crate':'{}','ty':'{}','name':'{}','path':'{}','parent':{}}}", + krate, item.ty, item.name, item.path, + if let Some(p) = item.parent_idx { p.to_string() } else { "null".to_owned() }) + } + + let dst = cx.dst.join("aliases.js"); + { + let mut all_aliases = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst); + let mut w = try_err!(File::create(&dst), &dst); + let mut output = String::with_capacity(100); + for (alias, items) in &cache.aliases { + if items.is_empty() { + continue + } + output.push_str(&format!("\"{}\":[{}],", + alias, + items.iter() + .map(|v| show_item(v, &krate.name)) + .collect::>() + .join(","))); + } + all_aliases.push(format!("ALIASES['{}'] = {{{}}};", krate.name, output)); + all_aliases.sort(); + try_err!(writeln!(&mut w, "var ALIASES = {{}};"), &dst); + for aliases in &all_aliases { + try_err!(writeln!(&mut w, "{}", aliases), &dst); + } + } + // Update the search index let dst = cx.dst.join("search-index.js"); let mut all_indexes = try_err!(collect(&dst, &krate.name, "searchIndex"), &dst); @@ -1251,13 +1288,13 @@ impl DocFolder for Cache { // `public_items` map, so we can skip inserting into the // paths map if there was already an entry present and we're // not a public item. - if - !self.paths.contains_key(&item.def_id) || - self.access_levels.is_public(item.def_id) + if !self.paths.contains_key(&item.def_id) || + self.access_levels.is_public(item.def_id) { self.paths.insert(item.def_id, (self.stack.clone(), item.type_())); } + self.add_aliases(&item); } // Link variants to their parent enum because pages aren't emitted // for each variant. @@ -1268,6 +1305,7 @@ impl DocFolder for Cache { } clean::PrimitiveItem(..) if item.visibility.is_some() => { + self.add_aliases(&item); self.paths.insert(item.def_id, (self.stack.clone(), item.type_())); } @@ -1372,6 +1410,36 @@ impl<'a> Cache { } } } + + fn add_aliases(&mut self, item: &clean::Item) { + if item.def_id.index == CRATE_DEF_INDEX { + return + } + if let Some(ref item_name) = item.name { + let path = self.paths.get(&item.def_id) + .map(|p| p.0.join("::").to_string()) + .unwrap_or("std".to_owned()); + for alias in item.attrs.lists("doc") + .filter(|a| a.check_name("alias")) + .filter_map(|a| a.value_str() + .map(|s| s.to_string().replace("\"", ""))) + .filter(|v| !v.is_empty()) + .collect::>() + .into_iter() { + self.aliases.entry(alias) + .or_insert(Vec::with_capacity(1)) + .push(IndexItem { + ty: item.type_(), + name: item_name.to_string(), + path: path.clone(), + desc: String::new(), + parent: None, + parent_idx: None, + search_type: get_index_search_type(&item), + }); + } + } + } } #[derive(Debug, Eq, PartialEq, Hash)] diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 7d8966953114e..579fd0eaf3faf 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -316,6 +316,7 @@ #![feature(doc_spotlight)] #![cfg_attr(test, feature(update_panic_count))] #![cfg_attr(windows, feature(used))] +#![feature(doc_alias)] #![default_lib_allocator] diff --git a/src/libstd/primitive_docs.rs b/src/libstd/primitive_docs.rs index ce4bbfffc2e47..e2fcfb7c4b1c0 100644 --- a/src/libstd/primitive_docs.rs +++ b/src/libstd/primitive_docs.rs @@ -9,6 +9,8 @@ // except according to those terms. #[doc(primitive = "bool")] +#[doc(alias = "true")] +#[doc(alias = "false")] // /// The boolean type. /// @@ -68,6 +70,7 @@ mod prim_bool { } #[doc(primitive = "never")] +#[doc(alias = "!")] // /// The `!` type, also called "never". /// diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 6426c9a92f231..3f0a402c21331 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -460,6 +460,9 @@ declare_features! ( (active, proc_macro_mod, "1.27.0", None, None), (active, proc_macro_expr, "1.27.0", None, None), (active, proc_macro_non_items, "1.27.0", None, None), + + // #[doc(alias = "...")] + (active, doc_alias, "1.27.0", None, None), ); declare_features! ( @@ -1455,6 +1458,10 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { gate_feature_post!(&self, doc_spotlight, attr.span, "#[doc(spotlight)] is experimental" ); + } else if content.iter().any(|c| c.check_name("alias")) { + gate_feature_post!(&self, doc_alias, attr.span, + "#[doc(alias = \"...\")] is experimental" + ); } } } From 03b0856849e625cde4418b403fb4bee7b7875c46 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 19 Apr 2018 18:23:12 +0200 Subject: [PATCH 03/10] Add aliases in the search as well --- src/librustdoc/html/render.rs | 10 +++++++--- src/librustdoc/html/static/main.js | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 8fe8fe671dd75..09a11073a3950 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -873,9 +873,13 @@ themePicker.onclick = function() {{ } fn show_item(item: &IndexItem, krate: &str) -> String { - format!("{{'crate':'{}','ty':'{}','name':'{}','path':'{}','parent':{}}}", - krate, item.ty, item.name, item.path, - if let Some(p) = item.parent_idx { p.to_string() } else { "null".to_owned() }) + format!("{{'crate':'{}','ty':{},'name':'{}','path':'{}'{}}}", + krate, item.ty as usize, item.name, item.path, + if let Some(p) = item.parent_idx { + format!(",'parent':{}", p) + } else { + String::new() + }) } let dst = cx.dst.join("aliases.js"); diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index fd8bd52cfab4c..c942eff6b0243 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1012,11 +1012,21 @@ } } - return { + var ret = { 'in_args': sortResults(results_in_args, true), 'returned': sortResults(results_returned, true), 'others': sortResults(results), }; + if (ALIASES[window.currentCrate][query.raw]) { + var aliases = ALIASES[window.currentCrate][query.raw]; + for (var i = 0; i < aliases.length; ++i) { + ret['others'].unshift(aliases[i]); + if (ret['others'].length > MAX_RESULTS) { + ret['others'].pop(); + } + } + } + return ret; } /** @@ -1202,11 +1212,11 @@ array.forEach(function(item) { var name, type, href, displayPath; - if (shown.indexOf(item) !== -1) { + if (shown.indexOf(item.ty) !== -1) { return; } - shown.push(item); + shown.push(item.ty); name = item.name; type = itemTypes[item.ty]; From 84b91d6f5c7a49159f46f9bec37b57bd7e0a61be Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 19 Apr 2018 19:56:10 +0200 Subject: [PATCH 04/10] add more aliases --- src/libcore/lib.rs | 1 + src/libcore/macros.rs | 1 + src/libcore/ops/arith.rs | 16 ++++++++++++++++ src/libcore/ops/index.rs | 6 ++++++ src/libcore/ops/try.rs | 1 + src/libstd/primitive_docs.rs | 7 +++++++ 6 files changed, 32 insertions(+) diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index ea7a46f44ae0f..2f3a7ebbe7726 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -98,6 +98,7 @@ #![feature(unboxed_closures)] #![feature(untagged_unions)] #![feature(unwind_attributes)] +#![feature(doc_alias)] #![cfg_attr(not(stage0), feature(mmx_target_feature))] #![cfg_attr(not(stage0), feature(tbm_target_feature))] diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index 90a9cb3379b82..346d404fa8c59 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -295,6 +295,7 @@ macro_rules! debug_assert_ne { /// ``` #[macro_export] #[stable(feature = "rust1", since = "1.0.0")] +#[doc(alias = "?")] macro_rules! try { ($expr:expr) => (match $expr { $crate::result::Result::Ok(val) => val, diff --git a/src/libcore/ops/arith.rs b/src/libcore/ops/arith.rs index 88db019b02f07..a1f6030428f1e 100644 --- a/src/libcore/ops/arith.rs +++ b/src/libcore/ops/arith.rs @@ -87,6 +87,7 @@ message="cannot add `{RHS}` to `{Self}`", label="no implementation for `{Self} + {RHS}`", )] +#[doc(alias = "+")] pub trait Add { /// The resulting type after applying the `+` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -183,6 +184,7 @@ add_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 } #[stable(feature = "rust1", since = "1.0.0")] #[rustc_on_unimplemented(message="cannot subtract `{RHS}` from `{Self}`", label="no implementation for `{Self} - {RHS}`")] +#[doc(alias = "-")] pub trait Sub { /// The resulting type after applying the `-` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -301,6 +303,7 @@ sub_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 } #[stable(feature = "rust1", since = "1.0.0")] #[rustc_on_unimplemented(message="cannot multiply `{RHS}` to `{Self}`", label="no implementation for `{Self} * {RHS}`")] +#[doc(alias = "*")] pub trait Mul { /// The resulting type after applying the `*` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -423,6 +426,7 @@ mul_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 } #[stable(feature = "rust1", since = "1.0.0")] #[rustc_on_unimplemented(message="cannot divide `{Self}` by `{RHS}`", label="no implementation for `{Self} / {RHS}`")] +#[doc(alias = "/")] pub trait Div { /// The resulting type after applying the `/` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -506,6 +510,7 @@ div_impl_float! { f32 f64 } #[stable(feature = "rust1", since = "1.0.0")] #[rustc_on_unimplemented(message="cannot mod `{Self}` by `{RHS}`", label="no implementation for `{Self} % {RHS}`")] +#[doc(alias = "%")] pub trait Rem { /// The resulting type after applying the `%` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -589,6 +594,7 @@ rem_impl_float! { f32 f64 } /// ``` #[lang = "neg"] #[stable(feature = "rust1", since = "1.0.0")] +#[doc(alias = "-")] pub trait Neg { /// The resulting type after applying the `-` operator. #[stable(feature = "rust1", since = "1.0.0")] @@ -664,6 +670,8 @@ neg_impl_numeric! { isize i8 i16 i32 i64 i128 f32 f64 } #[stable(feature = "op_assign_traits", since = "1.8.0")] #[rustc_on_unimplemented(message="cannot add-assign `{Rhs}` to `{Self}`", label="no implementation for `{Self} += {Rhs}`")] +#[doc(alias = "+")] +#[doc(alias = "+=")] pub trait AddAssign { /// Performs the `+=` operation. #[stable(feature = "op_assign_traits", since = "1.8.0")] @@ -718,6 +726,8 @@ add_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 } #[stable(feature = "op_assign_traits", since = "1.8.0")] #[rustc_on_unimplemented(message="cannot subtract-assign `{Rhs}` from `{Self}`", label="no implementation for `{Self} -= {Rhs}`")] +#[doc(alias = "-")] +#[doc(alias = "-=")] pub trait SubAssign { /// Performs the `-=` operation. #[stable(feature = "op_assign_traits", since = "1.8.0")] @@ -763,6 +773,8 @@ sub_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 } #[stable(feature = "op_assign_traits", since = "1.8.0")] #[rustc_on_unimplemented(message="cannot multiply-assign `{Rhs}` to `{Self}`", label="no implementation for `{Self} *= {Rhs}`")] +#[doc(alias = "*")] +#[doc(alias = "*=")] pub trait MulAssign { /// Performs the `*=` operation. #[stable(feature = "op_assign_traits", since = "1.8.0")] @@ -808,6 +820,8 @@ mul_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 } #[stable(feature = "op_assign_traits", since = "1.8.0")] #[rustc_on_unimplemented(message="cannot divide-assign `{Self}` by `{Rhs}`", label="no implementation for `{Self} /= {Rhs}`")] +#[doc(alias = "/")] +#[doc(alias = "/=")] pub trait DivAssign { /// Performs the `/=` operation. #[stable(feature = "op_assign_traits", since = "1.8.0")] @@ -856,6 +870,8 @@ div_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 } #[stable(feature = "op_assign_traits", since = "1.8.0")] #[rustc_on_unimplemented(message="cannot mod-assign `{Self}` by `{Rhs}``", label="no implementation for `{Self} %= {Rhs}`")] +#[doc(alias = "%")] +#[doc(alias = "%=")] pub trait RemAssign { /// Performs the `%=` operation. #[stable(feature = "op_assign_traits", since = "1.8.0")] diff --git a/src/libcore/ops/index.rs b/src/libcore/ops/index.rs index d65c0aba50481..0a0e92a918006 100644 --- a/src/libcore/ops/index.rs +++ b/src/libcore/ops/index.rs @@ -62,6 +62,9 @@ #[lang = "index"] #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"] #[stable(feature = "rust1", since = "1.0.0")] +#[doc(alias = "]")] +#[doc(alias = "[")] +#[doc(alias = "[]")] pub trait Index { /// The returned type after indexing. #[stable(feature = "rust1", since = "1.0.0")] @@ -146,6 +149,9 @@ pub trait Index { #[lang = "index_mut"] #[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"] #[stable(feature = "rust1", since = "1.0.0")] +#[doc(alias = "[")] +#[doc(alias = "]")] +#[doc(alias = "[]")] pub trait IndexMut: Index { /// Performs the mutable indexing (`container[index]`) operation. #[stable(feature = "rust1", since = "1.0.0")] diff --git a/src/libcore/ops/try.rs b/src/libcore/ops/try.rs index ef6a8fb6a61c1..4f2d30aa6a8a7 100644 --- a/src/libcore/ops/try.rs +++ b/src/libcore/ops/try.rs @@ -28,6 +28,7 @@ that implement `{Try}`", label="the `?` operator cannot be applied to type `{Self}`") )] +#[doc(alias = "?")] pub trait Try { /// The type of this value when viewed as successful. #[unstable(feature = "try_trait", issue = "42327")] diff --git a/src/libstd/primitive_docs.rs b/src/libstd/primitive_docs.rs index e2fcfb7c4b1c0..8248ee71474c2 100644 --- a/src/libstd/primitive_docs.rs +++ b/src/libstd/primitive_docs.rs @@ -502,6 +502,9 @@ mod prim_pointer { } mod prim_array { } #[doc(primitive = "slice")] +#[doc(alias = "[")] +#[doc(alias = "]")] +#[doc(alias = "[]")] // /// A dynamically-sized view into a contiguous sequence, `[T]`. /// @@ -600,6 +603,9 @@ mod prim_slice { } mod prim_str { } #[doc(primitive = "tuple")] +#[doc(alias = "(")] +#[doc(alias = ")")] +#[doc(alias = "()")] // /// A finite heterogeneous sequence, `(T, U, ..)`. /// @@ -822,6 +828,7 @@ mod prim_isize { } mod prim_usize { } #[doc(primitive = "reference")] +#[doc(alias = "&")] // /// References, both shared and mutable. /// From ca3213c3380a0c1cd8e342057a7218695ebfc3a7 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 19 Apr 2018 19:59:45 +0200 Subject: [PATCH 05/10] fix invalid items removal --- src/librustdoc/html/static/main.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index c942eff6b0243..27d10bce3a0e5 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1212,11 +1212,13 @@ array.forEach(function(item) { var name, type, href, displayPath; - if (shown.indexOf(item.ty) !== -1) { + var id_ty = item.ty + item.path + item.name; + if (shown.indexOf(id_ty) !== -1) { return; } - shown.push(item.ty); + console.log(item); + shown.push(id_ty); name = item.name; type = itemTypes[item.ty]; From 31e7cc4ba44f16d1ed3b2c5c20f32fe5928e0615 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 19 Apr 2018 20:14:24 +0200 Subject: [PATCH 06/10] update tester --- src/tools/rustdoc-js/tester.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/tools/rustdoc-js/tester.js b/src/tools/rustdoc-js/tester.js index 1e6c4336a9e99..6992f2ba12337 100644 --- a/src/tools/rustdoc-js/tester.js +++ b/src/tools/rustdoc-js/tester.js @@ -144,6 +144,7 @@ function main(argv) { var toolchain = argv[2]; var mainJs = readFile("build/" + toolchain + "/doc/main.js"); + var ALIASES = readFile("build/" + toolchain + "/doc/aliases.js"); var searchIndex = readFile("build/" + toolchain + "/doc/search-index.js").split("\n"); if (searchIndex[searchIndex.length - 1].length === 0) { searchIndex.pop(); @@ -161,6 +162,7 @@ function main(argv) { "execSearch"]; finalJS += 'window = { "currentCrate": "std" };\n'; + finalJS += ALIASES; finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs); finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs); finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs); From d5eade2b941e349be598286b6ca6954d18ac4323 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 19 Apr 2018 20:14:47 +0200 Subject: [PATCH 07/10] Add alias tests --- src/test/rustdoc-js/alias-1.js | 17 +++++++++++++++++ src/test/rustdoc-js/alias-2.js | 18 ++++++++++++++++++ src/test/rustdoc-js/alias-3.js | 17 +++++++++++++++++ src/test/rustdoc-js/alias.js | 19 +++++++++++++++++++ src/test/ui/feature-gate-doc_alias.rs | 14 ++++++++++++++ src/test/ui/feature-gate-doc_alias.stderr | 11 +++++++++++ 6 files changed, 96 insertions(+) create mode 100644 src/test/rustdoc-js/alias-1.js create mode 100644 src/test/rustdoc-js/alias-2.js create mode 100644 src/test/rustdoc-js/alias-3.js create mode 100644 src/test/rustdoc-js/alias.js create mode 100644 src/test/ui/feature-gate-doc_alias.rs create mode 100644 src/test/ui/feature-gate-doc_alias.stderr diff --git a/src/test/rustdoc-js/alias-1.js b/src/test/rustdoc-js/alias-1.js new file mode 100644 index 0000000000000..496bd559b877f --- /dev/null +++ b/src/test/rustdoc-js/alias-1.js @@ -0,0 +1,17 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +const QUERY = '&'; + +const EXPECTED = { + 'others': [ + { 'path': 'std', 'name': 'reference' }, + ], +}; diff --git a/src/test/rustdoc-js/alias-2.js b/src/test/rustdoc-js/alias-2.js new file mode 100644 index 0000000000000..f31786df67cc3 --- /dev/null +++ b/src/test/rustdoc-js/alias-2.js @@ -0,0 +1,18 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +const QUERY = '+'; + +const EXPECTED = { + 'others': [ + { 'path': 'std::ops::AddAssign', 'name': 'AddAssign' }, + { 'path': 'std::ops::Add', 'name': 'Add' }, + ], +}; diff --git a/src/test/rustdoc-js/alias-3.js b/src/test/rustdoc-js/alias-3.js new file mode 100644 index 0000000000000..d9e1ca5f760d5 --- /dev/null +++ b/src/test/rustdoc-js/alias-3.js @@ -0,0 +1,17 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +const QUERY = '!'; + +const EXPECTED = { + 'others': [ + { 'path': 'std', 'name': 'never' }, + ], +}; diff --git a/src/test/rustdoc-js/alias.js b/src/test/rustdoc-js/alias.js new file mode 100644 index 0000000000000..a0500f24c17ee --- /dev/null +++ b/src/test/rustdoc-js/alias.js @@ -0,0 +1,19 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +const QUERY = '['; + +const EXPECTED = { + 'others': [ + { 'path': 'std', 'name': 'slice' }, + { 'path': 'std::ops::IndexMut', 'name': 'IndexMut' }, + { 'path': 'std::ops::Index', 'name': 'Index' }, + ], +}; diff --git a/src/test/ui/feature-gate-doc_alias.rs b/src/test/ui/feature-gate-doc_alias.rs new file mode 100644 index 0000000000000..1503dfe81fb7d --- /dev/null +++ b/src/test/ui/feature-gate-doc_alias.rs @@ -0,0 +1,14 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[doc(alias = "foo")] //~ ERROR: #[doc(alias = "...")] is experimental +pub struct Foo; + +fn main() {} diff --git a/src/test/ui/feature-gate-doc_alias.stderr b/src/test/ui/feature-gate-doc_alias.stderr new file mode 100644 index 0000000000000..693dc0a4000d5 --- /dev/null +++ b/src/test/ui/feature-gate-doc_alias.stderr @@ -0,0 +1,11 @@ +error[E0658]: #[doc(alias = "...")] is experimental + --> $DIR/feature-gate-doc_alias.rs:11:1 + | +LL | #[doc(alias = "foo")] //~ ERROR: #[doc(alias = "...")] is experimental + | ^^^^^^^^^^^^^^^^^^^^^ + | + = help: add #![feature(doc_alias)] to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`. From d0e2d26ceb0055dbc0782c611d36f3efdf241b04 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 19 Apr 2018 23:50:17 +0200 Subject: [PATCH 08/10] remove unused condition --- src/librustdoc/html/static/main.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 27d10bce3a0e5..962ff14d09aef 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1072,11 +1072,6 @@ type = matches[1].replace(/^const$/, 'constant'); query = query.substring(matches[0].length); } - // In case we just get a '!' as input, we can assume that the user is looking for the - // `Never` primitive type. - if (raw === '!') { - query = 'Never'; - } return { raw: raw, From 48ab422be4cbc3ab4eb5d3456c2b92ccfe9d7f8d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 21 Apr 2018 22:33:11 +0200 Subject: [PATCH 09/10] Add tracking issue number for doc alias feature --- src/libsyntax/feature_gate.rs | 2 +- src/test/ui/feature-gate-doc_alias.stderr | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 3f0a402c21331..5f051e2f19310 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -462,7 +462,7 @@ declare_features! ( (active, proc_macro_non_items, "1.27.0", None, None), // #[doc(alias = "...")] - (active, doc_alias, "1.27.0", None, None), + (active, doc_alias, "1.27.0", Some(50146), None), ); declare_features! ( diff --git a/src/test/ui/feature-gate-doc_alias.stderr b/src/test/ui/feature-gate-doc_alias.stderr index 693dc0a4000d5..a987e03c0aaf1 100644 --- a/src/test/ui/feature-gate-doc_alias.stderr +++ b/src/test/ui/feature-gate-doc_alias.stderr @@ -1,4 +1,4 @@ -error[E0658]: #[doc(alias = "...")] is experimental +error[E0658]: #[doc(alias = "...")] is experimental (see issue #50146) --> $DIR/feature-gate-doc_alias.rs:11:1 | LL | #[doc(alias = "foo")] //~ ERROR: #[doc(alias = "...")] is experimental From 1ed3e77b8a254fd9cbf8f922d1f910d375a9d1e4 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 21 Apr 2018 22:41:08 +0200 Subject: [PATCH 10/10] Add doc about doc alias feature --- src/doc/rustdoc/src/unstable-features.md | 16 +++++++++++++ .../src/language-features/doc-alias.md | 23 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/doc/unstable-book/src/language-features/doc-alias.md diff --git a/src/doc/rustdoc/src/unstable-features.md b/src/doc/rustdoc/src/unstable-features.md index bf97fb4686180..7f110d6a3d22c 100644 --- a/src/doc/rustdoc/src/unstable-features.md +++ b/src/doc/rustdoc/src/unstable-features.md @@ -373,3 +373,19 @@ This is an internal flag intended for the standard library and compiler that app `#[unstable]` attribute to any dependent crate that doesn't have another stability attribute. This allows `rustdoc` to be able to generate documentation for the compiler crates and the standard library, as an equivalent command-line argument is provided to `rustc` when building those crates. + +### `doc_alias` feature + +This feature allows you to add alias(es) to an item when using the `rustdoc` search through the +`doc(alias)` attribute. Example: + +```rust,no_run +#![feature(doc_alias)] + +#[doc(alias = "x")] +#[doc(alias = "big")] +pub struct BigX; +``` + +Then, when looking for it through the `rustdoc` search, if you enter "x" or +"big", search will show the `BigX` struct first. diff --git a/src/doc/unstable-book/src/language-features/doc-alias.md b/src/doc/unstable-book/src/language-features/doc-alias.md new file mode 100644 index 0000000000000..647ac0cf663fd --- /dev/null +++ b/src/doc/unstable-book/src/language-features/doc-alias.md @@ -0,0 +1,23 @@ +# `doc_alias` + +The tracking issue for this feature is: [#50146] + +[#50146]: https://github.com/rust-lang/rust/issues/50146 + +------------------------ + +You can add alias(es) to an item when using the `rustdoc` search through the +`doc(alias)` attribute. Example: + +```rust,no_run +#![feature(doc_alias)] + +#[doc(alias = "x")] +#[doc(alias = "big")] +pub struct BigX; +``` + +Then, when looking for it through the `rustdoc` search, if you enter "x" or +"big", search will show the `BigX` struct first. + +Note that this feature is currently hidden behind the `feature(doc_alias)` gate.