Skip to content

rustdoc: Start cleaning up search index generation #92340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
}
Expand Down Expand Up @@ -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<IndexItemFunctionType> {
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,
};

Expand Down Expand Up @@ -254,14 +253,12 @@ crate fn get_real_types<'tcx>(
tcx: TyCtxt<'tcx>,
recurse: usize,
res: &mut Vec<TypeWithKind>,
cache: &Cache,
) {
fn insert_ty(
res: &mut Vec<TypeWithKind>,
tcx: TyCtxt<'_>,
ty: Type,
mut generics: Vec<TypeWithKind>,
_cache: &Cache,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the right move because of:

} else if let Some(kind) = ty.def_id_no_primitives().map(|did| tcx.def_kind(did).into()) {

We are replacing def_id_no_primitives calls, so we need to keep the Cache.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I see what you mean, but the Cache can easily be re-added later. As of now, passing around the Cache is just dead code that might be sitting there for a while.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed a PR doing exactly that today (but of course, can't find it again...). I'd prefer to avoid having removals/adds following but not sure it matters much since it's just about git history here...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're thinking of #92342? I'd rather remove the dead code for now – e.g., that PR may not work and end up being closed, and then the dead code will still be there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fully agree with you but it's fine. Let's move forward then!

) {
let is_full_generic = ty.is_full_generic();

Expand Down Expand Up @@ -350,32 +347,25 @@ crate fn get_real_types<'tcx>(
for param_def in poly_trait.generic_params.iter() {
match &param_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<u32>`...
if let Some(bound) = generics.params.iter().find(|g| g.is_type() && g.name == arg_s) {
let mut ty_generics = Vec::new();
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<T>`, and we're
Expand All @@ -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);
}
}

Expand All @@ -401,15 +391,14 @@ crate fn get_all_types<'tcx>(
generics: &Generics,
decl: &FnDecl,
tcx: TyCtxt<'tcx>,
cache: &Cache,
) -> (Vec<TypeWithKind>, Vec<TypeWithKind>) {
let mut all_types = Vec::new();
for arg in decl.inputs.values.iter() {
if arg.type_.is_self_type() {
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 {
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down