Skip to content

Commit 461cbe4

Browse files
committed
Auto merge of #81557 - GuillaumeGomez:primitive-search, r=ollie27
Fix primitive search in parameters and returned values Part of #60485. Fixes #74780. Replacing #74879. cc `@camelid` `@jyn514` `@CraftSpider` r? `@ollie27`
2 parents d4e3570 + c013f2a commit 461cbe4

File tree

7 files changed

+61
-14
lines changed

7 files changed

+61
-14
lines changed

src/librustdoc/clean/types.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,7 @@ crate enum TypeKind {
13011301
Attr,
13021302
Derive,
13031303
TraitAlias,
1304+
Primitive,
13041305
}
13051306

13061307
crate trait GetDefId {
@@ -1403,6 +1404,16 @@ impl Type {
14031404
matches!(self, Type::Generic(_))
14041405
}
14051406

1407+
crate fn is_primitive(&self) -> bool {
1408+
match self {
1409+
Self::Primitive(_) => true,
1410+
Self::BorrowedRef { ref type_, .. } | Self::RawPointer(_, ref type_) => {
1411+
type_.is_primitive()
1412+
}
1413+
_ => false,
1414+
}
1415+
}
1416+
14061417
crate fn projection(&self) -> Option<(&Type, DefId, Symbol)> {
14071418
let (self_, trait_, name) = match self {
14081419
QPath { self_type, trait_, name } => (self_type, trait_, name),

src/librustdoc/clean/utils.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,20 @@ crate fn get_real_types(
171171
cx: &DocContext<'_>,
172172
recurse: i32,
173173
) -> FxHashSet<(Type, TypeKind)> {
174+
fn insert(res: &mut FxHashSet<(Type, TypeKind)>, cx: &DocContext<'_>, ty: Type) {
175+
if let Some(kind) = ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
176+
res.insert((ty, kind));
177+
} else if ty.is_primitive() {
178+
// This is a primitive, let's store it as such.
179+
res.insert((ty, TypeKind::Primitive));
180+
}
181+
}
174182
let mut res = FxHashSet::default();
175183
if recurse >= 10 {
176184
// FIXME: remove this whole recurse thing when the recursion bug is fixed
177185
return res;
178186
}
187+
179188
if arg.is_full_generic() {
180189
let arg_s = Symbol::intern(&arg.print(&cx.cache).to_string());
181190
if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g {
@@ -194,11 +203,7 @@ crate fn get_real_types(
194203
if !adds.is_empty() {
195204
res.extend(adds);
196205
} else if !ty.is_full_generic() {
197-
if let Some(kind) =
198-
ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx))
199-
{
200-
res.insert((ty, kind));
201-
}
206+
insert(&mut res, cx, ty);
202207
}
203208
}
204209
}
@@ -212,26 +217,22 @@ crate fn get_real_types(
212217
if !adds.is_empty() {
213218
res.extend(adds);
214219
} else if !ty.is_full_generic() {
215-
if let Some(kind) = ty.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
216-
res.insert((ty.clone(), kind));
217-
}
220+
insert(&mut res, cx, ty);
218221
}
219222
}
220223
}
221224
}
222225
} else {
223-
if let Some(kind) = arg.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
224-
res.insert((arg.clone(), kind));
225-
}
226+
insert(&mut res, cx, arg.clone());
226227
if let Some(gens) = arg.generics() {
227228
for gen in gens.iter() {
228229
if gen.is_full_generic() {
229230
let adds = get_real_types(generics, gen, cx, recurse + 1);
230231
if !adds.is_empty() {
231232
res.extend(adds);
232233
}
233-
} else if let Some(kind) = gen.def_id().map(|did| cx.tcx.def_kind(did).clean(cx)) {
234-
res.insert((gen.clone(), kind));
234+
} else {
235+
insert(&mut res, cx, gen.clone());
235236
}
236237
}
237238
}

src/librustdoc/formats/item_type.rs

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ impl From<clean::TypeKind> for ItemType {
119119
clean::TypeKind::Attr => ItemType::ProcAttribute,
120120
clean::TypeKind::Derive => ItemType::ProcDerive,
121121
clean::TypeKind::TraitAlias => ItemType::TraitAlias,
122+
clean::TypeKind::Primitive => ItemType::Primitive,
122123
}
123124
}
124125
}

src/librustdoc/html/render/cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ crate fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
7878
desc: item.doc_value().map_or_else(String::new, |s| short_markdown_summary(&s)),
7979
parent: Some(did),
8080
parent_idx: None,
81-
search_type: get_index_search_type(&item, None),
81+
search_type: get_index_search_type(&item, Some(cache)),
8282
});
8383
for alias in item.attrs.get_doc_aliases() {
8484
cache

src/test/rustdoc-js/primitive.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// exact-check
2+
3+
const QUERY = [
4+
"i32",
5+
"str",
6+
"TotoIsSomewhere",
7+
];
8+
9+
const EXPECTED = [
10+
{
11+
'in_args': [
12+
{ 'path': 'primitive', 'name': 'foo' },
13+
],
14+
},
15+
{
16+
'returned': [
17+
{ 'path': 'primitive', 'name': 'foo' },
18+
],
19+
},
20+
{
21+
'others': [],
22+
'in_args': [],
23+
'returned': [],
24+
},
25+
];

src/test/rustdoc-js/primitive.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pub fn foo(i: i32) -> &'static str {
2+
"hello"
3+
}
4+
5+
pub fn foo2<TotoIsSomewhere>(i: &TotoIsSomewhere, j: TotoIsSomewhere) {}

src/tools/rustdoc-js/tester.js

+4
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ function betterLookingDiff(entry, data) {
203203
if (!entry.hasOwnProperty(key)) {
204204
continue;
205205
}
206+
if (!data || !data.hasOwnProperty(key)) {
207+
output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';
208+
continue;
209+
}
206210
let value = data[key];
207211
if (value !== entry[key]) {
208212
output += '-' + spaces + contentToDiffLine(key, entry[key]) + '\n';

0 commit comments

Comments
 (0)