Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

If a trait item appears in rustdoc search, hide the corrosponding impl items #145898

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

Open
lolbinarycat wants to merge 3 commits into rust-lang:master
base: master
Choose a base branch
Loading
from lolbinarycat:rustdoc-search-trait-parent
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 20 additions & 11 deletions src/librustdoc/formats/cache.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ impl Cache {
Cache { document_private, document_hidden, ..Cache::default() }
}

fn parent_stack_last_impl_and_trait_id(&self) -> (Option<DefId>, Option<DefId>) {
if let Some(ParentStackItem::Impl { item_id, trait_, .. }) = self.parent_stack.last() {
(item_id.as_def_id(), trait_.as_ref().map(|tr| tr.def_id()))
} else {
(None, None)
}
}

/// Populates the `Cache` with more data. The returned `Crate` will be missing some data that was
/// in `krate` due to the data being moved into the `Cache`.
pub(crate) fn populate(cx: &mut DocContext<'_>, mut krate: clean::Crate) -> clean::Crate {
Expand Down Expand Up @@ -574,11 +582,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
clean::ItemKind::ImportItem(import) => import.source.did.unwrap_or(item_def_id),
_ => item_def_id,
};
let impl_id = if let Some(ParentStackItem::Impl { item_id, .. }) = cache.parent_stack.last() {
item_id.as_def_id()
} else {
None
};
let (impl_id, trait_parent) = cache.parent_stack_last_impl_and_trait_id();
let search_type = get_function_type_for_search(
item,
tcx,
Expand All @@ -596,12 +600,15 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
desc,
parent: parent_did,
parent_idx: None,
trait_parent,
trait_parent_idx: None,
exact_module_path: None,
impl_id,
search_type,
aliases,
deprecation,
};

cache.search_index.push(index_item);
}

