Skip to content

Commit

Permalink
rustdoc: use a single box to store Attributes and ItemKind
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Aug 30, 2024
1 parent 9ad8e26 commit a3127ca
Show file tree
Hide file tree
Showing 28 changed files with 194 additions and 178 deletions.
22 changes: 12 additions & 10 deletions src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,19 @@ fn synthesize_auto_trait_impl<'tcx>(

Some(clean::Item {
name: None,
attrs: Default::default(),
inner: Box::new(clean::ItemInner {
attrs: Default::default(),
kind: clean::ImplItem(Box::new(clean::Impl {
safety: hir::Safety::Safe,
generics,
trait_: Some(clean_trait_ref_with_constraints(cx, trait_ref, ThinVec::new())),
for_: clean_middle_ty(ty::Binder::dummy(ty), cx, None, None),
items: Vec::new(),
polarity,
kind: clean::ImplKind::Auto,
})),
}),
item_id: clean::ItemId::Auto { trait_: trait_def_id, for_: item_def_id },
kind: Box::new(clean::ImplItem(Box::new(clean::Impl {
safety: hir::Safety::Safe,
generics,
trait_: Some(clean_trait_ref_with_constraints(cx, trait_ref, ThinVec::new())),
for_: clean_middle_ty(ty::Binder::dummy(ty), cx, None, None),
items: Vec::new(),
polarity,
kind: clean::ImplKind::Auto,
}))),
cfg: None,
inline_stmt_id: None,
})
Expand Down
72 changes: 37 additions & 35 deletions src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,42 +83,44 @@ pub(crate) fn synthesize_blanket_impls(

blanket_impls.push(clean::Item {
name: None,
attrs: Default::default(),
item_id: clean::ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
kind: Box::new(clean::ImplItem(Box::new(clean::Impl {
safety: hir::Safety::Safe,
generics: clean_ty_generics(
cx,
tcx.generics_of(impl_def_id),
tcx.explicit_predicates_of(impl_def_id),
),
// FIXME(eddyb) compute both `trait_` and `for_` from
// the post-inference `trait_ref`, as it's more accurate.
trait_: Some(clean_trait_ref_with_constraints(
cx,
ty::Binder::dummy(trait_ref.instantiate_identity()),
ThinVec::new(),
)),
for_: clean_middle_ty(
ty::Binder::dummy(ty.instantiate_identity()),
cx,
None,
None,
),
items: tcx
.associated_items(impl_def_id)
.in_definition_order()
.filter(|item| !item.is_impl_trait_in_trait())
.map(|item| clean_middle_assoc_item(item, cx))
.collect(),
polarity: ty::ImplPolarity::Positive,
kind: clean::ImplKind::Blanket(Box::new(clean_middle_ty(
ty::Binder::dummy(trait_ref.instantiate_identity().self_ty()),
cx,
None,
None,
))),
}))),
inner: Box::new(clean::ItemInner {
attrs: Default::default(),
kind: clean::ImplItem(Box::new(clean::Impl {
safety: hir::Safety::Safe,
generics: clean_ty_generics(
cx,
tcx.generics_of(impl_def_id),
tcx.explicit_predicates_of(impl_def_id),
),
// FIXME(eddyb) compute both `trait_` and `for_` from
// the post-inference `trait_ref`, as it's more accurate.
trait_: Some(clean_trait_ref_with_constraints(
cx,
ty::Binder::dummy(trait_ref.instantiate_identity()),
ThinVec::new(),
)),
for_: clean_middle_ty(
ty::Binder::dummy(ty.instantiate_identity()),
cx,
None,
None,
),
items: tcx
.associated_items(impl_def_id)
.in_definition_order()
.filter(|item| !item.is_impl_trait_in_trait())
.map(|item| clean_middle_assoc_item(item, cx))
.collect(),
polarity: ty::ImplPolarity::Positive,
kind: clean::ImplKind::Blanket(Box::new(clean_middle_ty(
ty::Binder::dummy(trait_ref.instantiate_identity().self_ty()),
cx,
None,
None,
))),
})),
}),
cfg: None,
inline_stmt_id: None,
});
Expand Down
51 changes: 24 additions & 27 deletions src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,8 @@ pub(crate) fn try_inline(
};

cx.inlined.insert(did.into());
let mut item = crate::clean::generate_item_with_correct_attrs(
cx,
kind,
did,
name,
import_def_id,
None,
);
let mut item =
crate::clean::generate_item_with_correct_attrs(cx, kind, did, name, import_def_id, None);
// The visibility needs to reflect the one from the reexport and not from the "source" DefId.
item.inline_stmt_id = import_def_id;
ret.push(item);
Expand Down Expand Up @@ -622,7 +616,7 @@ pub(crate) fn build_impl(
ImplKind::Normal
},
})),
Box::new(merged_attrs),
merged_attrs,
cfg,
));
}
Expand Down Expand Up @@ -672,27 +666,29 @@ fn build_module_items(
let prim_ty = clean::PrimitiveType::from(p);
items.push(clean::Item {
name: None,
attrs: Box::default(),
// We can use the item's `DefId` directly since the only information ever used
// from it is `DefId.krate`.
item_id: ItemId::DefId(did),
kind: Box::new(clean::ImportItem(clean::Import::new_simple(
item.ident.name,
clean::ImportSource {
path: clean::Path {
res,
segments: thin_vec![clean::PathSegment {
name: prim_ty.as_sym(),
args: clean::GenericArgs::AngleBracketed {
args: Default::default(),
constraints: ThinVec::new(),
},
}],
inner: Box::new(clean::ItemInner {
attrs: Default::default(),
kind: clean::ImportItem(clean::Import::new_simple(
item.ident.name,
clean::ImportSource {
path: clean::Path {
res,
segments: thin_vec![clean::PathSegment {
name: prim_ty.as_sym(),
args: clean::GenericArgs::AngleBracketed {
args: Default::default(),
constraints: ThinVec::new(),
},
}],
},
did: None,
},
did: None,
},
true,
))),
true,
)),
}),
cfg: None,
inline_stmt_id: None,
});
Expand Down Expand Up @@ -752,7 +748,8 @@ fn build_macro(
LoadedMacro::MacroDef(item_def, _) => match macro_kind {
MacroKind::Bang => {
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
let vis = cx.tcx.visibility(import_def_id.map(|d| d.to_def_id()).unwrap_or(def_id));
let vis =
cx.tcx.visibility(import_def_id.map(|d| d.to_def_id()).unwrap_or(def_id));
clean::MacroItem(clean::Macro {
source: utils::display_macro_source(
cx,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn generate_item_with_correct_attrs(
let attrs = Attributes::from_ast_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);

let name = renamed.or(Some(name));
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, Box::new(attrs), cfg);
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, attrs, cfg);
item.inline_stmt_id = import_id;
item
}
Expand Down
55 changes: 33 additions & 22 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,7 @@ pub(crate) struct Item {
/// The name of this item.
/// Optional because not every item has a name, e.g. impls.
pub(crate) name: Option<Symbol>,
pub(crate) attrs: Box<Attributes>,
/// Information about this item that is specific to what kind of item it is.
/// E.g., struct vs enum vs function.
pub(crate) kind: Box<ItemKind>,
pub(crate) inner: Box<ItemInner>,
pub(crate) item_id: ItemId,
/// This is the `LocalDefId` of the `use` statement if the item was inlined.
/// The crate metadata doesn't hold this information, so the `use` statement
Expand All @@ -331,6 +328,21 @@ pub(crate) struct Item {
pub(crate) cfg: Option<Arc<Cfg>>,
}

#[derive(Clone)]
pub(crate) struct ItemInner {
/// Information about this item that is specific to what kind of item it is.
/// E.g., struct vs enum vs function.
pub(crate) kind: ItemKind,
pub(crate) attrs: Attributes,
}

impl std::ops::Deref for Item {
type Target = ItemInner;
fn deref(&self) -> &ItemInner {
&*self.inner
}
}

/// NOTE: this does NOT unconditionally print every item, to avoid thousands of lines of logs.
/// If you want to see the debug output for attributes and the `kind` as well, use `{:#?}` instead of `{:?}`.
impl fmt::Debug for Item {
Expand Down Expand Up @@ -390,9 +402,9 @@ impl Item {
}

pub(crate) fn span(&self, tcx: TyCtxt<'_>) -> Option<Span> {
let kind = match &*self.kind {
ItemKind::StrippedItem(k) => k,
_ => &*self.kind,
let kind = match &self.kind {
ItemKind::StrippedItem(k) => &*k,
_ => &self.kind,
};
match kind {
ItemKind::ModuleItem(Module { span, .. }) => Some(*span),
Expand Down Expand Up @@ -437,7 +449,7 @@ impl Item {
def_id,
name,
kind,
Box::new(Attributes::from_ast(ast_attrs)),
Attributes::from_ast(ast_attrs),
ast_attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
)
}
Expand All @@ -446,16 +458,15 @@ impl Item {
def_id: DefId,
name: Option<Symbol>,
kind: ItemKind,
attrs: Box<Attributes>,
attrs: Attributes,
cfg: Option<Arc<Cfg>>,
) -> Item {
trace!("name={name:?}, def_id={def_id:?} cfg={cfg:?}");

Item {
item_id: def_id.into(),
kind: Box::new(kind),
inner: Box::new(ItemInner { kind, attrs }),
name,
attrs,
cfg,
inline_stmt_id: None,
}
Expand Down Expand Up @@ -526,16 +537,16 @@ impl Item {
self.type_() == ItemType::Variant
}
pub(crate) fn is_associated_type(&self) -> bool {
matches!(&*self.kind, AssocTypeItem(..) | StrippedItem(box AssocTypeItem(..)))
matches!(self.kind, AssocTypeItem(..) | StrippedItem(box AssocTypeItem(..)))
}
pub(crate) fn is_ty_associated_type(&self) -> bool {
matches!(&*self.kind, TyAssocTypeItem(..) | StrippedItem(box TyAssocTypeItem(..)))
matches!(self.kind, TyAssocTypeItem(..) | StrippedItem(box TyAssocTypeItem(..)))
}
pub(crate) fn is_associated_const(&self) -> bool {
matches!(&*self.kind, AssocConstItem(..) | StrippedItem(box AssocConstItem(..)))
matches!(self.kind, AssocConstItem(..) | StrippedItem(box AssocConstItem(..)))
}
pub(crate) fn is_ty_associated_const(&self) -> bool {
matches!(&*self.kind, TyAssocConstItem(..) | StrippedItem(box TyAssocConstItem(..)))
matches!(self.kind, TyAssocConstItem(..) | StrippedItem(box TyAssocConstItem(..)))
}
pub(crate) fn is_method(&self) -> bool {
self.type_() == ItemType::Method
Expand All @@ -562,14 +573,14 @@ impl Item {
self.type_() == ItemType::Keyword
}
pub(crate) fn is_stripped(&self) -> bool {
match *self.kind {
match self.kind {
StrippedItem(..) => true,
ImportItem(ref i) => !i.should_be_displayed,
_ => false,
}
}
pub(crate) fn has_stripped_entries(&self) -> Option<bool> {
match *self.kind {
match self.kind {
StructItem(ref struct_) => Some(struct_.has_stripped_entries()),
UnionItem(ref union_) => Some(union_.has_stripped_entries()),
EnumItem(ref enum_) => Some(enum_.has_stripped_entries()),
Expand Down Expand Up @@ -612,7 +623,7 @@ impl Item {
}

pub(crate) fn is_default(&self) -> bool {
match *self.kind {
match self.kind {
ItemKind::MethodItem(_, Some(defaultness)) => {
defaultness.has_value() && !defaultness.is_final()
}
Expand Down Expand Up @@ -640,7 +651,7 @@ impl Item {
};
hir::FnHeader { safety: sig.safety(), abi: sig.abi(), constness, asyncness }
}
let header = match *self.kind {
let header = match self.kind {
ItemKind::ForeignFunctionItem(_, safety) => {
let def_id = self.def_id().unwrap();
let abi = tcx.fn_sig(def_id).skip_binder().abi();
Expand Down Expand Up @@ -679,7 +690,7 @@ impl Item {
ItemId::DefId(def_id) => def_id,
};

match *self.kind {
match self.kind {
// Primitives and Keywords are written in the source code as private modules.
// The modules need to be private so that nobody actually uses them, but the
// keywords and primitives that they are documenting are public.
Expand Down Expand Up @@ -2566,13 +2577,13 @@ mod size_asserts {

use super::*;
// tidy-alphabetical-start
static_assert_size!(Crate, 64); // frequently moved by-value
static_assert_size!(Crate, 56); // frequently moved by-value
static_assert_size!(DocFragment, 32);
static_assert_size!(GenericArg, 32);
static_assert_size!(GenericArgs, 32);
static_assert_size!(GenericParamDef, 40);
static_assert_size!(Generics, 16);
static_assert_size!(Item, 56);
static_assert_size!(Item, 48);
static_assert_size!(ItemKind, 48);
static_assert_size!(PathSegment, 40);
static_assert_size!(Type, 32);
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate {
// understood by rustdoc.
let mut module = clean_doc_module(&module, cx);

match *module.kind {
match module.kind {
ItemKind::ModuleItem(ref module) => {
for it in &module.items {
// `compiler_builtins` should be masked too, but we can't apply
Expand All @@ -59,7 +59,7 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate {
let primitives = local_crate.primitives(cx.tcx);
let keywords = local_crate.keywords(cx.tcx);
{
let ItemKind::ModuleItem(ref mut m) = *module.kind else { unreachable!() };
let ItemKind::ModuleItem(ref mut m) = &mut module.inner.kind else { unreachable!() };
m.items.extend(primitives.iter().map(|&(def_id, prim)| {
Item::from_def_id_and_parts(
def_id,
Expand Down Expand Up @@ -280,7 +280,7 @@ pub(crate) fn build_deref_target_impls(
let tcx = cx.tcx;

for item in items {
let target = match *item.kind {
let target = match item.kind {
ItemKind::AssocTypeItem(ref t, _) => &t.type_,
_ => continue,
};
Expand Down
Loading

0 comments on commit a3127ca

Please sign in to comment.