Skip to content

Commit 16ffd83

Browse files
authored
Rollup merge of rust-lang#97592 - notriddle:notriddle/impl-trait, r=GuillaumeGomez
rustdoc: also index impl trait and raw pointers Revives rust-lang#92339
2 parents 989ec15 + 14d8baf commit 16ffd83

File tree

6 files changed

+182
-8
lines changed

6 files changed

+182
-8
lines changed

src/librustdoc/clean/types.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1667,6 +1667,10 @@ impl Type {
16671667
matches!(self, Type::Generic(_))
16681668
}
16691669

1670+
pub(crate) fn is_impl_trait(&self) -> bool {
1671+
matches!(self, Type::ImplTrait(_))
1672+
}
1673+
16701674
pub(crate) fn is_primitive(&self) -> bool {
16711675
self.primitive_type().is_some()
16721676
}

src/librustdoc/html/render/search_index.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,17 @@ fn get_index_type_name(clean_type: &clean::Type) -> Option<Symbol> {
226226
Some(path.segments.last().unwrap().name)
227227
}
228228
// We return an empty name because we don't care about the generic name itself.
229-
clean::Generic(_) => Some(kw::Empty),
229+
clean::Generic(_) | clean::ImplTrait(_) => Some(kw::Empty),
230230
clean::Primitive(ref p) => Some(p.as_sym()),
231-
clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_),
231+
clean::BorrowedRef { ref type_, .. } | clean::RawPointer(_, ref type_) => {
232+
get_index_type_name(type_)
233+
}
232234
clean::BareFunction(_)
233235
| clean::Tuple(_)
234236
| clean::Slice(_)
235237
| clean::Array(_, _)
236-
| clean::RawPointer(_, _)
237238
| clean::QPath { .. }
238-
| clean::Infer
239-
| clean::ImplTrait(_) => None,
239+
| clean::Infer => None,
240240
}
241241
}
242242