Expand All @@ -610,19 +617,21 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
/// See [`Cache::orphan_impl_items`].
fn handle_orphan_impl_child(cache: &mut Cache, item: &clean::Item, parent_did: DefId) {
let impl_generics = clean_impl_generics(cache.parent_stack.last());
let impl_id = if let Some(ParentStackItem::Impl { item_id, .. }) = cache.parent_stack.last() {
item_id.as_def_id()
} else {
None
let (impl_id, trait_parent) = cache.parent_stack_last_impl_and_trait_id();
let orphan_item = OrphanImplItem {
parent: parent_did,
trait_parent,
item: item.clone(),
impl_generics,
impl_id,
};
let orphan_item =
OrphanImplItem { parent: parent_did, item: item.clone(), impl_generics, impl_id };
cache.orphan_impl_items.push(orphan_item);
}

pub(crate) struct OrphanImplItem {
pub(crate) parent: DefId,
pub(crate) impl_id: Option<DefId>,
pub(crate) trait_parent: Option<DefId>,
pub(crate) item: clean::Item,
pub(crate) impl_generics: Option<(clean::Type, clean::Generics)>,
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustdoc/html/render/mod.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ pub(crate) struct IndexItem {
pub(crate) desc: String,
pub(crate) parent: Option<DefId>,
pub(crate) parent_idx: Option<usize>,
pub(crate) trait_parent: Option<DefId>,
pub(crate) trait_parent_idx: Option<usize>,
pub(crate) exact_module_path: Option<Vec<Symbol>>,
pub(crate) impl_id: Option<DefId>,
pub(crate) search_type: Option<IndexItemFunctionType>,
Expand Down
78 changes: 49 additions & 29 deletions src/librustdoc/html/render/search_index.rs
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ impl SerializedSearchIndex {
module_path,
exact_module_path,
parent,
trait_parent,
deprecated,
associated_item_disambiguator,
}| EntryData {
Expand All @@ -589,6 +590,7 @@ impl SerializedSearchIndex {
exact_module_path: exact_module_path
.and_then(|path_id| map.get(&path_id).copied()),
parent: parent.and_then(|path_id| map.get(&path_id).copied()),
trait_parent: trait_parent.and_then(|path_id| map.get(&path_id).copied()),
deprecated: *deprecated,
associated_item_disambiguator: associated_item_disambiguator.clone(),
},
Expand Down Expand Up @@ -870,6 +872,7 @@ struct EntryData {
module_path: Option<usize>,
exact_module_path: Option<usize>,
parent: Option<usize>,
trait_parent: Option<usize>,
deprecated: bool,
associated_item_disambiguator: Option<String>,
}
Expand All @@ -885,6 +888,7 @@ impl Serialize for EntryData {
seq.serialize_element(&self.module_path.map(|id| id + 1).unwrap_or(0))?;
seq.serialize_element(&self.exact_module_path.map(|id| id + 1).unwrap_or(0))?;
seq.serialize_element(&self.parent.map(|id| id + 1).unwrap_or(0))?;
seq.serialize_element(&self.trait_parent.map(|id| id + 1).unwrap_or(0))?;
seq.serialize_element(&if self.deprecated { 1 } else { 0 })?;
if let Some(disambig) = &self.associated_item_disambiguator {
seq.serialize_element(&disambig)?;
Expand Down Expand Up @@ -916,6 +920,9 @@ impl<'de> Deserialize<'de> for EntryData {
.ok_or_else(|| A::Error::missing_field("exact_module_path"))?;
let parent: SerializedOptional32 =
v.next_element()?.ok_or_else(|| A::Error::missing_field("parent"))?;
let trait_parent: SerializedOptional32 =
v.next_element()?.ok_or_else(|| A::Error::missing_field("trait_parent"))?;

let deprecated: u32 = v.next_element()?.unwrap_or(0);
let associated_item_disambiguator: Option<String> = v.next_element()?;
Ok(EntryData {
Expand All @@ -925,6 +932,7 @@ impl<'de> Deserialize<'de> for EntryData {
exact_module_path: Option::<i32>::from(exact_module_path)
.map(|path| path as usize),
parent: Option::<i32>::from(parent).map(|path| path as usize),
trait_parent: Option::<i32>::from(trait_parent).map(|path| path as usize),
deprecated: deprecated != 0,
associated_item_disambiguator,
})
Expand Down Expand Up @@ -1275,7 +1283,8 @@ pub(crate) fn build_index(

// Attach all orphan items to the type's definition if the type
// has since been learned.
for &OrphanImplItem { impl_id, parent, ref item, ref impl_generics } in &cache.orphan_impl_items
for &OrphanImplItem { impl_id, parent, trait_parent, ref item, ref impl_generics } in
&cache.orphan_impl_items
{
if let Some((fqp, _)) = cache.paths.get(&parent) {
let desc = short_markdown_summary(&item.doc_value(), &item.link_names(cache));
Expand All @@ -1287,6 +1296,8 @@ pub(crate) fn build_index(
desc,
parent: Some(parent),
parent_idx: None,
trait_parent,
trait_parent_idx: None,
exact_module_path: None,
impl_id,
search_type: get_function_type_for_search(
Expand Down Expand Up @@ -1391,6 +1402,7 @@ pub(crate) fn build_index(
module_path: None,
exact_module_path: None,
parent: None,
trait_parent: None,
deprecated: false,
associated_item_disambiguator: None,
}),
Expand All @@ -1404,39 +1416,46 @@ pub(crate) fn build_index(
}
};

// First, populate associated item parents
// First, populate associated item parents and trait parents
let crate_items: Vec<&mut IndexItem> = search_index
.iter_mut()
.map(|item| {
item.parent_idx = item.parent.and_then(|defid| {
cache.paths.get(&defid).map(|&(ref fqp, ty)| {
let pathid = serialized_index.names.len();
match serialized_index.crate_paths_index.entry((ty, fqp.clone())) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
entry.insert(pathid);
let (name, path) = fqp.split_last().unwrap();
serialized_index.push_path(
name.as_str().to_string(),
PathData {
ty,
module_path: path.to_vec(),
exact_module_path: if let Some(exact_path) =
cache.exact_paths.get(&defid)
&& let Some((name2, exact_path)) = exact_path.split_last()
&& name == name2
{
Some(exact_path.to_vec())
} else {
None
let mut defid_to_rowid = |defid, check_external: bool| {
cache
.paths
.get(&defid)
.or_else(|| check_external.then(|| cache.external_paths.get(&defid)).flatten())
.map(|&(ref fqp, ty)| {
let pathid = serialized_index.names.len();
match serialized_index.crate_paths_index.entry((ty, fqp.clone())) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
entry.insert(pathid);
let (name, path) = fqp.split_last().unwrap();
serialized_index.push_path(
name.as_str().to_string(),
PathData {
ty,
module_path: path.to_vec(),
exact_module_path: if let Some(exact_path) =
cache.exact_paths.get(&defid)
&& let Some((name2, exact_path)) =
exact_path.split_last()
&& name == name2
{
Some(exact_path.to_vec())
} else {
None
},
},
},
);
usize::try_from(pathid).unwrap()
);
usize::try_from(pathid).unwrap()
}
}
}
})
});
})
};
item.parent_idx = item.parent.and_then(|p| defid_to_rowid(p, false));
item.trait_parent_idx = item.trait_parent.and_then(|p| defid_to_rowid(p, true));

if let Some(defid) = item.defid
&& item.parent_idx.is_none()
Expand Down Expand Up @@ -1519,6 +1538,7 @@ pub(crate) fn build_index(
Some(EntryData {
ty: item.ty,
parent: item.parent_idx,
trait_parent: item.trait_parent_idx,
module_path,
exact_module_path,
deprecated: item.deprecation.is_some(),
Expand Down
22 changes: 21 additions & 1 deletion src/librustdoc/html/static/js/rustdoc.d.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ declare namespace rustdoc {
modulePath: number?,
exactModulePath: number?,
parent: number?,
traitParent: number?,
deprecated: boolean,
associatedItemDisambiguator: string?,
}
Expand Down Expand Up @@ -291,9 +292,12 @@ declare namespace rustdoc {
path: PathData?,
functionData: FunctionData?,
deprecated: boolean,
parent: { path: PathData, name: string}?,
parent: RowParent,
traitParent: RowParent,
}

type RowParent = { path: PathData, name: string } | null;

type ItemType = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26;
Expand All @@ -316,7 +320,23 @@ declare namespace rustdoc {
interface ResultObject {
desc: Promise<string|null>,
displayPath: string,
/**
* path to where the item was defined (not inlined),
* then `|`, then the `ItemType` of the item.
*
* This is often a private path, so it should not be displayed,
* but this allows us to use it to reliably deduplicate reexported and inlined items
*/
fullPath: string,
/**
* The `fullPath` of the corresponding item within a trait.
* For example, for `File::read`, this would be `std::io::Read::read|12`
*
* This is used to hide items from trait impls when the trait itself is in the search results.
*
* `null` if the item is not from a trait impl block.
*/
traitPath: string | null,
href: string,
id: number,
dist: number,
Expand Down
Loading
Loading

AltStyle によって変換されたページ (->オリジナル) /