Skip to content

Commit 0a5a8c4

Browse files
Rollup merge of rust-lang#135302 - lolbinarycat:rustdoc-search-return-sort-134935, r=notriddle
for purely return-type based searches, deprioritize clone-like functions closes rust-lang#134935
2 parents 55247be + ebd5ce1 commit 0a5a8c4

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/librustdoc/html/static/js/search.js

+46
Original file line numberDiff line numberDiff line change
@@ -2717,9 +2717,26 @@ class DocSearch {
27172717
const normalizedUserQuery = parsedQuery.userQuery.toLowerCase();
27182718
const isMixedCase = normalizedUserQuery !== userQuery;
27192719
const result_list = [];
2720+
const isReturnTypeQuery = parsedQuery.elems.length === 0 ||
2721+
typeInfo === "returned";
27202722
for (const result of results.values()) {
27212723
result.item = this.searchIndex[result.id];
27222724
result.word = this.searchIndex[result.id].word;
2725+
if (isReturnTypeQuery) {
2726+
// we are doing a return-type based search,
2727+
// deprioritize "clone-like" results,
2728+
// ie. functions that also take the queried type as an argument.
2729+
const hasType = result.item && result.item.type;
2730+
if (!hasType) {
2731+
continue;
2732+
}
2733+
const inputs = result.item.type.inputs;
2734+
const where_clause = result.item.type.where_clause;
2735+
if (containsTypeFromQuery(inputs, where_clause)) {
2736+
result.path_dist *= 100;
2737+
result.dist *= 100;
2738+
}
2739+
}
27232740
result_list.push(result);
27242741
}
27252742

@@ -3540,6 +3557,35 @@ class DocSearch {
35403557
return false;
35413558
}
35423559

3560+
/**
3561+
* This function checks if the given list contains any
3562+
* (non-generic) types mentioned in the query.
3563+
*
3564+
* @param {Array<FunctionType>} list - A list of function types.
3565+
* @param {[FunctionType]} where_clause - Trait bounds for generic items.
3566+
*/
3567+
function containsTypeFromQuery(list, where_clause) {
3568+
if (!list) return false;
3569+
for (const ty of parsedQuery.returned) {
3570+
// negative type ids are generics
3571+
if (ty.id < 0) {
3572+
continue;
3573+
}
3574+
if (checkIfInList(list, ty, where_clause, null, 0)) {
3575+
return true;
3576+
}
3577+
}
3578+
for (const ty of parsedQuery.elems) {
3579+
if (ty.id < 0) {
3580+
continue;
3581+
}
3582+
if (checkIfInList(list, ty, where_clause, null, 0)) {
3583+
return true;
3584+
}
3585+
}
3586+
return false;
3587+
}
3588+
35433589
/**
35443590
* This function checks if the object (`row`) matches the given type (`elem`) and its
35453591
* generics (if any).
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// test that `clone`-like functions are sorted lower when
2+
// a search is based soley on return type
3+
4+
const FILTER_CRATE = "core";
5+
6+
const EXPECTED = [
7+
{
8+
'query': '-> AllocError',
9+
'others': [
10+
{ 'path': 'core::alloc::Allocator', 'name': 'allocate' },
11+
{ 'path': 'core::alloc::AllocError', 'name': 'clone' },
12+
],
13+
},
14+
{
15+
'query': 'AllocError',
16+
'returned': [
17+
{ 'path': 'core::alloc::Allocator', 'name': 'allocate' },
18+
{ 'path': 'core::alloc::AllocError', 'name': 'clone' },
19+
],
20+
},
21+
{
22+
'query': '-> &str',
23+
'others': [
24+
// type_name_of_val should not be consider clone-like
25+
{ 'path': 'core::any', 'name': 'type_name_of_val' },
26+
// this returns `Option<&str>`, and thus should be sorted lower
27+
{ 'path': 'core::str::Split', 'name': 'next' },
28+
],
29+
},
30+
]

0 commit comments

Comments
 (0)