From 60a1abe4c5b6399e62144fc94d236b6f7eefcdf0 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Mon, 27 Dec 2021 18:39:35 -0800 Subject: [PATCH 1/8] Rename `rustdoc::html::render::cache` to `search_index` The old name wasn't very clear, while the new one makes it clear that this is the code responsible for creating the search index. --- src/librustdoc/clean/types.rs | 2 +- src/librustdoc/formats/cache.rs | 2 +- src/librustdoc/html/format.rs | 2 +- src/librustdoc/html/render/context.rs | 2 +- src/librustdoc/html/render/mod.rs | 2 +- src/librustdoc/html/render/{cache.rs => search_index.rs} | 0 src/librustdoc/json/mod.rs | 2 +- 7 files changed, 6 insertions(+), 6 deletions(-) rename src/librustdoc/html/render/{cache.rs => search_index.rs} (100%) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index fe32b75d668ce..f0e6d4315648e 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -38,7 +38,7 @@ use crate::clean::Clean; use crate::core::DocContext; use crate::formats::cache::Cache; use crate::formats::item_type::ItemType; -use crate::html::render::cache::ExternalLocation; +use crate::html::render::search_index::ExternalLocation; use crate::html::render::Context; crate use self::FnRetTy::*; diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 5813062ceab77..8a5c8651f9992 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -12,7 +12,7 @@ use crate::fold::DocFolder; use crate::formats::item_type::ItemType; use crate::formats::Impl; use crate::html::markdown::short_markdown_summary; -use crate::html::render::cache::{get_index_search_type, ExternalLocation}; +use crate::html::render::search_index::{get_index_search_type, ExternalLocation}; use crate::html::render::IndexItem; /// This cache is used to store information about the [`clean::Crate`] being diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 3a2effa625cf3..f7396f758b103 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -24,7 +24,7 @@ use rustc_target::spec::abi::Abi; use crate::clean::{self, utils::find_nearest_parent_module, ExternalCrate, ItemId, PrimitiveType}; use crate::formats::item_type::ItemType; use crate::html::escape::Escape; -use crate::html::render::cache::ExternalLocation; +use crate::html::render::search_index::ExternalLocation; use crate::html::render::Context; use super::url_parts_builder::UrlPartsBuilder; diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 45a436c448710..6729129027f55 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -13,8 +13,8 @@ use rustc_span::edition::Edition; use rustc_span::source_map::FileName; use rustc_span::symbol::sym; -use super::cache::{build_index, ExternalLocation}; use super::print_item::{full_path, item_path, print_item}; +use super::search_index::{build_index, ExternalLocation}; use super::templates; use super::write_shared::write_shared; use super::{ diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index eb606178d244d..3e7711181f730 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -23,7 +23,7 @@ //! These threads are not parallelized (they haven't been a bottleneck yet), and //! both occur before the crate is rendered. -crate mod cache; +crate mod search_index; #[cfg(test)] mod tests; diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/search_index.rs similarity index 100% rename from src/librustdoc/html/render/cache.rs rename to src/librustdoc/html/render/search_index.rs diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 0031e3915fa40..01641bae3845a 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -24,7 +24,7 @@ use crate::config::RenderOptions; use crate::error::Error; use crate::formats::cache::Cache; use crate::formats::FormatRenderer; -use crate::html::render::cache::ExternalLocation; +use crate::html::render::search_index::ExternalLocation; use crate::json::conversions::{from_item_id, IntoWithTcx}; #[derive(Clone)] From e19593f0e51196dc3528bb5fa8b89e81ae493092 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Mon, 27 Dec 2021 18:53:00 -0800 Subject: [PATCH 2/8] rustdoc: Remove some unnecessary `cache` parameters Based on https://github.com/rust-lang/rust/pull/80883#issuecomment-774437832. The `tcx` parameters do seem to be used though, so I only removed the `cache` parameters. --- src/librustdoc/formats/cache.rs | 2 +- src/librustdoc/html/render/search_index.rs | 35 ++++++++-------------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 8a5c8651f9992..528d48a8d059e 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -303,7 +303,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { desc, parent, parent_idx: None, - search_type: get_index_search_type(&item, self.tcx, self.cache), + search_type: get_index_search_type(&item, self.tcx), aliases: item.attrs.get_doc_aliases(), }); } diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index 631eacc961828..5d4df3ee5ff3a 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -42,7 +42,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt< desc, parent: Some(did), parent_idx: None, - search_type: get_index_search_type(item, tcx, cache), + search_type: get_index_search_type(item, tcx), aliases: item.attrs.get_doc_aliases(), }); } @@ -194,12 +194,11 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt< crate fn get_index_search_type<'tcx>( item: &clean::Item, tcx: TyCtxt<'tcx>, - cache: &Cache, ) -> Option { let (mut inputs, mut output) = match *item.kind { - clean::FunctionItem(ref f) => get_all_types(&f.generics, &f.decl, tcx, cache), - clean::MethodItem(ref m, _) => get_all_types(&m.generics, &m.decl, tcx, cache), - clean::TyMethodItem(ref m) => get_all_types(&m.generics, &m.decl, tcx, cache), + clean::FunctionItem(ref f) => get_all_types(&f.generics, &f.decl, tcx), + clean::MethodItem(ref m, _) => get_all_types(&m.generics, &m.decl, tcx), + clean::TyMethodItem(ref m) => get_all_types(&m.generics, &m.decl, tcx), _ => return None, }; @@ -254,14 +253,12 @@ crate fn get_real_types<'tcx>( tcx: TyCtxt<'tcx>, recurse: usize, res: &mut Vec, - cache: &Cache, ) { fn insert_ty( res: &mut Vec, tcx: TyCtxt<'_>, ty: Type, mut generics: Vec, - _cache: &Cache, ) { let is_full_generic = ty.is_full_generic(); @@ -350,21 +347,14 @@ crate fn get_real_types<'tcx>( for param_def in poly_trait.generic_params.iter() { match ¶m_def.kind { clean::GenericParamDefKind::Type { default: Some(ty), .. } => { - get_real_types( - generics, - ty, - tcx, - recurse + 1, - &mut ty_generics, - cache, - ) + get_real_types(generics, ty, tcx, recurse + 1, &mut ty_generics) } _ => {} } } } } - insert_ty(res, tcx, arg.clone(), ty_generics, cache); + insert_ty(res, tcx, arg.clone(), ty_generics); } // Otherwise we check if the trait bounds are "inlined" like `T: Option`... if let Some(bound) = generics.params.iter().find(|g| g.is_type() && g.name == arg_s) { @@ -372,10 +362,10 @@ crate fn get_real_types<'tcx>( for bound in bound.get_bounds().unwrap_or(&[]) { if let Some(path) = bound.get_trait_path() { let ty = Type::Path { path }; - get_real_types(generics, &ty, tcx, recurse + 1, &mut ty_generics, cache); + get_real_types(generics, &ty, tcx, recurse + 1, &mut ty_generics); } } - insert_ty(res, tcx, arg.clone(), ty_generics, cache); + insert_ty(res, tcx, arg.clone(), ty_generics); } } else { // This is not a type parameter. So for example if we have `T, U: Option`, and we're @@ -386,10 +376,10 @@ crate fn get_real_types<'tcx>( let mut ty_generics = Vec::new(); if let Some(arg_generics) = arg.generics() { for gen in arg_generics.iter() { - get_real_types(generics, gen, tcx, recurse + 1, &mut ty_generics, cache); + get_real_types(generics, gen, tcx, recurse + 1, &mut ty_generics); } } - insert_ty(res, tcx, arg.clone(), ty_generics, cache); + insert_ty(res, tcx, arg.clone(), ty_generics); } } @@ -401,7 +391,6 @@ crate fn get_all_types<'tcx>( generics: &Generics, decl: &FnDecl, tcx: TyCtxt<'tcx>, - cache: &Cache, ) -> (Vec, Vec) { let mut all_types = Vec::new(); for arg in decl.inputs.values.iter() { @@ -409,7 +398,7 @@ crate fn get_all_types<'tcx>( continue; } let mut args = Vec::new(); - get_real_types(generics, &arg.type_, tcx, 0, &mut args, cache); + get_real_types(generics, &arg.type_, tcx, 0, &mut args); if !args.is_empty() { all_types.extend(args); } else { @@ -423,7 +412,7 @@ crate fn get_all_types<'tcx>( let mut ret_types = Vec::new(); match decl.output { FnRetTy::Return(ref return_type) => { - get_real_types(generics, return_type, tcx, 0, &mut ret_types, cache); + get_real_types(generics, return_type, tcx, 0, &mut ret_types); if ret_types.is_empty() { if let Some(kind) = return_type.def_id_no_primitives().map(|did| tcx.def_kind(did).into()) From 2b801dcdd303e3e2a92e255d8cfb9427800403e5 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Mon, 27 Dec 2021 18:57:07 -0800 Subject: [PATCH 3/8] Move `ExternalLocation` to `clean::types` It was previously defined in `render::search_index` but wasn't used at all there. `clean::types` seems like a better fit since that's where `ExternalCrate` is defined. --- src/librustdoc/clean/types.rs | 11 ++++++++++- src/librustdoc/formats/cache.rs | 4 ++-- src/librustdoc/html/format.rs | 6 ++++-- src/librustdoc/html/render/context.rs | 4 ++-- src/librustdoc/html/render/search_index.rs | 10 ---------- src/librustdoc/json/mod.rs | 3 +-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index f0e6d4315648e..861b70bac6c4f 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -38,7 +38,6 @@ use crate::clean::Clean; use crate::core::DocContext; use crate::formats::cache::Cache; use crate::formats::item_type::ItemType; -use crate::html::render::search_index::ExternalLocation; use crate::html::render::Context; crate use self::FnRetTy::*; @@ -337,6 +336,16 @@ impl ExternalCrate { } } +/// Indicates where an external crate can be found. +crate enum ExternalLocation { + /// Remote URL root of the external crate + Remote(String), + /// This external crate can be found in the local doc/ folder + Local, + /// The external crate could not be found. + Unknown, +} + /// Anything with a source location and set of attributes and, optionally, a /// name. That is, anything that can be documented. This doesn't correspond /// directly to the AST's concept of an item; it's a strict superset. diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 528d48a8d059e..4dbf3b64a3d18 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -6,13 +6,13 @@ use rustc_middle::middle::privacy::AccessLevels; use rustc_middle::ty::TyCtxt; use rustc_span::symbol::sym; -use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType}; +use crate::clean::{self, types::ExternalLocation, ExternalCrate, ItemId, PrimitiveType}; use crate::core::DocContext; use crate::fold::DocFolder; use crate::formats::item_type::ItemType; use crate::formats::Impl; use crate::html::markdown::short_markdown_summary; -use crate::html::render::search_index::{get_index_search_type, ExternalLocation}; +use crate::html::render::search_index::get_index_search_type; use crate::html::render::IndexItem; /// This cache is used to store information about the [`clean::Crate`] being diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index f7396f758b103..4088163df3918 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -21,10 +21,12 @@ use rustc_middle::ty::TyCtxt; use rustc_span::def_id::CRATE_DEF_INDEX; use rustc_target::spec::abi::Abi; -use crate::clean::{self, utils::find_nearest_parent_module, ExternalCrate, ItemId, PrimitiveType}; +use crate::clean::{ + self, types::ExternalLocation, utils::find_nearest_parent_module, ExternalCrate, ItemId, + PrimitiveType, +}; use crate::formats::item_type::ItemType; use crate::html::escape::Escape; -use crate::html::render::search_index::ExternalLocation; use crate::html::render::Context; use super::url_parts_builder::UrlPartsBuilder; diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 6729129027f55..534a542d58ed0 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -14,7 +14,7 @@ use rustc_span::source_map::FileName; use rustc_span::symbol::sym; use super::print_item::{full_path, item_path, print_item}; -use super::search_index::{build_index, ExternalLocation}; +use super::search_index::build_index; use super::templates; use super::write_shared::write_shared; use super::{ @@ -22,7 +22,7 @@ use super::{ BASIC_KEYWORDS, }; -use crate::clean::{self, ExternalCrate}; +use crate::clean::{self, types::ExternalLocation, ExternalCrate}; use crate::config::RenderOptions; use crate::docfs::{DocFS, PathError}; use crate::error::Error; diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index 5d4df3ee5ff3a..787f047790aeb 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -13,16 +13,6 @@ use crate::formats::item_type::ItemType; use crate::html::markdown::short_markdown_summary; use crate::html::render::{IndexItem, IndexItemFunctionType, RenderType, TypeWithKind}; -/// Indicates where an external crate can be found. -crate enum ExternalLocation { - /// Remote URL root of the external crate - Remote(String), - /// This external crate can be found in the local doc/ folder - Local, - /// The external crate could not be found. - Unknown, -} - /// Builds the search index from the collected metadata crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<'tcx>) -> String { let mut defid_to_pathid = FxHashMap::default(); diff --git a/src/librustdoc/json/mod.rs b/src/librustdoc/json/mod.rs index 01641bae3845a..005da01b52b6c 100644 --- a/src/librustdoc/json/mod.rs +++ b/src/librustdoc/json/mod.rs @@ -19,12 +19,11 @@ use rustc_session::Session; use rustdoc_json_types as types; use crate::clean; -use crate::clean::ExternalCrate; +use crate::clean::types::{ExternalCrate, ExternalLocation}; use crate::config::RenderOptions; use crate::error::Error; use crate::formats::cache::Cache; use crate::formats::FormatRenderer; -use crate::html::render::search_index::ExternalLocation; use crate::json::conversions::{from_item_id, IntoWithTcx}; #[derive(Clone)] From bd6692c50dd97a5679dbe0c8f23cc1b844c4cefe Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Mon, 27 Dec 2021 18:59:27 -0800 Subject: [PATCH 4/8] Make `search_index` functions private where possible Now the only two crate-public items are `build_index` and `get_index_search_type` (because for some reason the latter is also used in `formats::cache`). --- src/librustdoc/html/render/search_index.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index 787f047790aeb..e03f96aeb4714 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -237,7 +237,7 @@ fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option /// /// Important note: It goes through generics recursively. So if you have /// `T: Option>`, it'll go into `Option` and then into `Result`. -crate fn get_real_types<'tcx>( +fn get_real_types<'tcx>( generics: &Generics, arg: &Type, tcx: TyCtxt<'tcx>, @@ -377,7 +377,7 @@ crate fn get_real_types<'tcx>( /// /// i.e. `fn foo>(x: u32, y: B)` will return /// `[u32, Display, Option]`. -crate fn get_all_types<'tcx>( +fn get_all_types<'tcx>( generics: &Generics, decl: &FnDecl, tcx: TyCtxt<'tcx>, From afb77a959a45571ea6180053a63a25c752995380 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Mon, 27 Dec 2021 19:16:33 -0800 Subject: [PATCH 5/8] Coalesce two arguments as `&Function` --- src/librustdoc/html/render/search_index.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index e03f96aeb4714..90e73f4a76d87 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -7,7 +7,7 @@ use rustc_span::symbol::Symbol; use serde::ser::{Serialize, SerializeStruct, Serializer}; use crate::clean; -use crate::clean::types::{FnDecl, FnRetTy, GenericBound, Generics, Type, WherePredicate}; +use crate::clean::types::{FnRetTy, Function, GenericBound, Generics, Type, WherePredicate}; use crate::formats::cache::Cache; use crate::formats::item_type::ItemType; use crate::html::markdown::short_markdown_summary; @@ -186,9 +186,9 @@ crate fn get_index_search_type<'tcx>( tcx: TyCtxt<'tcx>, ) -> Option { let (mut inputs, mut output) = match *item.kind { - clean::FunctionItem(ref f) => get_all_types(&f.generics, &f.decl, tcx), - clean::MethodItem(ref m, _) => get_all_types(&m.generics, &m.decl, tcx), - clean::TyMethodItem(ref m) => get_all_types(&m.generics, &m.decl, tcx), + clean::FunctionItem(ref f) => get_all_types(f, tcx), + clean::MethodItem(ref m, _) => get_all_types(m, tcx), + clean::TyMethodItem(ref m) => get_all_types(m, tcx), _ => return None, }; @@ -378,10 +378,12 @@ fn get_real_types<'tcx>( /// i.e. `fn foo>(x: u32, y: B)` will return /// `[u32, Display, Option]`. fn get_all_types<'tcx>( - generics: &Generics, - decl: &FnDecl, + func: &Function, tcx: TyCtxt<'tcx>, ) -> (Vec, Vec) { + let decl = &func.decl; + let generics = &func.generics; + let mut all_types = Vec::new(); for arg in decl.inputs.values.iter() { if arg.type_.is_self_type() { From 5c8e8e565d854f72691031262fcf945bcf1f24f9 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Mon, 27 Dec 2021 19:19:56 -0800 Subject: [PATCH 6/8] Give clearer names to several search index functions --- src/librustdoc/formats/cache.rs | 4 +-- src/librustdoc/html/render/search_index.rs | 36 ++++++++++++++-------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index 4dbf3b64a3d18..6b9ccd37cfb37 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -12,7 +12,7 @@ use crate::fold::DocFolder; use crate::formats::item_type::ItemType; use crate::formats::Impl; use crate::html::markdown::short_markdown_summary; -use crate::html::render::search_index::get_index_search_type; +use crate::html::render::search_index::get_function_type_for_search; use crate::html::render::IndexItem; /// This cache is used to store information about the [`clean::Crate`] being @@ -303,7 +303,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { desc, parent, parent_idx: None, - search_type: get_index_search_type(&item, self.tcx), + search_type: get_function_type_for_search(&item, self.tcx), aliases: item.attrs.get_doc_aliases(), }); } diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index 90e73f4a76d87..9543afc68a400 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -32,7 +32,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt< desc, parent: Some(did), parent_idx: None, - search_type: get_index_search_type(item, tcx), + search_type: get_function_type_for_search(item, tcx), aliases: item.attrs.get_doc_aliases(), }); } @@ -181,14 +181,14 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt< ) } -crate fn get_index_search_type<'tcx>( +crate fn get_function_type_for_search<'tcx>( item: &clean::Item, tcx: TyCtxt<'tcx>, ) -> Option { let (mut inputs, mut output) = match *item.kind { - clean::FunctionItem(ref f) => get_all_types(f, tcx), - clean::MethodItem(ref m, _) => get_all_types(m, tcx), - clean::TyMethodItem(ref m) => get_all_types(m, tcx), + clean::FunctionItem(ref f) => get_fn_inputs_and_outputs(f, tcx), + clean::MethodItem(ref m, _) => get_fn_inputs_and_outputs(m, tcx), + clean::TyMethodItem(ref m) => get_fn_inputs_and_outputs(m, tcx), _ => return None, }; @@ -237,7 +237,7 @@ fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option /// /// Important note: It goes through generics recursively. So if you have /// `T: Option>`, it'll go into `Option` and then into `Result`. -fn get_real_types<'tcx>( +fn add_generics_and_bounds_as_types<'tcx>( generics: &Generics, arg: &Type, tcx: TyCtxt<'tcx>, @@ -337,7 +337,13 @@ fn get_real_types<'tcx>( for param_def in poly_trait.generic_params.iter() { match ¶m_def.kind { clean::GenericParamDefKind::Type { default: Some(ty), .. } => { - get_real_types(generics, ty, tcx, recurse + 1, &mut ty_generics) + add_generics_and_bounds_as_types( + generics, + ty, + tcx, + recurse + 1, + &mut ty_generics, + ) } _ => {} } @@ -352,7 +358,13 @@ fn get_real_types<'tcx>( for bound in bound.get_bounds().unwrap_or(&[]) { if let Some(path) = bound.get_trait_path() { let ty = Type::Path { path }; - get_real_types(generics, &ty, tcx, recurse + 1, &mut ty_generics); + add_generics_and_bounds_as_types( + generics, + &ty, + tcx, + recurse + 1, + &mut ty_generics, + ); } } insert_ty(res, tcx, arg.clone(), ty_generics); @@ -366,7 +378,7 @@ fn get_real_types<'tcx>( let mut ty_generics = Vec::new(); if let Some(arg_generics) = arg.generics() { for gen in arg_generics.iter() { - get_real_types(generics, gen, tcx, recurse + 1, &mut ty_generics); + add_generics_and_bounds_as_types(generics, gen, tcx, recurse + 1, &mut ty_generics); } } insert_ty(res, tcx, arg.clone(), ty_generics); @@ -377,7 +389,7 @@ fn get_real_types<'tcx>( /// /// i.e. `fn foo>(x: u32, y: B)` will return /// `[u32, Display, Option]`. -fn get_all_types<'tcx>( +fn get_fn_inputs_and_outputs<'tcx>( func: &Function, tcx: TyCtxt<'tcx>, ) -> (Vec, Vec) { @@ -390,7 +402,7 @@ fn get_all_types<'tcx>( continue; } let mut args = Vec::new(); - get_real_types(generics, &arg.type_, tcx, 0, &mut args); + add_generics_and_bounds_as_types(generics, &arg.type_, tcx, 0, &mut args); if !args.is_empty() { all_types.extend(args); } else { @@ -404,7 +416,7 @@ fn get_all_types<'tcx>( let mut ret_types = Vec::new(); match decl.output { FnRetTy::Return(ref return_type) => { - get_real_types(generics, return_type, tcx, 0, &mut ret_types); + add_generics_and_bounds_as_types(generics, return_type, tcx, 0, &mut ret_types); if ret_types.is_empty() { if let Some(kind) = return_type.def_id_no_primitives().map(|did| tcx.def_kind(did).into()) From 7d1ec648914d59a6620ed0dcc2ed60c86b53f189 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Mon, 27 Dec 2021 19:28:05 -0800 Subject: [PATCH 7/8] Remove unused parameter --- src/librustdoc/html/render/search_index.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index 9543afc68a400..038ee351545f6 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -200,12 +200,12 @@ crate fn get_function_type_for_search<'tcx>( fn get_index_type(clean_type: &clean::Type, generics: Vec) -> RenderType { RenderType { - name: get_index_type_name(clean_type, true).map(|s| s.as_str().to_ascii_lowercase()), + name: get_index_type_name(clean_type).map(|s| s.as_str().to_ascii_lowercase()), generics: if generics.is_empty() { None } else { Some(generics) }, } } -fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option { +fn get_index_type_name(clean_type: &clean::Type) -> Option { match *clean_type { clean::Type::Path { ref path, .. } => { let path_segment = path.segments.last().unwrap(); @@ -215,11 +215,10 @@ fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option let path = &bounds[0].trait_; Some(path.segments.last().unwrap().name) } - clean::Generic(s) if accept_generic => Some(s), + clean::Generic(s) => Some(s), clean::Primitive(ref p) => Some(p.as_sym()), - clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_, accept_generic), - clean::Generic(_) - | clean::BareFunction(_) + clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_), + clean::BareFunction(_) | clean::Tuple(_) | clean::Slice(_) | clean::Array(_, _) From 908a9d4aad61f4b8108ff91ae42f76e67dec5148 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Tue, 28 Dec 2021 13:15:02 -0800 Subject: [PATCH 8/8] Add regression test for #59502 This issue was fixed using a hacky recursion "fuel" argument, but the issue was never minimized nor was a regression test added. The underlying bug is still unfixed, so this test should help with fixing it and removing the `recurse` hack. --- src/librustdoc/html/render/search_index.rs | 2 ++ ...search-index-generics-recursion-bug-issue-59502.rs | 11 +++++++++++ 2 files changed, 13 insertions(+) create mode 100644 src/test/rustdoc-ui/search-index-generics-recursion-bug-issue-59502.rs diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index 038ee351545f6..0fbe090f2190a 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -236,6 +236,7 @@ fn get_index_type_name(clean_type: &clean::Type) -> Option { /// /// Important note: It goes through generics recursively. So if you have /// `T: Option>`, it'll go into `Option` and then into `Result`. +#[instrument(level = "trace", skip(tcx, res))] fn add_generics_and_bounds_as_types<'tcx>( generics: &Generics, arg: &Type, @@ -316,6 +317,7 @@ fn add_generics_and_bounds_as_types<'tcx>( if recurse >= 10 { // FIXME: remove this whole recurse thing when the recursion bug is fixed + // See #59502 for the original issue. return; } diff --git a/src/test/rustdoc-ui/search-index-generics-recursion-bug-issue-59502.rs b/src/test/rustdoc-ui/search-index-generics-recursion-bug-issue-59502.rs new file mode 100644 index 0000000000000..ce51556dd418b --- /dev/null +++ b/src/test/rustdoc-ui/search-index-generics-recursion-bug-issue-59502.rs @@ -0,0 +1,11 @@ +// check-pass + +// Minimization of issue #59502 + +trait MyTrait { + type Output; +} + +pub fn pow>(arg: T) -> T { + arg +}