Skip to content

Commit d4a700a

Browse files
Rollup merge of #149452 - yotamofek:pr/rustdoc/IndexItem-new, r=notriddle,lolbinarycat
Refactor out common code into a `IndexItem::new` constructor #149404
2 parents ec6f9a5 + ab1500b commit d4a700a

3 files changed

Lines changed: 64 additions & 57 deletions

File tree

src/librustdoc/formats/cache.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ use crate::core::DocContext;
1515
use crate::fold::DocFolder;
1616
use crate::formats::Impl;
1717
use crate::formats::item_type::ItemType;
18-
use crate::html::markdown::short_markdown_summary;
19-
use crate::html::render::IndexItem;
20-
use crate::html::render::search_index::get_function_type_for_search;
18+
use crate::html::render::{IndexItem, IndexItemInfo};
2119
use crate::visit_lib::RustdocEffectiveVisibilities;
2220

2321
/// This cache is used to store information about the [`clean::Crate`] being
@@ -580,7 +578,6 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
580578

581579
debug_assert!(!item.is_stripped());
582580

583-
let desc = short_markdown_summary(&item.doc_value(), &item.link_names(cache));
584581
// For searching purposes, a re-export is a duplicate if:
585582
//
586583
// - It's either an inline, or a true re-export
@@ -591,32 +588,24 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
591588
_ => item_def_id,
592589
};
593590
let (impl_id, trait_parent) = cache.parent_stack_last_impl_and_trait_id();
594-
let search_type = get_function_type_for_search(
595-
item,
591+
let info = IndexItemInfo::new(
596592
tcx,
597-
clean_impl_generics(cache.parent_stack.last()).as_ref(),
598-
parent_did,
599593
cache,
594+
item,
595+
parent_did,
596+
clean_impl_generics(cache.parent_stack.last()).as_ref(),
600597
);
601-
let aliases = item.attrs.get_doc_aliases();
602-
let is_deprecated = item.is_deprecated(tcx);
603-
let is_unstable = item.is_unstable();
604598
let index_item = IndexItem {
605-
ty: item.type_(),
606599
defid: Some(defid),
607600
name,
608601
module_path: parent_path.to_vec(),
609-
desc,
610602
parent: parent_did,
611603
parent_idx: None,
612604
trait_parent,
613605
trait_parent_idx: None,
614606
exact_module_path: None,
615607
impl_id,
616-
search_type,
617-
aliases,
618-
is_deprecated,
619-
is_unstable,
608+
info,
620609
};
621610

622611
cache.search_index.push(index_item);

src/librustdoc/html/render/mod.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use tracing::{debug, info};
6666
pub(crate) use self::context::*;
6767
pub(crate) use self::span_map::{LinkFromSrc, collect_spans_and_sources};
6868
pub(crate) use self::write_shared::*;
69-
use crate::clean::{self, Defaultness, ItemId, RenderedLink};
69+
use crate::clean::{self, Defaultness, Item, ItemId, RenderedLink};
7070
use crate::display::{Joined as _, MaybeDisplay as _};
7171
use crate::error::Error;
7272
use crate::formats::Impl;
@@ -79,8 +79,9 @@ use crate::html::format::{
7979
print_type, print_where_clause, visibility_print_with_space,
8080
};
8181
use crate::html::markdown::{
82-
HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine,
82+
HeadingOffset, IdMap, Markdown, MarkdownItemInfo, MarkdownSummaryLine, short_markdown_summary,
8383
};
84+
use crate::html::render::search_index::get_function_type_for_search;
8485
use crate::html::static_files::SCRAPE_EXAMPLES_HELP_MD;
8586
use crate::html::{highlight, sources};
8687
use crate::scrape_examples::{CallData, CallLocation};
@@ -124,25 +125,48 @@ enum RenderMode {
124125
// Helper structs for rendering items/sidebars and carrying along contextual
125126
// information
126127

128+
#[derive(Debug)]
129+
pub(crate) struct IndexItemInfo {
130+
pub(crate) ty: ItemType,
131+
pub(crate) desc: String,
132+
pub(crate) search_type: Option<IndexItemFunctionType>,
133+
pub(crate) aliases: Box<[Symbol]>,
134+
pub(crate) deprecation: Option<Deprecation>,
135+
pub(crate) is_unstable: bool,
136+
}
137+
138+
impl IndexItemInfo {
139+
pub(crate) fn new(
140+
tcx: TyCtxt<'_>,
141+
cache: &Cache,
142+
item: &Item,
143+
parent_did: Option<DefId>,
144+
impl_generics: Option<&(clean::Type, clean::Generics)>,
145+
) -> Self {
146+
let ty = item.type_();
147+
let desc = short_markdown_summary(&item.doc_value(), &item.link_names(cache));
148+
let search_type = get_function_type_for_search(item, tcx, impl_generics, parent_did, cache);
149+
let aliases = item.attrs.get_doc_aliases();
150+
let deprecation = item.deprecation(tcx);
151+
let is_unstable = item.is_unstable();
152+
Self { ty, desc, search_type, aliases, deprecation, is_unstable }
153+
}
154+
}
155+
127156
/// Struct representing one entry in the JS search index. These are all emitted
128157
/// by hand to a large JS file at the end of cache-creation.
129158
#[derive(Debug)]
130159
pub(crate) struct IndexItem {
131-
pub(crate) ty: ItemType,
132160
pub(crate) defid: Option<DefId>,
133161
pub(crate) name: Symbol,
134162
pub(crate) module_path: Vec<Symbol>,
135-
pub(crate) desc: String,
136163
pub(crate) parent: Option<DefId>,
137164
pub(crate) parent_idx: Option<usize>,
138165
pub(crate) trait_parent: Option<DefId>,
139166
pub(crate) trait_parent_idx: Option<usize>,
140167
pub(crate) exact_module_path: Option<Vec<Symbol>>,
141168
pub(crate) impl_id: Option<DefId>,
142-
pub(crate) search_type: Option<IndexItemFunctionType>,
143-
pub(crate) aliases: Box<[Symbol]>,
144-
pub(crate) is_deprecated: bool,
145-
pub(crate) is_unstable: bool,
169+
pub(crate) info: IndexItemInfo,
146170
}
147171

148172
/// A type used for the search index.

src/librustdoc/html/render/search_index.rs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ::serde::{Deserialize, Serialize};
1313
use rustc_ast::join_path_syms;
1414
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
1515
use rustc_data_structures::thin_vec::ThinVec;
16-
use rustc_hir::def_id::LOCAL_CRATE;
16+
use rustc_hir::def_id::{CrateNum, DefIndex, LOCAL_CRATE};
1717
use rustc_hir::find_attr;
1818
use rustc_middle::ty::TyCtxt;
1919
use rustc_span::def_id::DefId;
@@ -29,7 +29,9 @@ use crate::error::Error;
2929
use crate::formats::cache::{Cache, OrphanImplItem};
3030
use crate::formats::item_type::ItemType;
3131
use crate::html::markdown::short_markdown_summary;
32-
use crate::html::render::{self, IndexItem, IndexItemFunctionType, RenderType, RenderTypeId};
32+
use crate::html::render::{
33+
self, IndexItem, IndexItemFunctionType, IndexItemInfo, RenderType, RenderTypeId,
34+
};
3335

3436
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
3537
pub(crate) struct SerializedSearchIndex {
@@ -1270,29 +1272,18 @@ pub(crate) fn build_index(
12701272
&cache.orphan_impl_items
12711273
{
12721274
if let Some((fqp, _)) = cache.paths.get(&parent) {
1273-
let desc = short_markdown_summary(&item.doc_value(), &item.link_names(cache));
1275+
let info = IndexItemInfo::new(tcx, cache, item, Some(parent), impl_generics.as_ref());
12741276
search_index.push(IndexItem {
1275-
ty: item.type_(),
12761277
defid: item.item_id.as_def_id(),
12771278
name: item.name.unwrap(),
12781279
module_path: fqp[..fqp.len() - 1].to_vec(),
1279-
desc,
12801280
parent: Some(parent),
12811281
parent_idx: None,
12821282
trait_parent,
12831283
trait_parent_idx: None,
12841284
exact_module_path: None,
12851285
impl_id,
1286-
search_type: get_function_type_for_search(
1287-
item,
1288-
tcx,
1289-
impl_generics.as_ref(),
1290-
Some(parent),
1291-
cache,
1292-
),
1293-
aliases: item.attrs.get_doc_aliases(),
1294-
is_deprecated: item.is_deprecated(tcx),
1295-
is_unstable: item.is_unstable(),
1286+
info,
12961287
});
12971288
}
12981289
}
@@ -1301,11 +1292,10 @@ pub(crate) fn build_index(
13011292
search_index.sort_unstable_by(|k1, k2| {
13021293
// `sort_unstable_by_key` produces lifetime errors
13031294
// HACK(rustdoc): should not be sorting `CrateNum` or `DefIndex`, this will soon go away, too
1304-
let k1 =
1305-
(&k1.module_path, k1.name.as_str(), &k1.ty, k1.parent.map(|id| (id.index, id.krate)));
1306-
let k2 =
1307-
(&k2.module_path, k2.name.as_str(), &k2.ty, k2.parent.map(|id| (id.index, id.krate)));
1308-
Ord::cmp(&k1, &k2)
1295+
fn key(i: &IndexItem) -> (&[Symbol], &str, ItemType, Option<(DefIndex, CrateNum)>) {
1296+
(&i.module_path, i.name.as_str(), i.info.ty, i.parent.map(|id| (id.index, id.krate)))
1297+
}
1298+
Ord::cmp(&key(k1), &key(k2))
13091299
});
13101300

13111301
// Now, convert to an on-disk search index format
@@ -1466,7 +1456,7 @@ pub(crate) fn build_index(
14661456
if fqp.last() != Some(&item.name) {
14671457
return None;
14681458
}
1469-
let path = if item.ty == ItemType::Macro
1459+
let path = if item.info.ty == ItemType::Macro
14701460
&& find_attr!(tcx, defid, MacroExport { .. })
14711461
{
14721462
// `#[macro_export]` always exports to the crate root.
@@ -1501,8 +1491,9 @@ pub(crate) fn build_index(
15011491
if item.impl_id.is_some()
15021492
&& let Some(parent_idx) = item.parent_idx
15031493
{
1504-
let count =
1505-
associated_item_duplicates.entry((parent_idx, item.ty, item.name)).or_insert(0);
1494+
let count = associated_item_duplicates
1495+
.entry((parent_idx, item.info.ty, item.name))
1496+
.or_insert(0);
15061497
*count += 1;
15071498
}
15081499
}
@@ -1525,24 +1516,27 @@ pub(crate) fn build_index(
15251516
let new_entry_id = serialized_index.add_entry(
15261517
item.name,
15271518
EntryData {
1528-
ty: item.ty,
1519+
ty: item.info.ty,
15291520
parent: item.parent_idx,
15301521
trait_parent: item.trait_parent_idx,
15311522
module_path,
15321523
exact_module_path,
1533-
deprecated: item.is_deprecated,
1534-
unstable: item.is_unstable,
1524+
deprecated: item
1525+
.info
1526+
.deprecation
1527+
.is_some_and(|deprecation| deprecation.is_in_effect()),
1528+
unstable: item.info.is_unstable,
15351529
associated_item_disambiguator_or_extern_crate_url: if let Some(impl_id) =
15361530
item.impl_id
15371531
&& let Some(parent_idx) = item.parent_idx
15381532
&& associated_item_duplicates
1539-
.get(&(parent_idx, item.ty, item.name))
1533+
.get(&(parent_idx, item.info.ty, item.name))
15401534
.copied()
15411535
.unwrap_or(0)
15421536
> 1
15431537
{
15441538
Some(render::get_id_for_impl(tcx, ItemId::DefId(impl_id)))
1545-
} else if item.ty == ItemType::ExternCrate
1539+
} else if item.info.ty == ItemType::ExternCrate
15461540
&& let Some(local_def_id) = item.defid.and_then(|def_id| def_id.as_local())
15471541
&& let cnum = tcx.extern_mod_stmt_cnum(local_def_id).unwrap_or(LOCAL_CRATE)
15481542
&& let Some(ExternalLocation::Remote { url, is_absolute }) =
@@ -1555,12 +1549,12 @@ pub(crate) fn build_index(
15551549
},
15561550
krate: crate_idx,
15571551
},
1558-
item.desc.to_string(),
1552+
item.info.desc.to_string(),
15591553
);
15601554

15611555
// Aliases
15621556
// -------
1563-
for alias in &item.aliases[..] {
1557+
for alias in &item.info.aliases {
15641558
serialized_index.push_alias(alias.as_str().to_string(), new_entry_id);
15651559
}
15661560

@@ -1833,7 +1827,7 @@ pub(crate) fn build_index(
18331827
_ => {}
18341828
}
18351829
}
1836-
if let Some(search_type) = &mut item.search_type {
1830+
if let Some(search_type) = &mut item.info.search_type {
18371831
let mut used_in_function_inputs = BTreeSet::new();
18381832
let mut used_in_function_output = BTreeSet::new();
18391833
for item in &mut search_type.inputs {
@@ -1900,7 +1894,7 @@ pub(crate) fn build_index(
19001894
// The number 8 is arbitrary. We want it big, but not enormous,
19011895
// because the postings list has to fill in an empty array for each
19021896
// unoccupied size.
1903-
if item.ty.is_fn_like() { 0 } else { 16 };
1897+
if item.info.ty.is_fn_like() { 0 } else { 16 };
19041898
serialized_index.function_data[new_entry_id] = Some(search_type.clone());
19051899

19061900
#[derive(Clone, Copy)]

0 commit comments

Comments
 (0)