Skip to content
Draft
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
9 changes: 6 additions & 3 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

use std::iter::once;
use std::sync::Arc;
use std::vec;

use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::smallvec::smallvec;
use rustc_data_structures::thin_vec::{ThinVec, thin_vec};
use rustc_hir::def::{DefKind, MacroKinds, Res};
use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId, LocalModDefId};
Expand All @@ -17,6 +19,7 @@ use rustc_span::symbol::{Symbol, sym};
use tracing::{debug, trace};

use super::{Item, extract_cfg_from_attrs};
use crate::clean::paths::ItemPath;
use crate::clean::{
self, Attributes, CfgInfo, ImplKind, ItemId, Type, clean_bound_vars, clean_generics,
clean_impl_item, clean_middle_assoc_item, clean_middle_field, clean_middle_ty,
Expand Down Expand Up @@ -234,15 +237,15 @@ pub(crate) fn load_attrs<'hir>(tcx: TyCtxt<'hir>, did: DefId) -> &'hir [hir::Att
tcx.get_all_attrs(did)
}

pub(crate) fn item_relative_path(tcx: TyCtxt<'_>, def_id: DefId) -> Vec<Symbol> {
pub(crate) fn item_relative_path(tcx: TyCtxt<'_>, def_id: DefId) -> ItemPath {
tcx.def_path(def_id).data.into_iter().filter_map(|elem| elem.data.get_opt_name()).collect()
}

/// Get the public Rust path to an item. This is used to generate the URL to the item's page.
///
/// In particular: we handle macro differently: if it's not a macro 2.0 oe a built-in macro, then
/// it is generated at the top-level of the crate and its path will be `[crate_name, macro_name]`.
pub(crate) fn get_item_path(tcx: TyCtxt<'_>, def_id: DefId, kind: ItemType) -> Vec<Symbol> {
pub(crate) fn get_item_path(tcx: TyCtxt<'_>, def_id: DefId, kind: ItemType) -> ItemPath {
let crate_name = tcx.crate_name(def_id.krate);
let relative = item_relative_path(tcx, def_id);

Expand All @@ -255,7 +258,7 @@ pub(crate) fn get_item_path(tcx: TyCtxt<'_>, def_id: DefId, kind: ItemType) -> V
) {
once(crate_name).chain(relative).collect()
} else {
vec![crate_name, *relative.last().expect("relative was empty")]
smallvec![crate_name, *relative.last().expect("relative was empty")].into()
}
} else {
once(crate_name).chain(relative).collect()
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod auto_trait;
mod blanket_impl;
pub(crate) mod cfg;
pub(crate) mod inline;
pub(crate) mod paths;
mod render_macro_matchers;
mod simplify;
pub(crate) mod types;
Expand Down
60 changes: 60 additions & 0 deletions src/librustdoc/clean/paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::ops::{Deref, DerefMut};
use std::slice;

use rustc_data_structures::smallvec::{self, SmallVec};
use rustc_span::Symbol;

#[derive(Default, Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) struct ItemPath(SmallVec<[Symbol; 4]>);

impl From<SmallVec<[Symbol; 4]>> for ItemPath {
fn from(value: SmallVec<[Symbol; 4]>) -> Self {
Self(value)
}
}

impl From<&[Symbol]> for ItemPath {
fn from(value: &[Symbol]) -> Self {
SmallVec::from_slice(value).into()
}
}

impl FromIterator<Symbol> for ItemPath {
fn from_iter<T: IntoIterator<Item = Symbol>>(iter: T) -> Self {
Self(FromIterator::from_iter(iter))
}
}

impl Deref for ItemPath {
type Target = SmallVec<[Symbol; 4]>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for ItemPath {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl IntoIterator for ItemPath {
type Item = Symbol;

type IntoIter = smallvec::IntoIter<[Symbol; 4]>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl<'a> IntoIterator for &'a ItemPath {

@GuillaumeGomez GuillaumeGomez Jun 9, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since we have deref implemented already, do we need to have this IntoIterator implementation?

View changes since the review

type Item = &'a Symbol;

type IntoIter = slice::Iter<'a, Symbol>;

fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
18 changes: 10 additions & 8 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::mem;

use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
use rustc_data_structures::smallvec::smallvec;
use rustc_hir::StabilityLevel;
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet};
use rustc_metadata::creader::CStore;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::Symbol;
use tracing::debug;

use crate::clean::paths::ItemPath;
use crate::clean::types::ExternalLocation;
use crate::clean::{self, ExternalCrate, ItemId, PrimitiveType};
use crate::config::RenderOptions;
Expand Down Expand Up @@ -42,11 +44,11 @@ pub(crate) struct Cache {
/// URLs when a type is being linked to. External paths are not located in
/// this map because the `External` type itself has all the information
/// necessary.
pub(crate) paths: FxIndexMap<DefId, (Vec<Symbol>, ItemType)>,
pub(crate) paths: FxIndexMap<DefId, (ItemPath, ItemType)>,

/// Similar to `paths`, but only holds external paths. This is only used for
/// generating explicit hyperlinks to other crates.
pub(crate) external_paths: FxIndexMap<DefId, (Vec<Symbol>, ItemType)>,
pub(crate) external_paths: FxIndexMap<DefId, (ItemPath, ItemType)>,

/// Maps local `DefId`s of exported types to fully qualified paths.
/// Unlike 'paths', this mapping ignores any renames that occur
Expand All @@ -58,7 +60,7 @@ pub(crate) struct Cache {
/// to the path used if the corresponding type is inlined. By
/// doing this, we can detect duplicate impls on a trait page, and only display
/// the impl for the inlined type.
pub(crate) exact_paths: DefIdMap<Vec<Symbol>>,
pub(crate) exact_paths: DefIdMap<ItemPath>,

/// This map contains information about all known traits of this crate.
/// Implementations of a crate should inherit the documentation of the
Expand Down Expand Up @@ -98,7 +100,7 @@ pub(crate) struct Cache {
pub(crate) masked_crates: FxHashSet<CrateNum>,

// Private fields only used when initially crawling a crate to build a cache
stack: Vec<Symbol>,
stack: ItemPath,
parent_stack: Vec<ParentStackItem>,
stripped_mod: bool,

Expand Down Expand Up @@ -193,7 +195,7 @@ impl Cache {
render_options.extern_html_root_urls.get(name.as_str()).map(|u| &**u);
e.location(extern_url, extern_url_takes_precedence, dst, tcx)
});
cx.cache.external_paths.insert(e.def_id(), (vec![name], ItemType::Module));
cx.cache.external_paths.insert(e.def_id(), (smallvec![name].into(), ItemType::Module));
}

// FIXME: avoid this clone (requires implementing Default manually)
Expand All @@ -204,7 +206,7 @@ impl Cache {
// If that restriction is ever lifted, this will have to include the relative paths instead.
cx.cache
.external_paths
.insert(def_id, (vec![crate_name, prim.as_sym()], ItemType::Primitive));
.insert(def_id, (smallvec![crate_name, prim.as_sym()].into(), ItemType::Primitive));
}

let (krate, mut impl_ids) = {
Expand Down Expand Up @@ -572,7 +574,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
if item_def_id.is_crate_root() || cache.stripped_mod {
return;
}
(None, &*cache.stack)
(None, &cache.stack[..])
}
};

Expand Down Expand Up @@ -600,7 +602,7 @@ fn add_item_to_search_index(tcx: TyCtxt<'_>, cache: &mut Cache, item: &clean::It
let index_item = IndexItem {
defid: Some(defid),
name,
module_path: parent_path.to_vec(),
module_path: parent_path.into(),
parent: parent_did,
parent_idx: None,
trait_parent,
Expand Down
6 changes: 4 additions & 2 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use itertools::{Either, Itertools};
use rustc_abi::ExternAbi;
use rustc_ast::join_path_syms;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::smallvec::smallvec;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, MacroKinds};
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
Expand All @@ -25,6 +26,7 @@ use rustc_span::{Ident, Symbol};
use tracing::{debug, trace};

use super::url_parts_builder::UrlPartsBuilder;
use crate::clean::paths::ItemPath;
use crate::clean::types::ExternalLocation;
use crate::clean::utils::find_nearest_parent_module;
use crate::clean::{self, ExternalCrate, PrimitiveType};
Expand Down Expand Up @@ -354,7 +356,7 @@ pub(crate) struct HrefInfo {
/// Kind of the item (used to generate the `title` attribute).
pub(crate) kind: ItemType,
/// Rust path to the item (used to generate the `title` attribute).
pub(crate) rust_path: Vec<Symbol>,
pub(crate) rust_path: ItemPath,
}

/// This function is to get the external macro path because they are not in the cache used in
Expand Down Expand Up @@ -451,7 +453,7 @@ fn generate_item_def_id_path(
}
}

let mut fqp = vec![crate_name];
let mut fqp = ItemPath::from(smallvec![crate_name]);
let shortty = if let Some(prim) = prim {
fqp.push(prim.as_sym());
ItemType::Primitive
Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ use tracing::{debug, info};

pub(crate) use self::context::*;
pub(crate) use self::write_shared::*;
use crate::clean::paths::ItemPath;
use crate::clean::{self, Defaultness, Item, ItemId, RenderedLink};
use crate::display::{Joined as _, MaybeDisplay as _};
use crate::error::Error;
Expand Down Expand Up @@ -158,12 +159,12 @@ impl IndexItemInfo {
pub(crate) struct IndexItem {
pub(crate) defid: Option<DefId>,
pub(crate) name: Symbol,
pub(crate) module_path: Vec<Symbol>,
pub(crate) module_path: ItemPath,
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) exact_module_path: Option<ItemPath>,
pub(crate) impl_id: Option<DefId>,
pub(crate) info: IndexItemInfo,
}
Expand Down
Loading
Loading