diff options
| author | bors <bors@rust-lang.org> | 2025-01-17 09:03:22 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-01-17 09:03:22 +0000 |
| commit | 73c0ae6aec8f3d07467dfb9339761fa2eec92a44 (patch) | |
| tree | 63609aaa5bbc9a876366bfc42e077315183d7370 /src | |
| parent | 0c2c096e1ac471b0c34629f9820a7cb1e6d4695d (diff) | |
| parent | 1360e76329a1e3b4590f9f1998779d663e0da003 (diff) | |
| download | rust-73c0ae6aec8f3d07467dfb9339761fa2eec92a44.tar.gz rust-73c0ae6aec8f3d07467dfb9339761fa2eec92a44.zip | |
Auto merge of #135615 - matthiaskrgr:rollup-ra7vftt, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #131806 (Treat other items as functions for the purpose of type-based search) - #134980 (Location-sensitive polonius prototype: endgame) - #135558 (Detect if-else chains with a missing final else in type errors) - #135594 (fix error for when results in a rustdoc-js test are in the wrong order) - #135601 (Fix suggestion to convert dereference of raw pointer to ref) - #135604 (Expand docs for `E0207` with additional example) r? `@ghost` `@rustbot` modify labels: rollup
Diffstat (limited to 'src')
| -rw-r--r-- | src/doc/rustdoc/src/read-documentation/search.md | 14 | ||||
| -rw-r--r-- | src/librustdoc/html/render/search_index.rs | 27 | ||||
| -rw-r--r-- | src/librustdoc/html/static/js/search.js | 16 | ||||
| -rw-r--r-- | src/tools/rustdoc-js/tester.js | 2 |
4 files changed, 57 insertions, 2 deletions
diff --git a/src/doc/rustdoc/src/read-documentation/search.md b/src/doc/rustdoc/src/read-documentation/search.md index e06dcdb7ed2..bace2f5f953 100644 --- a/src/doc/rustdoc/src/read-documentation/search.md +++ b/src/doc/rustdoc/src/read-documentation/search.md @@ -52,9 +52,10 @@ methods on the allocator or free functions. [`Layout`]: ../../alloc/index.html?search=Layout&filter-crate=alloc -## Searching By Type Signature for functions +## Searching By Type Signature If you know more specifically what the function you want to look at does, +or you want to know how to get from one type to another, Rustdoc can search by more than one type at once in the parameters and return value. Multiple parameters are separated by `,` commas, and the return value is written with after a `->` arrow. @@ -86,6 +87,17 @@ the standard library and functions that are included in the results list: [iterasslice]: ../../std/vec/struct.Vec.html?search=vec%3A%3Aintoiter<T>%20->%20[T]&filter-crate=std [iterreduce]: ../../std/index.html?search=iterator<T>%2C%20fnmut%20->%20T&filter-crate=std +### Non-functions in type-based search +Certain items that are not functions are treated as though they +were a semantically equivelent function. + +For example, struct fields are treated as though they were getter methods. +This means that a search for `CpuidResult -> u32` will show +the `CpuidResult::eax` field in the results. + +Additionally, `const` and `static` items are treated as nullary functions, +so `-> u32` will match `u32::MAX`. + ### How type-based search works In a complex type-based search, Rustdoc always treats every item's name as literal. diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index 66c52bec4ba..e4a9a2b512e 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -840,6 +840,22 @@ pub(crate) fn get_function_type_for_search( | clean::RequiredMethodItem(ref f) => { get_fn_inputs_and_outputs(f, tcx, impl_or_trait_generics, cache) } + clean::ConstantItem(ref c) => make_nullary_fn(&c.type_), + clean::StaticItem(ref s) => make_nullary_fn(&s.type_), + clean::StructFieldItem(ref t) => { + let Some(parent) = parent else { + return None; + }; + let mut rgen: FxIndexMap<SimplifiedParam, (isize, Vec<RenderType>)> = + Default::default(); + let output = get_index_type(t, vec![], &mut rgen); + let input = RenderType { + id: Some(RenderTypeId::DefId(parent)), + generics: None, + bindings: None, + }; + (vec![input], vec![output], vec![], vec![]) + } _ => return None, }; @@ -1353,6 +1369,17 @@ fn simplify_fn_constraint<'a>( res.push((ty_constrained_assoc, ty_constraints)); } +/// Create a fake nullary function. +/// +/// Used to allow type-based search on constants and statics. +fn make_nullary_fn( + clean_type: &clean::Type, +) -> (Vec<RenderType>, Vec<RenderType>, Vec<Symbol>, Vec<Vec<RenderType>>) { + let mut rgen: FxIndexMap<SimplifiedParam, (isize, Vec<RenderType>)> = Default::default(); + let output = get_index_type(clean_type, vec![], &mut rgen); + (vec![], vec![output], vec![], vec![]) +} + /// Return the full list of types when bounds have been resolved. /// /// i.e. `fn foo<A: Display, B: Option<A>>(x: u32, y: B)` will return diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 0a0550ab82f..660484c133c 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -63,6 +63,9 @@ const TY_PRIMITIVE = itemTypes.indexOf("primitive"); const TY_GENERIC = itemTypes.indexOf("generic"); const TY_IMPORT = itemTypes.indexOf("import"); const TY_TRAIT = itemTypes.indexOf("trait"); +const TY_FN = itemTypes.indexOf("fn"); +const TY_METHOD = itemTypes.indexOf("method"); +const TY_TYMETHOD = itemTypes.indexOf("tymethod"); const ROOT_PATH = typeof window !== "undefined" ? window.rootPath : "../"; // Hard limit on how deep to recurse into generics when doing type-driven search. @@ -191,6 +194,10 @@ function isEndCharacter(c) { return "=,>-])".indexOf(c) !== -1; } +function isFnLikeTy(ty) { + return ty === TY_FN || ty === TY_METHOD || ty === TY_TYMETHOD; +} + /** * Returns `true` if the given `c` character is a separator. * @@ -2766,6 +2773,15 @@ class DocSearch { return a - b; } + // in type based search, put functions first + if (parsedQuery.hasReturnArrow) { + a = !isFnLikeTy(aaa.item.ty); + b = !isFnLikeTy(bbb.item.ty); + if (a !== b) { + return a - b; + } + } + // Sort by distance in the path part, if specified // (less changes required to match means higher rankings) a = aaa.path_dist; diff --git a/src/tools/rustdoc-js/tester.js b/src/tools/rustdoc-js/tester.js index 7aa5e102e6d..ea575f27799 100644 --- a/src/tools/rustdoc-js/tester.js +++ b/src/tools/rustdoc-js/tester.js @@ -256,7 +256,7 @@ async function runSearch(query, expected, doSearch, loadedFile, queryName) { JSON.stringify(results[key][index]) + "'"); } else if (ignore_order === false && entry_pos < prev_pos) { error_text.push(queryName + "==> '" + JSON.stringify(elem) + "' was supposed " + - "to be before '" + JSON.stringify(results[key][entry_pos]) + "'"); + "to be before '" + JSON.stringify(results[key][prev_pos]) + "'"); } else { prev_pos = entry_pos; } |
