about summary refs log tree commit diff
diff options
context:
space:
mode:
author许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com>2024-06-09 20:54:23 +0100
committerGitHub <noreply@github.com>2024-06-09 20:54:23 +0100
commit1fb48053416e96f5e19d439ff4cbcb83ca1ff6ad (patch)
tree9d7f2c8337566bf75c503a5ae57360eff12dd0c4
parentcbb986945b56a2f5f3a3331a5bb00b72e4e393f6 (diff)
parent8865b8c63900acb1834390344fc1cc43ae209f08 (diff)
downloadrust-1fb48053416e96f5e19d439ff4cbcb83ca1ff6ad.tar.gz
rust-1fb48053416e96f5e19d439ff4cbcb83ca1ff6ad.zip
Rollup merge of #126176 - notriddle:notriddle/fix-type-name-normalize, r=fmease
rustdoc-search: use lowercase, non-normalized name for type search

The type name ID map has underscores in its names, so the query element should have them, too.

Fixes #125993
-rw-r--r--src/librustdoc/html/static/js/search.js12
-rw-r--r--tests/rustdoc-js/underscoredtype.js62
-rw-r--r--tests/rustdoc-js/underscoredtype.rs8
3 files changed, 78 insertions, 4 deletions
diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js
index 8ac4b53673f..a0ab262bf0b 100644
--- a/src/librustdoc/html/static/js/search.js
+++ b/src/librustdoc/html/static/js/search.js
@@ -2399,15 +2399,19 @@ function initSearch(rawSearchIndex) {
              * @param {boolean} isAssocType
              */
             function convertNameToId(elem, isAssocType) {
-                if (typeNameIdMap.has(elem.normalizedPathLast) &&
-                    (isAssocType || !typeNameIdMap.get(elem.normalizedPathLast).assocOnly)) {
-                    elem.id = typeNameIdMap.get(elem.normalizedPathLast).id;
+                const loweredName = elem.pathLast.toLowerCase();
+                if (typeNameIdMap.has(loweredName) &&
+                    (isAssocType || !typeNameIdMap.get(loweredName).assocOnly)) {
+                    elem.id = typeNameIdMap.get(loweredName).id;
                 } else if (!parsedQuery.literalSearch) {
                     let match = null;
                     let matchDist = maxEditDistance + 1;
                     let matchName = "";
                     for (const [name, {id, assocOnly}] of typeNameIdMap) {
-                        const dist = editDistance(name, elem.normalizedPathLast, maxEditDistance);
+                        const dist = Math.min(
+                            editDistance(name, loweredName, maxEditDistance),
+                            editDistance(name, elem.normalizedPathLast, maxEditDistance),
+                        );
                         if (dist <= matchDist && dist <= maxEditDistance &&
                             (isAssocType || !assocOnly)) {
                             if (dist === matchDist && matchName > name) {
diff --git a/tests/rustdoc-js/underscoredtype.js b/tests/rustdoc-js/underscoredtype.js
new file mode 100644
index 00000000000..06e3b07b096
--- /dev/null
+++ b/tests/rustdoc-js/underscoredtype.js
@@ -0,0 +1,62 @@
+const EXPECTED = [
+    {
+        'query': 'pid_t',
+        'correction': null,
+        'proposeCorrectionFrom': null,
+        'proposeCorrectionTo': null,
+        'others': [
+            { 'path': 'underscoredtype::unix', 'name': 'pid_t' },
+        ],
+        'returned': [
+            { 'path': 'underscoredtype::unix', 'name': 'set_pid' },
+        ],
+        'returned': [
+            { 'path': 'underscoredtype::unix', 'name': 'get_pid' },
+        ],
+    },
+    {
+        'query': 'pidt',
+        'correction': 'pid_t',
+        'proposeCorrectionFrom': null,
+        'proposeCorrectionTo': null,
+        'others': [
+            { 'path': 'underscoredtype::unix', 'name': 'pid_t' },
+        ],
+        'returned': [
+            { 'path': 'underscoredtype::unix', 'name': 'set_pid' },
+        ],
+        'returned': [
+            { 'path': 'underscoredtype::unix', 'name': 'get_pid' },
+        ],
+    },
+    {
+        'query': 'unix::pid_t',
+        'correction': null,
+        'proposeCorrectionFrom': null,
+        'proposeCorrectionTo': null,
+        'others': [
+            { 'path': 'underscoredtype::unix', 'name': 'pid_t' },
+        ],
+        'returned': [
+            { 'path': 'underscoredtype::unix', 'name': 'set_pid' },
+        ],
+        'returned': [
+            { 'path': 'underscoredtype::unix', 'name': 'get_pid' },
+        ],
+    },
+    {
+        'query': 'unix::pidt',
+        'correction': 'pid_t',
+        'proposeCorrectionFrom': null,
+        'proposeCorrectionTo': null,
+        'others': [
+            { 'path': 'underscoredtype::unix', 'name': 'pid_t' },
+        ],
+        'returned': [
+            { 'path': 'underscoredtype::unix', 'name': 'set_pid' },
+        ],
+        'returned': [
+            { 'path': 'underscoredtype::unix', 'name': 'get_pid' },
+        ],
+    },
+];
diff --git a/tests/rustdoc-js/underscoredtype.rs b/tests/rustdoc-js/underscoredtype.rs
new file mode 100644
index 00000000000..83c532cf855
--- /dev/null
+++ b/tests/rustdoc-js/underscoredtype.rs
@@ -0,0 +1,8 @@
+pub mod unix {
+    #[allow(non_camel_case_types)]
+    pub type pid_t = i32;
+    pub fn get_pid() -> pid_t {
+        0
+    }
+    pub fn set_pid(_: pid_t) {}
+}