Skip to content

rustdoc search: allow queries to end in an empty path segment #132569

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,6 @@ function createQueryElement(query, parserState, name, generics, isInGenerics) {
const quadcolon = /::\s*::/.exec(path);
if (path.startsWith("::")) {
throw ["Paths cannot start with ", "::"];
} else if (path.endsWith("::")) {
throw ["Paths cannot end with ", "::"];
} else if (quadcolon !== null) {
throw ["Unexpected ", quadcolon[0]];
}
Expand Down Expand Up @@ -3974,18 +3972,19 @@ class DocSearch {

if (parsedQuery.foundElems === 1 && !parsedQuery.hasReturnArrow) {
const elem = parsedQuery.elems[0];
for (const id of this.nameTrie.search(elem.normalizedPathLast, this.tailTable)) {
// use arrow functions to preserve `this`.
const handleNameSearch = id => {
const row = this.searchIndex[id];
if (!typePassesFilter(elem.typeFilter, row.ty) ||
(filterCrates !== null && row.crate !== filterCrates)) {
continue;
return;
}

let pathDist = 0;
if (elem.fullPath.length > 1) {
pathDist = checkPath(elem.pathWithoutLast, row);
if (pathDist === null) {
continue;
return;
}
}

Expand All @@ -4008,9 +4007,20 @@ class DocSearch {
maxEditDistance,
);
}
};
if (elem.normalizedPathLast !== "") {
const last = elem.normalizedPathLast;
for (const id of this.nameTrie.search(last, this.tailTable)) {
handleNameSearch(id);
}
}
const length = this.searchIndex.length;

for (let i = 0, nSearchIndex = length; i < nSearchIndex; ++i) {
// queries that end in :: bypass the trie
if (elem.normalizedPathLast === "") {
handleNameSearch(i);
}
const row = this.searchIndex[i];
if (filterCrates !== null && row.crate !== filterCrates) {
continue;
Expand Down
8 changes: 0 additions & 8 deletions tests/rustdoc-js-std/parser-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,6 @@ const PARSED = [
returned: [],
error: "Unexpected `:: ::`",
},
{
query: "a::b::",
elems: [],
foundElems: 0,
userQuery: "a::b::",
returned: [],
error: "Paths cannot end with `::`",
},
{
query: ":a",
elems: [],
Expand Down
6 changes: 6 additions & 0 deletions tests/rustdoc-js-std/path-end-empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const EXPECTED = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this test, can you add a new one in rustdoc-js instead so we can check exactly what is returned? We have type with very long names so I don't think you need to update the sources for it, just pick a type and ensure its items are listed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not the length of the type that matters, it's the length of the associated item.

but yeah, it could probably use a more extreme testcase.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you add the test?

'query': 'Option::',
'others': [
{ 'path': 'std::option::Option', 'name': 'get_or_insert_default' },
],
}
Loading