Skip to content

Commit c0ed53c

Browse files
authored
Rollup merge of #96430 - GuillaumeGomez:search-exclamation, r=notriddle
Fix handling of `!` in rustdoc search Fixes #96399. I also updated the eBNF. cc `@jsha` r? `@notriddle`
2 parents 875b22f + 4ea1499 commit c0ed53c

File tree

4 files changed

+148
-3
lines changed

4 files changed

+148
-3
lines changed

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,20 @@ window.initSearch = function(rawSearchIndex) {
310310
*/
311311
function getIdentEndPosition(parserState) {
312312
let end = parserState.pos;
313+
let foundExclamation = false;
313314
while (parserState.pos < parserState.length) {
314315
const c = parserState.userQuery[parserState.pos];
315316
if (!isIdentCharacter(c)) {
316-
if (isErrorCharacter(c)) {
317+
if (c === "!") {
318+
if (foundExclamation) {
319+
throw new Error("Cannot have more than one `!` in an ident");
320+
} else if (parserState.pos + 1 < parserState.length &&
321+
isIdentCharacter(parserState.userQuery[parserState.pos + 1]))
322+
{
323+
throw new Error("`!` can only be at the end of an ident");
324+
}
325+
foundExclamation = true;
326+
} else if (isErrorCharacter(c)) {
317327
throw new Error(`Unexpected \`${c}\``);
318328
} else if (
319329
isStopCharacter(c) ||
@@ -329,6 +339,7 @@ window.initSearch = function(rawSearchIndex) {
329339
}
330340
// Skip current ":".
331341
parserState.pos += 1;
342+
foundExclamation = false;
332343
} else {
333344
throw new Error(`Unexpected \`${c}\``);
334345
}
@@ -591,7 +602,7 @@ window.initSearch = function(rawSearchIndex) {
591602
*
592603
* The supported syntax by this parser is as follow:
593604
*
594-
* ident = *(ALPHA / DIGIT / "_")
605+
* ident = *(ALPHA / DIGIT / "_") [!]
595606
* path = ident *(DOUBLE-COLON ident)
596607
* arg = path [generics]
597608
* arg-without-generic = path

src/test/rustdoc-js-std/parser-errors.js

+20
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const QUERY = [
3535
"a,:",
3636
" a<> :",
3737
"mod : :",
38+
"a!a",
39+
"a!!",
3840
];
3941

4042
const PARSED = [
@@ -362,4 +364,22 @@ const PARSED = [
362364
userQuery: "mod : :",
363365
error: 'Unexpected `:`',
364366
},
367+
{
368+
elems: [],
369+
foundElems: 0,
370+
original: "a!a",
371+
returned: [],
372+
typeFilter: -1,
373+
userQuery: "a!a",
374+
error: '`!` can only be at the end of an ident',
375+
},
376+
{
377+
elems: [],
378+
foundElems: 0,
379+
original: "a!!",
380+
returned: [],
381+
typeFilter: -1,
382+
userQuery: "a!!",
383+
error: 'Cannot have more than one `!` in an ident',
384+
},
365385
];
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const QUERY = [
2+
"R<!>",
3+
"!",
4+
"a!",
5+
"a!::b",
6+
"a!::b!",
7+
];
8+
9+
const PARSED = [
10+
{
11+
elems: [{
12+
name: "r",
13+
fullPath: ["r"],
14+
pathWithoutLast: [],
15+
pathLast: "r",
16+
generics: [
17+
{
18+
name: "!",
19+
fullPath: ["!"],
20+
pathWithoutLast: [],
21+
pathLast: "!",
22+
generics: [],
23+
},
24+
],
25+
}],
26+
foundElems: 1,
27+
original: "R<!>",
28+
returned: [],
29+
typeFilter: -1,
30+
userQuery: "r<!>",
31+
error: null,
32+
},
33+
{
34+
elems: [{
35+
name: "!",
36+
fullPath: ["!"],
37+
pathWithoutLast: [],
38+
pathLast: "!",
39+
generics: [],
40+
}],
41+
foundElems: 1,
42+
original: "!",
43+
returned: [],
44+
typeFilter: -1,
45+
userQuery: "!",
46+
error: null,
47+
},
48+
{
49+
elems: [{
50+
name: "a!",
51+
fullPath: ["a!"],
52+
pathWithoutLast: [],
53+
pathLast: "a!",
54+
generics: [],
55+
}],
56+
foundElems: 1,
57+
original: "a!",
58+
returned: [],
59+
typeFilter: -1,
60+
userQuery: "a!",
61+
error: null,
62+
},
63+
{
64+
elems: [{
65+
name: "a!::b",
66+
fullPath: ["a!", "b"],
67+
pathWithoutLast: ["a!"],
68+
pathLast: "b",
69+
generics: [],
70+
}],
71+
foundElems: 1,
72+
original: "a!::b",
73+
returned: [],
74+
typeFilter: -1,
75+
userQuery: "a!::b",
76+
error: null,
77+
},
78+
{
79+
elems: [{
80+
name: "a!::b!",
81+
fullPath: ["a!", "b!"],
82+
pathWithoutLast: ["a!"],
83+
pathLast: "b!",
84+
generics: [],
85+
}],
86+
foundElems: 1,
87+
original: "a!::b!",
88+
returned: [],
89+
typeFilter: -1,
90+
userQuery: "a!::b!",
91+
error: null,
92+
},
93+
];

src/test/rustdoc-js-std/parser-returned.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
const QUERY = ['-> F<P>', '-> P', '->,a', 'aaaaa->a'];
1+
const QUERY = [
2+
"-> F<P>",
3+
"-> P",
4+
"->,a",
5+
"aaaaa->a",
6+
"-> !",
7+
];
28

39
const PARSED = [
410
{
@@ -75,4 +81,19 @@ const PARSED = [
7581
userQuery: "aaaaa->a",
7682
error: null,
7783
},
84+
{
85+
elems: [],
86+
foundElems: 1,
87+
original: "-> !",
88+
returned: [{
89+
name: "!",
90+
fullPath: ["!"],
91+
pathWithoutLast: [],
92+
pathLast: "!",
93+
generics: [],
94+
}],
95+
typeFilter: -1,
96+
userQuery: "-> !",
97+
error: null,
98+
},
7899
];

0 commit comments

Comments
 (0)