Skip to content

Commit 8a1f4f3

Browse files
authored
Rollup merge of rust-lang#110780 - notriddle:notriddle/slice-index, r=GuillaumeGomez
rustdoc-search: add slices and arrays to index This indexes them as primitives with generics, so `slice<u32>` is how you search for `[u32]`, and `array<u32>` for `[u32; 1]`. A future commit will desugar the square bracket syntax to search both arrays and slices at once.
2 parents ac712b4 + c4e00f7 commit 8a1f4f3

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

src/librustdoc/html/render/search_index.rs

+28-2
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,14 @@ fn get_index_type_id(clean_type: &clean::Type) -> Option<RenderTypeId> {
391391
clean::BorrowedRef { ref type_, .. } | clean::RawPointer(_, ref type_) => {
392392
get_index_type_id(type_)
393393
}
394+
// The type parameters are converted to generics in `add_generics_and_bounds_as_types`
395+
clean::Slice(_) => Some(RenderTypeId::Primitive(clean::PrimitiveType::Slice)),
396+
clean::Array(_, _) => Some(RenderTypeId::Primitive(clean::PrimitiveType::Array)),
397+
// Not supported yet
394398
clean::BareFunction(_)
395399
| clean::Generic(_)
396400
| clean::ImplTrait(_)
397401
| clean::Tuple(_)
398-
| clean::Slice(_)
399-
| clean::Array(_, _)
400402
| clean::QPath { .. }
401403
| clean::Infer => None,
402404
}
@@ -563,6 +565,30 @@ fn add_generics_and_bounds_as_types<'tcx, 'a>(
563565
}
564566
}
565567
insert_ty(res, arg.clone(), ty_generics);
568+
} else if let Type::Slice(ref ty) = *arg {
569+
let mut ty_generics = Vec::new();
570+
add_generics_and_bounds_as_types(
571+
self_,
572+
generics,
573+
&ty,
574+
tcx,
575+
recurse + 1,
576+
&mut ty_generics,
577+
cache,
578+
);
579+
insert_ty(res, arg.clone(), ty_generics);
580+
} else if let Type::Array(ref ty, _) = *arg {
581+
let mut ty_generics = Vec::new();
582+
add_generics_and_bounds_as_types(
583+
self_,
584+
generics,
585+
&ty,
586+
tcx,
587+
recurse + 1,
588+
&mut ty_generics,
589+
cache,
590+
);
591+
insert_ty(res, arg.clone(), ty_generics);
566592
} else {
567593
// This is not a type parameter. So for example if we have `T, U: Option<T>`, and we're
568594
// looking at `Option`, we enter this "else" condition, otherwise if it's `T`, we don't.

tests/rustdoc-js/slice-array.js

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// exact-check
2+
3+
const QUERY = [
4+
'R<primitive:slice<P>>',
5+
'primitive:slice<R<P>>',
6+
'R<primitive:slice<Q>>',
7+
'primitive:slice<R<Q>>',
8+
'R<primitive:array<Q>>',
9+
'primitive:array<R<Q>>',
10+
'primitive:array<TraitCat>',
11+
'primitive:array<TraitDog>',
12+
];
13+
14+
const EXPECTED = [
15+
{
16+
// R<primitive:slice<P>>
17+
'returned': [],
18+
'in_args': [
19+
{ 'path': 'slice_array', 'name': 'alpha' },
20+
],
21+
},
22+
{
23+
// primitive:slice<R<P>>
24+
'returned': [
25+
{ 'path': 'slice_array', 'name': 'alef' },
26+
],
27+
'in_args': [],
28+
},
29+
{
30+
// R<primitive:slice<Q>>
31+
'returned': [],
32+
'in_args': [],
33+
},
34+
{
35+
// primitive:slice<R<Q>>
36+
'returned': [],
37+
'in_args': [],
38+
},
39+
{
40+
// R<primitive:array<Q>>
41+
'returned': [
42+
{ 'path': 'slice_array', 'name': 'bet' },
43+
],
44+
'in_args': [],
45+
},
46+
{
47+
// primitive:array<R<Q>>
48+
'returned': [],
49+
'in_args': [
50+
{ 'path': 'slice_array', 'name': 'beta' },
51+
],
52+
},
53+
{
54+
// primitive::array<TraitCat>
55+
'in_args': [
56+
{ 'path': 'slice_array', 'name': 'gamma' },
57+
],
58+
},
59+
{
60+
// primitive::array<TraitDog>
61+
'in_args': [
62+
{ 'path': 'slice_array', 'name': 'gamma' },
63+
],
64+
},
65+
];

tests/rustdoc-js/slice-array.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
pub struct P;
2+
pub struct Q;
3+
pub struct R<T>(T);
4+
5+
// returns test
6+
pub fn alef() -> &'static [R<P>] { loop {} }
7+
pub fn bet() -> R<[Q; 32]> { loop {} }
8+
9+
// in_args test
10+
pub fn alpha(_x: R<&'static [P]>) { loop {} }
11+
pub fn beta(_x: [R<Q>; 32]) { loop {} }
12+
13+
pub trait TraitCat {}
14+
pub trait TraitDog {}
15+
16+
pub fn gamma<T: TraitCat + TraitDog>(t: [T; 32]) {}

0 commit comments

Comments
 (0)