Skip to content

Commit bbdd1cf

Browse files
committed
Auto merge of #49757 - GuillaumeGomez:never-search, r=QuietMisdreavus
Add specific never search Fixes #49529. r? @QuietMisdreavus
2 parents d5616e1 + 1ed3e77 commit bbdd1cf

File tree

22 files changed

+293
-11
lines changed

22 files changed

+293
-11
lines changed

src/doc/rustdoc/src/unstable-features.md

+16
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,19 @@ This is an internal flag intended for the standard library and compiler that app
373373
`#[unstable]` attribute to any dependent crate that doesn't have another stability attribute. This
374374
allows `rustdoc` to be able to generate documentation for the compiler crates and the standard
375375
library, as an equivalent command-line argument is provided to `rustc` when building those crates.
376+
377+
### `doc_alias` feature
378+
379+
This feature allows you to add alias(es) to an item when using the `rustdoc` search through the
380+
`doc(alias)` attribute. Example:
381+
382+
```rust,no_run
383+
#![feature(doc_alias)]
384+
385+
#[doc(alias = "x")]
386+
#[doc(alias = "big")]
387+
pub struct BigX;
388+
```
389+
390+
Then, when looking for it through the `rustdoc` search, if you enter "x" or
391+
"big", search will show the `BigX` struct first.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# `doc_alias`
2+
3+
The tracking issue for this feature is: [#50146]
4+
5+
[#50146]: https://github.com/rust-lang/rust/issues/50146
6+
7+
------------------------
8+
9+
You can add alias(es) to an item when using the `rustdoc` search through the
10+
`doc(alias)` attribute. Example:
11+
12+
```rust,no_run
13+
#![feature(doc_alias)]
14+
15+
#[doc(alias = "x")]
16+
#[doc(alias = "big")]
17+
pub struct BigX;
18+
```
19+
20+
Then, when looking for it through the `rustdoc` search, if you enter "x" or
21+
"big", search will show the `BigX` struct first.
22+
23+
Note that this feature is currently hidden behind the `feature(doc_alias)` gate.

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
#![feature(unboxed_closures)]
103103
#![feature(untagged_unions)]
104104
#![feature(unwind_attributes)]
105+
#![feature(doc_alias)]
105106

106107
#![cfg_attr(not(stage0), feature(mmx_target_feature))]
107108
#![cfg_attr(not(stage0), feature(tbm_target_feature))]

src/libcore/macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ macro_rules! debug_assert_ne {
295295
/// ```
296296
#[macro_export]
297297
#[stable(feature = "rust1", since = "1.0.0")]
298+
#[doc(alias = "?")]
298299
macro_rules! try {
299300
($expr:expr) => (match $expr {
300301
$crate::result::Result::Ok(val) => val,

src/libcore/ops/arith.rs

+16
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
message="cannot add `{RHS}` to `{Self}`",
8888
label="no implementation for `{Self} + {RHS}`",
8989
)]
90+
#[doc(alias = "+")]
9091
pub trait Add<RHS=Self> {
9192
/// The resulting type after applying the `+` operator.
9293
#[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 }
183184
#[stable(feature = "rust1", since = "1.0.0")]
184185
#[rustc_on_unimplemented(message="cannot subtract `{RHS}` from `{Self}`",
185186
label="no implementation for `{Self} - {RHS}`")]
187+
#[doc(alias = "-")]
186188
pub trait Sub<RHS=Self> {
187189
/// The resulting type after applying the `-` operator.
188190
#[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 }
301303
#[stable(feature = "rust1", since = "1.0.0")]
302304
#[rustc_on_unimplemented(message="cannot multiply `{RHS}` to `{Self}`",
303305
label="no implementation for `{Self} * {RHS}`")]
306+
#[doc(alias = "*")]
304307
pub trait Mul<RHS=Self> {
305308
/// The resulting type after applying the `*` operator.
306309
#[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 }
423426
#[stable(feature = "rust1", since = "1.0.0")]
424427
#[rustc_on_unimplemented(message="cannot divide `{Self}` by `{RHS}`",
425428
label="no implementation for `{Self} / {RHS}`")]
429+
#[doc(alias = "/")]
426430
pub trait Div<RHS=Self> {
427431
/// The resulting type after applying the `/` operator.
428432
#[stable(feature = "rust1", since = "1.0.0")]
@@ -506,6 +510,7 @@ div_impl_float! { f32 f64 }
506510
#[stable(feature = "rust1", since = "1.0.0")]
507511
#[rustc_on_unimplemented(message="cannot mod `{Self}` by `{RHS}`",
508512
label="no implementation for `{Self} % {RHS}`")]
513+
#[doc(alias = "%")]
509514
pub trait Rem<RHS=Self> {
510515
/// The resulting type after applying the `%` operator.
511516
#[stable(feature = "rust1", since = "1.0.0")]
@@ -589,6 +594,7 @@ rem_impl_float! { f32 f64 }
589594
/// ```
590595
#[lang = "neg"]
591596
#[stable(feature = "rust1", since = "1.0.0")]
597+
#[doc(alias = "-")]
592598
pub trait Neg {
593599
/// The resulting type after applying the `-` operator.
594600
#[stable(feature = "rust1", since = "1.0.0")]
@@ -664,6 +670,8 @@ neg_impl_numeric! { isize i8 i16 i32 i64 i128 f32 f64 }
664670
#[stable(feature = "op_assign_traits", since = "1.8.0")]
665671
#[rustc_on_unimplemented(message="cannot add-assign `{Rhs}` to `{Self}`",
666672
label="no implementation for `{Self} += {Rhs}`")]
673+
#[doc(alias = "+")]
674+
#[doc(alias = "+=")]
667675
pub trait AddAssign<Rhs=Self> {
668676
/// Performs the `+=` operation.
669677
#[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 }
718726
#[stable(feature = "op_assign_traits", since = "1.8.0")]
719727
#[rustc_on_unimplemented(message="cannot subtract-assign `{Rhs}` from `{Self}`",
720728
label="no implementation for `{Self} -= {Rhs}`")]
729+
#[doc(alias = "-")]
730+
#[doc(alias = "-=")]
721731
pub trait SubAssign<Rhs=Self> {
722732
/// Performs the `-=` operation.
723733
#[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 }
763773
#[stable(feature = "op_assign_traits", since = "1.8.0")]
764774
#[rustc_on_unimplemented(message="cannot multiply-assign `{Rhs}` to `{Self}`",
765775
label="no implementation for `{Self} *= {Rhs}`")]
776+
#[doc(alias = "*")]
777+
#[doc(alias = "*=")]
766778
pub trait MulAssign<Rhs=Self> {
767779
/// Performs the `*=` operation.
768780
#[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 }
808820
#[stable(feature = "op_assign_traits", since = "1.8.0")]
809821
#[rustc_on_unimplemented(message="cannot divide-assign `{Self}` by `{Rhs}`",
810822
label="no implementation for `{Self} /= {Rhs}`")]
823+
#[doc(alias = "/")]
824+
#[doc(alias = "/=")]
811825
pub trait DivAssign<Rhs=Self> {
812826
/// Performs the `/=` operation.
813827
#[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 }
856870
#[stable(feature = "op_assign_traits", since = "1.8.0")]
857871
#[rustc_on_unimplemented(message="cannot mod-assign `{Self}` by `{Rhs}``",
858872
label="no implementation for `{Self} %= {Rhs}`")]
873+
#[doc(alias = "%")]
874+
#[doc(alias = "%=")]
859875
pub trait RemAssign<Rhs=Self> {
860876
/// Performs the `%=` operation.
861877
#[stable(feature = "op_assign_traits", since = "1.8.0")]

src/libcore/ops/index.rs

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@
6262
#[lang = "index"]
6363
#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
6464
#[stable(feature = "rust1", since = "1.0.0")]
65+
#[doc(alias = "]")]
66+
#[doc(alias = "[")]
67+
#[doc(alias = "[]")]
6568
pub trait Index<Idx: ?Sized> {
6669
/// The returned type after indexing.
6770
#[stable(feature = "rust1", since = "1.0.0")]
@@ -146,6 +149,9 @@ pub trait Index<Idx: ?Sized> {
146149
#[lang = "index_mut"]
147150
#[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
148151
#[stable(feature = "rust1", since = "1.0.0")]
152+
#[doc(alias = "[")]
153+
#[doc(alias = "]")]
154+
#[doc(alias = "[]")]
149155
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
150156
/// Performs the mutable indexing (`container[index]`) operation.
151157
#[stable(feature = "rust1", since = "1.0.0")]

src/libcore/ops/try.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
that implement `{Try}`",
2929
label="the `?` operator cannot be applied to type `{Self}`")
3030
)]
31+
#[doc(alias = "?")]
3132
pub trait Try {
3233
/// The type of this value when viewed as successful.
3334
#[unstable(feature = "try_trait", issue = "42327")]

src/librustdoc/html/item_type.rs

+1-1
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

+1
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

+78-6
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,40 @@ themePicker.onclick = function() {{
865872
Ok(ret)
866873
}
867874

875+
fn show_item(item: &IndexItem, krate: &str) -> String {
876+
format!("{{'crate':'{}','ty':{},'name':'{}','path':'{}'{}}}",
877+
krate, item.ty as usize, item.name, item.path,
878+
if let Some(p) = item.parent_idx {
879+
format!(",'parent':{}", p)
880+
} else {
881+
String::new()
882+
})
883+
}
884+
885+
let dst = cx.dst.join("aliases.js");
886+
{
887+
let mut all_aliases = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst);
888+
let mut w = try_err!(File::create(&dst), &dst);
889+
let mut output = String::with_capacity(100);
890+
for (alias, items) in &cache.aliases {
891+
if items.is_empty() {
892+
continue
893+
}
894+
output.push_str(&format!("\"{}\":[{}],",
895+
alias,
896+
items.iter()
897+
.map(|v| show_item(v, &krate.name))
898+
.collect::<Vec<_>>()
899+
.join(",")));
900+
}
901+
all_aliases.push(format!("ALIASES['{}'] = {{{}}};", krate.name, output));
902+
all_aliases.sort();
903+
try_err!(writeln!(&mut w, "var ALIASES = {{}};"), &dst);
904+
for aliases in &all_aliases {
905+
try_err!(writeln!(&mut w, "{}", aliases), &dst);
906+
}
907+
}
908+
868909
// Update the search index
869910
let dst = cx.dst.join("search-index.js");
870911
let mut all_indexes = try_err!(collect(&dst, &krate.name, "searchIndex"), &dst);
@@ -1251,13 +1292,13 @@ impl DocFolder for Cache {
12511292
// `public_items` map, so we can skip inserting into the
12521293
// paths map if there was already an entry present and we're
12531294
// not a public item.
1254-
if
1255-
!self.paths.contains_key(&item.def_id) ||
1256-
self.access_levels.is_public(item.def_id)
1295+
if !self.paths.contains_key(&item.def_id) ||
1296+
self.access_levels.is_public(item.def_id)
12571297
{
12581298
self.paths.insert(item.def_id,
12591299
(self.stack.clone(), item.type_()));
12601300
}
1301+
self.add_aliases(&item);
12611302
}
12621303
// Link variants to their parent enum because pages aren't emitted
12631304
// for each variant.
@@ -1268,6 +1309,7 @@ impl DocFolder for Cache {
12681309
}
12691310

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

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

0 commit comments

Comments
 (0)