@@ -264,10 +264,12 @@ fn add_generics_and_bounds_as_types<'tcx, 'a>(
264264
mut generics: Vec<TypeWithKind>,
265265
cache: &Cache,
266266
) {
267-
let is_full_generic = ty.is_full_generic();
267+
// generics and impl trait are both identified by their generics,
268+
// rather than a type name itself
269+
let anonymous = ty.is_full_generic() || ty.is_impl_trait();
268270
let generics_empty = generics.is_empty();
269271

270-
if is_full_generic {
272+
if anonymous {
271273
if generics_empty {
272274
// This is a type parameter with no trait bounds (for example: `T` in
273275
// `fn f<T>(p: T)`, so not useful for the rustdoc search because we would end up
@@ -318,7 +320,7 @@ fn add_generics_and_bounds_as_types<'tcx, 'a>(
318320
if index_ty.name.as_ref().map(|s| s.is_empty() && generics_empty).unwrap_or(true) {
319321
return;
320322
}
321-
if is_full_generic {
323+
if anonymous {
322324
// We remove the name of the full generic because we have no use for it.
323325
index_ty.name = Some(String::new());
324326
res.push(TypeWithKind::from((index_ty, ItemType::Generic)));
@@ -398,6 +400,23 @@ fn add_generics_and_bounds_as_types<'tcx, 'a>(
398400
}
399401
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
400402
}
403+
} else if let Type::ImplTrait(ref bounds) = *arg {
404+
let mut ty_generics = Vec::new();
405+
for bound in bounds {
406+
if let Some(path) = bound.get_trait_path() {
407+
let ty = Type::Path { path };
408+
add_generics_and_bounds_as_types(
409+
self_,
410+
generics,
411+
&ty,
412+
tcx,
413+
recurse + 1,
414+
&mut ty_generics,
415+
cache,
416+
);
417+
}
418+
}
419+
insert_ty(res, tcx, arg.clone(), ty_generics, cache);
401420
} else {
402421
// This is not a type parameter. So for example if we have `T, U: Option<T>`, and we're
403422
// looking at `Option`, we enter this "else" condition, otherwise if it's `T`, we don't.

src/test/rustdoc-js/impl-trait.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// ignore-order
2+
3+
const QUERY = [
4+
'Aaaaaaa -> i32',
5+
'Aaaaaaa -> Aaaaaaa',
6+
'Aaaaaaa -> usize',
7+
'-> Aaaaaaa',
8+
'Aaaaaaa',
9+
];
10+
11+
const EXPECTED = [
12+
{
13+
// Aaaaaaa -> i32
14+
'others': [
15+
{ 'path': 'impl_trait::Ccccccc', 'name': 'eeeeeee' },
16+
],
17+
},
18+
{
19+
// Aaaaaaa -> Aaaaaaa
20+
'others': [
21+
{ 'path': 'impl_trait::Ccccccc', 'name': 'fffffff' },
22+
],
23+
},
24+
{
25+
// Aaaaaaa -> usize
26+
'others': [],
27+
},
28+
{
29+
// -> Aaaaaaa
30+
'others': [
31+
{ 'path': 'impl_trait::Ccccccc', 'name': 'fffffff' },
32+
{ 'path': 'impl_trait::Ccccccc', 'name': 'ddddddd' },
33+
{ 'path': 'impl_trait', 'name': 'bbbbbbb' },
34+
],
35+
},
36+
{
37+
// Aaaaaaa
38+
'others': [
39+
{ 'path': 'impl_trait', 'name': 'Aaaaaaa' },
40+
],
41+
'in_args': [
42+
{ 'path': 'impl_trait::Ccccccc', 'name': 'fffffff' },
43+
{ 'path': 'impl_trait::Ccccccc', 'name': 'eeeeeee' },
44+
],
45+
'returned': [
46+
{ 'path': 'impl_trait::Ccccccc', 'name': 'fffffff' },
47+
{ 'path': 'impl_trait::Ccccccc', 'name': 'ddddddd' },
48+
{ 'path': 'impl_trait', 'name': 'bbbbbbb' },
49+
],
50+
},
51+
];

src/test/rustdoc-js/impl-trait.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
pub trait Aaaaaaa {}
2+
3+
impl Aaaaaaa for () {}
4+
5+
pub fn bbbbbbb() -> impl Aaaaaaa {
6+
()
7+
}
8+
9+
pub struct Ccccccc {}
10+
11+
impl Ccccccc {
12+
pub fn ddddddd(&self) -> impl Aaaaaaa {
13+
()
14+
}
15+
pub fn eeeeeee(&self, _x: impl Aaaaaaa) -> i32 {
16+
0
17+
}
18+
pub fn fffffff(&self, x: impl Aaaaaaa) -> impl Aaaaaaa {
19+
x
20+
}
21+
}

src/test/rustdoc-js/raw-pointer.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// ignore-order
2+
3+
const QUERY = [
4+
'Aaaaaaa -> i32',
5+
'Aaaaaaa -> Aaaaaaa',
6+
'Aaaaaaa -> usize',
7+
'-> Aaaaaaa',
8+
'Aaaaaaa',
9+
];
10+
11+
const EXPECTED = [
12+
{
13+
// Aaaaaaa -> i32
14+
'others': [
15+
{ 'path': 'raw_pointer::Ccccccc', 'name': 'eeeeeee' },
16+
],
17+
},
18+
{
19+
// Aaaaaaa -> Aaaaaaa
20+
'others': [
21+
{ 'path': 'raw_pointer::Ccccccc', 'name': 'fffffff' },
22+
{ 'path': 'raw_pointer::Ccccccc', 'name': 'ggggggg' },
23+
],
24+
},
25+
{
26+
// Aaaaaaa -> usize
27+
'others': [],
28+
},
29+
{
30+
// -> Aaaaaaa
31+
'others': [
32+
{ 'path': 'raw_pointer::Ccccccc', 'name': 'fffffff' },
33+
{ 'path': 'raw_pointer::Ccccccc', 'name': 'ggggggg' },
34+
{ 'path': 'raw_pointer::Ccccccc', 'name': 'ddddddd' },
35+
{ 'path': 'raw_pointer', 'name': 'bbbbbbb' },
36+
],
37+
},
38+
{
39+
// Aaaaaaa
40+
'others': [
41+
{ 'path': 'raw_pointer', 'name': 'Aaaaaaa' },
42+
],
43+
'in_args': [
44+
{ 'path': 'raw_pointer::Ccccccc', 'name': 'fffffff' },
45+
{ 'path': 'raw_pointer::Ccccccc', 'name': 'ggggggg' },
46+
{ 'path': 'raw_pointer::Ccccccc', 'name': 'eeeeeee' },
47+
],
48+
'returned': [
49+
{ 'path': 'raw_pointer::Ccccccc', 'name': 'fffffff' },
50+
{ 'path': 'raw_pointer::Ccccccc', 'name': 'ggggggg' },
51+
{ 'path': 'raw_pointer::Ccccccc', 'name': 'ddddddd' },
52+
{ 'path': 'raw_pointer', 'name': 'bbbbbbb' },
53+
],
54+
},
55+
];

src/test/rustdoc-js/raw-pointer.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::ptr;
2+
3+
pub struct Aaaaaaa {}
4+
5+
pub fn bbbbbbb() -> *const Aaaaaaa {
6+
ptr::null()
7+
}
8+
9+
pub struct Ccccccc {}
10+
11+
impl Ccccccc {
12+
pub fn ddddddd(&self) -> *const Aaaaaaa {
13+
ptr::null()
14+
}
15+
pub fn eeeeeee(&self, _x: *const Aaaaaaa) -> i32 {
16+
0
17+
}
18+
pub fn fffffff(&self, x: *const Aaaaaaa) -> *const Aaaaaaa {
19+
x
20+
}
21+
pub fn ggggggg(&self, x: *mut Aaaaaaa) -> *mut Aaaaaaa {
22+
x
23+
}
24+
}

0 commit comments

Comments
 (0)