From 07072812ca00473176be7df5a0ed18517c9c0889 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 14 Oct 2025 15:16:11 -0500 Subject: [PATCH] rustdoc: account for path distance in doc aliases --- src/librustdoc/html/static/js/search.js | 13 +++++++++++-- tests/rustdoc-js/alias-path-distance-146214.js | 18 ++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 0929d351463c..f187d8d70339 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -3933,16 +3933,25 @@ class DocSearch { * @returns {Promise} */ const handleAlias = async(name, alias, dist, index) => { + const item = nonnull(await this.getRow(alias, false)); + // space both is an alias for ::, + // and is also allowed to appear in doc alias names + const path_dist = name.includes(" ") || parsedQuery.elems.length === 0 ? + 0 : checkRowPath(parsedQuery.elems[0].pathWithoutLast, item); + // path distance exceeds max, omit alias from results + if (path_dist === null) { + return null; + } return { id: alias, dist, - path_dist: 0, + path_dist, index, alias: name, is_alias: true, elems: [], // only used in type-based queries returned: [], // only used in type-based queries - item: nonnull(await this.getRow(alias, false)), + item, }; }; /** diff --git a/tests/rustdoc-js/alias-path-distance-146214.js b/tests/rustdoc-js/alias-path-distance-146214.js index 30413bf9ee2b..722cf00de910 100644 --- a/tests/rustdoc-js/alias-path-distance-146214.js +++ b/tests/rustdoc-js/alias-path-distance-146214.js @@ -3,7 +3,17 @@ // consider path distance for doc aliases // regression test for -const EXPECTED = { - 'query': 'Foo::zzz', - 'others': [{ 'path': 'alias_path_distance::Foo', 'name': 'baz' }], -}; +const EXPECTED = [ + { + 'query': 'Foo::zzz', + 'others': [ + { 'path': 'alias_path_distance::Foo', 'name': 'baz' }, + ], + }, + { + 'query': '"Foo::zzz"', + 'others': [ + { 'path': 'alias_path_distance::Foo', 'name': 'baz' }, + ], + }, +];