From 3699e939e8f6a2517806a219c082723d50dd4288 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Thu, 5 Sep 2024 17:58:05 -0700 Subject: [PATCH] rustdoc-search: allow trailing `Foo ->` arg search --- src/librustdoc/html/static/js/externs.js | 1 + src/librustdoc/html/static/js/search.js | 10 ++-- tests/rustdoc-js-std/parser-errors.js | 9 ---- tests/rustdoc-js-std/parser-returned.js | 68 ++++++++++++++++++++++++ tests/rustdoc-js/never-search.js | 7 +++ 5 files changed, 81 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/html/static/js/externs.js b/src/librustdoc/html/static/js/externs.js index 6fd60d6cc34..3d3e0a8f838 100644 --- a/src/librustdoc/html/static/js/externs.js +++ b/src/librustdoc/html/static/js/externs.js @@ -41,6 +41,7 @@ let ParserState; * foundElems: number, * totalElems: number, * literalSearch: boolean, + * hasReturnArrow: boolean, * corrections: Array<{from: string, to: integer}> | null, * typeFingerprint: Uint32Array, * error: Array | null, diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 6f575e60ed4..4da0bbc787d 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -657,7 +657,7 @@ function createQueryElement(query, parserState, name, generics, isInGenerics) { } const typeFilter = parserState.typeFilter; parserState.typeFilter = null; - if (name === "!") { + if (name.trim() === "!") { if (typeFilter !== null && typeFilter !== "primitive") { throw [ "Invalid search type: primitive never type ", @@ -1795,6 +1795,7 @@ class DocSearch { // Total number of elements (includes generics). totalElems: 0, literalSearch: false, + hasReturnArrow: false, error: null, correction: null, proposeCorrectionFrom: null, @@ -1823,6 +1824,7 @@ class DocSearch { continue; } else if (c === "-" || c === ">") { if (isReturnArrow(parserState)) { + query.hasReturnArrow = true; break; } throw ["Unexpected ", c, " (did you mean ", "->", "?)"]; @@ -1889,9 +1891,7 @@ class DocSearch { // Get returned elements. getItemsBefore(query, parserState, query.returned, ""); // Nothing can come afterward! - if (query.returned.length === 0) { - throw ["Expected at least one item after ", "->"]; - } + query.hasReturnArrow = true; break; } else { parserState.pos += 1; @@ -3249,7 +3249,7 @@ class DocSearch { this.buildFunctionTypeFingerprint(elem, parsedQuery.typeFingerprint, fps); } - if (parsedQuery.foundElems === 1 && parsedQuery.returned.length === 0) { + if (parsedQuery.foundElems === 1 && !parsedQuery.hasReturnArrow) { if (parsedQuery.elems.length === 1) { const elem = parsedQuery.elems[0]; const length = this.searchIndex.length; diff --git a/tests/rustdoc-js-std/parser-errors.js b/tests/rustdoc-js-std/parser-errors.js index c4d7c2b0b85..5ce35bf511d 100644 --- a/tests/rustdoc-js-std/parser-errors.js +++ b/tests/rustdoc-js-std/parser-errors.js @@ -251,15 +251,6 @@ const PARSED = [ userQuery: "ab'", error: "Unexpected `'` after `b` (not a valid identifier)", }, - { - query: "a->", - elems: [], - foundElems: 0, - original: "a->", - returned: [], - userQuery: "a->", - error: "Expected at least one item after `->`", - }, { query: '"p" ', elems: [], diff --git a/tests/rustdoc-js-std/parser-returned.js b/tests/rustdoc-js-std/parser-returned.js index 44e517c49b5..8f68209bb96 100644 --- a/tests/rustdoc-js-std/parser-returned.js +++ b/tests/rustdoc-js-std/parser-returned.js @@ -94,4 +94,72 @@ const PARSED = [ userQuery: "-> !", error: null, }, + { + query: "a->", + elems: [{ + name: "a", + fullPath: ["a"], + pathWithoutLast: [], + pathLast: "a", + generics: [], + typeFilter: -1, + }], + foundElems: 1, + original: "a->", + returned: [], + userQuery: "a->", + hasReturnArrow: true, + error: null, + }, + { + query: "!->", + elems: [{ + name: "never", + fullPath: ["never"], + pathWithoutLast: [], + pathLast: "never", + generics: [], + typeFilter: 1, + }], + foundElems: 1, + original: "!->", + returned: [], + userQuery: "!->", + hasReturnArrow: true, + error: null, + }, + { + query: "! ->", + elems: [{ + name: "never", + fullPath: ["never"], + pathWithoutLast: [], + pathLast: "never", + generics: [], + typeFilter: 1, + }], + foundElems: 1, + original: "! ->", + returned: [], + userQuery: "! ->", + hasReturnArrow: true, + error: null, + }, + { + query: "primitive:!->", + elems: [{ + name: "never", + fullPath: ["never"], + pathWithoutLast: [], + pathLast: "never", + generics: [], + typeFilter: 1, + }], + foundElems: 1, + original: "primitive:!->", + returned: [], + userQuery: "primitive:!->", + hasReturnArrow: true, + error: null, + }, ]; diff --git a/tests/rustdoc-js/never-search.js b/tests/rustdoc-js/never-search.js index 9f18370c2f5..9cc62a5ed04 100644 --- a/tests/rustdoc-js/never-search.js +++ b/tests/rustdoc-js/never-search.js @@ -1,6 +1,13 @@ // exact-check const EXPECTED = [ + { + 'query': '! ->', + 'others': [ + { 'path': 'never_search', 'name': 'impossible' }, + { 'path': 'never_search', 'name': 'box_impossible' }, + ], + }, { 'query': '-> !', 'others': [