diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 0929d351463cc..7d0182348841b 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(" ") ? + 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 new file mode 100644 index 0000000000000..722cf00de9100 --- /dev/null +++ b/tests/rustdoc-js/alias-path-distance-146214.js @@ -0,0 +1,19 @@ +// exact-check + +// consider path distance for doc aliases +// regression test for + +const EXPECTED = [ + { + 'query': 'Foo::zzz', + 'others': [ + { 'path': 'alias_path_distance::Foo', 'name': 'baz' }, + ], + }, + { + 'query': '"Foo::zzz"', + 'others': [ + { 'path': 'alias_path_distance::Foo', 'name': 'baz' }, + ], + }, +]; diff --git a/tests/rustdoc-js/alias-path-distance-146214.rs b/tests/rustdoc-js/alias-path-distance-146214.rs new file mode 100644 index 0000000000000..09d1068e3edee --- /dev/null +++ b/tests/rustdoc-js/alias-path-distance-146214.rs @@ -0,0 +1,14 @@ +#![crate_name = "alias_path_distance"] + +pub struct Foo; +pub struct Bar; + +impl Foo { + #[doc(alias = "zzz")] + pub fn baz() {} +} + +impl Bar { + #[doc(alias = "zzz")] + pub fn baz() {} +} diff --git a/tests/rustdoc-js/alias-rank-lower-140968.js b/tests/rustdoc-js/alias-rank-lower-140968.js new file mode 100644 index 0000000000000..976fd314e429b --- /dev/null +++ b/tests/rustdoc-js/alias-rank-lower-140968.js @@ -0,0 +1,10 @@ +// rank doc aliases lower than exact matches +// regression test for + +const EXPECTED = { + 'query': 'Foo', + 'others': [ + { 'path': 'alias_rank_lower', 'name': 'Foo' }, + { 'path': 'alias_rank_lower', 'name': 'Bar' }, + ], +}; diff --git a/tests/rustdoc-js/alias-rank-lower-140968.rs b/tests/rustdoc-js/alias-rank-lower-140968.rs new file mode 100644 index 0000000000000..1b0d2abfdcdcc --- /dev/null +++ b/tests/rustdoc-js/alias-rank-lower-140968.rs @@ -0,0 +1,6 @@ +#![crate_name = "alias_rank_lower"] + +pub struct Foo; + +#[doc(alias = "Foo")] +pub struct Bar;