Skip to content
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

rustdoc-search: add support for nested generics #109802

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
11 changes: 11 additions & 0 deletions src/doc/rustdoc/src/how-to-read-rustdoc.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ can be matched with the following queries:
* `trait:Iterator<primitive:u32> -> primitive:usize`
* `Iterator -> usize`

Generics and function parameters are order-agnostic, but sensitive to nesting
and number of matches. For example, a function with the signature
`fn read_all(&mut self: impl Read) -> Result<Vec<u8>, Error>`
will match these queries:

* `Read -> Result<Vec<u8>, Error>`
* `Read -> Result<Error, Vec>`
* `Read -> Result<Vec<u8>>`

But it *does not* match `Result<Vec, u8>` or `Result<u8<Vec>>`.

### Changing displayed theme

You can change the displayed theme by opening the settings menu (the gear
Expand Down
21 changes: 10 additions & 11 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,7 @@ function initSearch(rawSearchIndex) {
if (parserState.pos < parserState.length &&
parserState.userQuery[parserState.pos] === "<"
) {
if (isInGenerics) {
throw ["Unexpected ", "<", " after ", "<"];
} else if (start >= end) {
if (start >= end) {
throw ["Found generics without a path"];
}
parserState.pos += 1;
Expand Down Expand Up @@ -765,13 +763,10 @@ function initSearch(rawSearchIndex) {
* ident = *(ALPHA / DIGIT / "_")
* path = ident *(DOUBLE-COLON ident) [!]
* arg = [type-filter *WS COLON *WS] path [generics]
* arg-without-generic = [type-filter *WS COLON *WS] path
* type-sep = COMMA/WS *(COMMA/WS)
* nonempty-arg-list = *(type-sep) arg *(type-sep arg) *(type-sep)
* nonempty-arg-list-without-generics = *(type-sep) arg-without-generic
* *(type-sep arg-without-generic) *(type-sep)
* generics = OPEN-ANGLE-BRACKET [ nonempty-arg-list-without-generics ] *(type-sep)
* CLOSE-ANGLE-BRACKET/EOF
* generics = OPEN-ANGLE-BRACKET [ nonempty-arg-list ] *(type-sep)
* CLOSE-ANGLE-BRACKET
* return-args = RETURN-ARROW *(type-sep) nonempty-arg-list
*
* exact-search = [type-filter *WS COLON] [ RETURN-ARROW ] *WS QUOTE ident QUOTE [ generics ]
Expand Down Expand Up @@ -1127,7 +1122,7 @@ function initSearch(rawSearchIndex) {
currentEntryElems = [];
elems.set(entry.name, currentEntryElems);
}
currentEntryElems.push(entry.ty);
currentEntryElems.push(entry);
}
// We need to find the type that matches the most to remove it in order
// to move forward.
Expand All @@ -1136,8 +1131,12 @@ function initSearch(rawSearchIndex) {
return false;
}
const matchElems = elems.get(generic.name);
const matchIdx = matchElems.findIndex(tmp_elem =>
typePassesFilter(generic.typeFilter, tmp_elem));
const matchIdx = matchElems.findIndex(tmp_elem => {
if (checkGenerics(tmp_elem, generic, 0, maxEditDistance) !== 0) {
return false;
}
return typePassesFilter(generic.typeFilter, tmp_elem.ty);
});
if (matchIdx === -1) {
return false;
}
Expand Down
124 changes: 122 additions & 2 deletions tests/rustdoc-js-std/parser-generics.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
const QUERY = ['A<B<C<D>, E>', 'p<> u8', '"p"<a>'];
const QUERY = [
'A<B<C<D>, E>',
'p<> u8',
'"p"<a>',
'p<u<x>>',
'p<u<x>, r>',
'p<u<x, r>>',
];

const PARSED = [
{
Expand All @@ -7,7 +14,7 @@ const PARSED = [
original: 'A<B<C<D>, E>',
returned: [],
userQuery: 'a<b<c<d>, e>',
error: 'Unexpected `<` after `<`',
error: 'Unclosed `<`',
},
{
elems: [
Expand Down Expand Up @@ -59,4 +66,117 @@ const PARSED = [
userQuery: '"p"<a>',
error: null,
},
{
elems: [
{
name: "p",
fullPath: ["p"],
pathWithoutLast: [],
pathLast: "p",
generics: [
{
name: "u",
fullPath: ["u"],
pathWithoutLast: [],
pathLast: "u",
generics: [
{
name: "x",
fullPath: ["x"],
pathWithoutLast: [],
pathLast: "x",
generics: [],
},
],
},
],
typeFilter: -1,
},
],
foundElems: 1,
original: 'p<u<x>>',
returned: [],
userQuery: 'p<u<x>>',
error: null,
},
{
elems: [
{
name: "p",
fullPath: ["p"],
pathWithoutLast: [],
pathLast: "p",
generics: [
{
name: "u",
fullPath: ["u"],
pathWithoutLast: [],
pathLast: "u",
generics: [
{
name: "x",
fullPath: ["x"],
pathWithoutLast: [],
pathLast: "x",
generics: [],
},
],
},
{
name: "r",
fullPath: ["r"],
pathWithoutLast: [],
pathLast: "r",
generics: [],
},
],
typeFilter: -1,
},
],
foundElems: 1,
original: 'p<u<x>, r>',
returned: [],
userQuery: 'p<u<x>, r>',
error: null,
},
{
elems: [
{
name: "p",
fullPath: ["p"],
pathWithoutLast: [],
pathLast: "p",
generics: [
{
name: "u",
fullPath: ["u"],
pathWithoutLast: [],
pathLast: "u",
generics: [
{
name: "x",
fullPath: ["x"],
pathWithoutLast: [],
pathLast: "x",
generics: [],
},
{
name: "r",
fullPath: ["r"],
pathWithoutLast: [],
pathLast: "r",
generics: [],
},
],
},
],
typeFilter: -1,
},
],
foundElems: 1,
original: 'p<u<x, r>>',
returned: [],
userQuery: 'p<u<x, r>>',
error: null,
},
];
33 changes: 33 additions & 0 deletions tests/rustdoc-js/generics-nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// exact-check

const QUERY = [
'-> Out<First<Second>>',
'-> Out<Second<First>>',
'-> Out<First, Second>',
'-> Out<Second, First>',
];

const EXPECTED = [
{
// -> Out<First<Second>>
'others': [
{ 'path': 'generics_nested', 'name': 'alef' },
],
},
{
// -> Out<Second<First>>
'others': [],
},
{
// -> Out<First, Second>
'others': [
{ 'path': 'generics_nested', 'name': 'bet' },
],
},
{
// -> Out<Second, First>
'others': [
{ 'path': 'generics_nested', 'name': 'bet' },
],
},
];
19 changes: 19 additions & 0 deletions tests/rustdoc-js/generics-nested.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub struct Out<A, B = ()> {
a: A,
b: B,
}

pub struct First<In = ()> {
in_: In,
}

pub struct Second;

// Out<First<Second>>
pub fn alef() -> Out<First<Second>> {
loop {}
}

pub fn bet() -> Out<First, Second> {
loop {}
}