Skip to content

Commit a3127ca

Browse files
committed
rustdoc: use a single box to store Attributes and ItemKind
1 parent 9ad8e26 commit a3127ca

28 files changed

+194
-178
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,19 @@ fn synthesize_auto_trait_impl<'tcx>(
114114

115115
Some(clean::Item {
116116
name: None,
117-
attrs: Default::default(),
117+
inner: Box::new(clean::ItemInner {
118+
attrs: Default::default(),
119+
kind: clean::ImplItem(Box::new(clean::Impl {
120+
safety: hir::Safety::Safe,
121+
generics,
122+
trait_: Some(clean_trait_ref_with_constraints(cx, trait_ref, ThinVec::new())),
123+
for_: clean_middle_ty(ty::Binder::dummy(ty), cx, None, None),
124+
items: Vec::new(),
125+
polarity,
126+
kind: clean::ImplKind::Auto,
127+
})),
128+
}),
118129
item_id: clean::ItemId::Auto { trait_: trait_def_id, for_: item_def_id },
119-
kind: Box::new(clean::ImplItem(Box::new(clean::Impl {
120-
safety: hir::Safety::Safe,
121-
generics,
122-
trait_: Some(clean_trait_ref_with_constraints(cx, trait_ref, ThinVec::new())),
123-
for_: clean_middle_ty(ty::Binder::dummy(ty), cx, None, None),
124-
items: Vec::new(),
125-
polarity,
126-
kind: clean::ImplKind::Auto,
127-
}))),
128130
cfg: None,
129131
inline_stmt_id: None,
130132
})

src/librustdoc/clean/blanket_impl.rs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -83,42 +83,44 @@ pub(crate) fn synthesize_blanket_impls(
8383

8484
blanket_impls.push(clean::Item {
8585
name: None,
86-
attrs: Default::default(),
8786
item_id: clean::ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
88-
kind: Box::new(clean::ImplItem(Box::new(clean::Impl {
89-
safety: hir::Safety::Safe,
90-
generics: clean_ty_generics(
91-
cx,
92-
tcx.generics_of(impl_def_id),
93-
tcx.explicit_predicates_of(impl_def_id),
94-
),
95-
// FIXME(eddyb) compute both `trait_` and `for_` from
96-
// the post-inference `trait_ref`, as it's more accurate.
97-
trait_: Some(clean_trait_ref_with_constraints(
98-
cx,
99-
ty::Binder::dummy(trait_ref.instantiate_identity()),
100-
ThinVec::new(),
101-
)),
102-
for_: clean_middle_ty(
103-
ty::Binder::dummy(ty.instantiate_identity()),
104-
cx,
105-
None,
106-
None,
107-
),
108-
items: tcx
109-
.associated_items(impl_def_id)
110-
.in_definition_order()
111-
.filter(|item| !item.is_impl_trait_in_trait())
112-
.map(|item| clean_middle_assoc_item(item, cx))
113-
.collect(),
114-
polarity: ty::ImplPolarity::Positive,
115-
kind: clean::ImplKind::Blanket(Box::new(clean_middle_ty(
116-
ty::Binder::dummy(trait_ref.instantiate_identity().self_ty()),
117-
cx,
118-
None,
119-
None,
120-
))),
121-
}))),
87+
inner: Box::new(clean::ItemInner {
88+
attrs: Default::default(),
89+
kind: clean::ImplItem(Box::new(clean::Impl {
90+
safety: hir::Safety::Safe,
91+
generics: clean_ty_generics(
92+
cx,
93+
tcx.generics_of(impl_def_id),
94+
tcx.explicit_predicates_of(impl_def_id),
95+
),
96+
// FIXME(eddyb) compute both `trait_` and `for_` from
97+
// the post-inference `trait_ref`, as it's more accurate.
98+
trait_: Some(clean_trait_ref_with_constraints(
99+
cx,
100+
ty::Binder::dummy(trait_ref.instantiate_identity()),
101+
ThinVec::new(),
102+
)),
103+
for_: clean_middle_ty(
104+
ty::Binder::dummy(ty.instantiate_identity()),
105+
cx,
106+
None,
107+
None,
108+
),
109+
items: tcx
110+
.associated_items(impl_def_id)
111+
.in_definition_order()
112+
.filter(|item| !item.is_impl_trait_in_trait())
113+
.map(|item| clean_middle_assoc_item(item, cx))
114+
.collect(),
115+
polarity: ty::ImplPolarity::Positive,
116+
kind: clean::ImplKind::Blanket(Box::new(clean_middle_ty(
117+
ty::Binder::dummy(trait_ref.instantiate_identity().self_ty()),
118+
cx,
119+
None,
120+
None,
121+
))),
122+
})),
123+
}),
122124
cfg: None,
123125
inline_stmt_id: None,
124126
});

src/librustdoc/clean/inline.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,8 @@ pub(crate) fn try_inline(
151151
};
152152

153153
cx.inlined.insert(did.into());
154-
let mut item = crate::clean::generate_item_with_correct_attrs(
155-
cx,
156-
kind,
157-
did,
158-
name,
159-
import_def_id,
160-
None,
161-
);
154+
let mut item =
155+
crate::clean::generate_item_with_correct_attrs(cx, kind, did, name, import_def_id, None);
162156
// The visibility needs to reflect the one from the reexport and not from the "source" DefId.
163157
item.inline_stmt_id = import_def_id;
164158
ret.push(item);
@@ -622,7 +616,7 @@ pub(crate) fn build_impl(
622616
ImplKind::Normal
623617
},
624618
})),
625-
Box::new(merged_attrs),
619+
merged_attrs,
626620
cfg,
627621
));
628622
}
@@ -672,27 +666,29 @@ fn build_module_items(
672666
let prim_ty = clean::PrimitiveType::from(p);
673667
items.push(clean::Item {
674668
name: None,
675-
attrs: Box::default(),
676669
// We can use the item's `DefId` directly since the only information ever used
677670
// from it is `DefId.krate`.
678671
item_id: ItemId::DefId(did),
679-
kind: Box::new(clean::ImportItem(clean::Import::new_simple(
680-
item.ident.name,
681-
clean::ImportSource {
682-
path: clean::Path {
683-
res,
684-
segments: thin_vec![clean::PathSegment {
685-
name: prim_ty.as_sym(),
686-
args: clean::GenericArgs::AngleBracketed {
687-
args: Default::default(),
688-
constraints: ThinVec::new(),
689-
},
690-
}],
672+
inner: Box::new(clean::ItemInner {
673+
attrs: Default::default(),
674+
kind: clean::ImportItem(clean::Import::new_simple(
675+
item.ident.name,
676+
clean::ImportSource {
677+
path: clean::Path {
678+
res,
679+
segments: thin_vec![clean::PathSegment {
680+
name: prim_ty.as_sym(),
681+
args: clean::GenericArgs::AngleBracketed {
682+
args: Default::default(),
683+
constraints: ThinVec::new(),
684+
},
685+
}],
686+
},
687+
did: None,
691688
},
692-
did: None,
693-
},
694-
true,
695-
))),
689+
true,
690+
)),
691+
}),
696692
cfg: None,
697693
inline_stmt_id: None,
698694
});
@@ -752,7 +748,8 @@ fn build_macro(
752748
LoadedMacro::MacroDef(item_def, _) => match macro_kind {
753749
MacroKind::Bang => {
754750
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
755-
let vis = cx.tcx.visibility(import_def_id.map(|d| d.to_def_id()).unwrap_or(def_id));
751+
let vis =
752+
cx.tcx.visibility(import_def_id.map(|d| d.to_def_id()).unwrap_or(def_id));
756753
clean::MacroItem(clean::Macro {
757754
source: utils::display_macro_source(
758755
cx,

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ fn generate_item_with_correct_attrs(
202202
let attrs = Attributes::from_ast_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
203203

204204
let name = renamed.or(Some(name));
205-
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, Box::new(attrs), cfg);
205+
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, attrs, cfg);
206206
item.inline_stmt_id = import_id;
207207
item
208208
}

src/librustdoc/clean/types.rs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,7 @@ pub(crate) struct Item {
319319
/// The name of this item.
320320
/// Optional because not every item has a name, e.g. impls.
321321
pub(crate) name: Option<Symbol>,
322-
pub(crate) attrs: Box<Attributes>,
323-
/// Information about this item that is specific to what kind of item it is.
324-
/// E.g., struct vs enum vs function.
325-
pub(crate) kind: Box<ItemKind>,
322+
pub(crate) inner: Box<ItemInner>,
326323
pub(crate) item_id: ItemId,
327324
/// This is the `LocalDefId` of the `use` statement if the item was inlined.
328325
/// The crate metadata doesn't hold this information, so the `use` statement
@@ -331,6 +328,21 @@ pub(crate) struct Item {
331328
pub(crate) cfg: Option<Arc<Cfg>>,
332329
}
333330

331+
#[derive(Clone)]
332+
pub(crate) struct ItemInner {
333+
/// Information about this item that is specific to what kind of item it is.
334+
/// E.g., struct vs enum vs function.
335+
pub(crate) kind: ItemKind,
336+
pub(crate) attrs: Attributes,
337+
}
338+
339+
impl std::ops::Deref for Item {
340+
type Target = ItemInner;
341+
fn deref(&self) -> &ItemInner {
342+
&*self.inner
343+
}
344+
}
345+
334346
/// NOTE: this does NOT unconditionally print every item, to avoid thousands of lines of logs.
335347
/// If you want to see the debug output for attributes and the `kind` as well, use `{:#?}` instead of `{:?}`.
336348
impl fmt::Debug for Item {
@@ -390,9 +402,9 @@ impl Item {
390402
}
391403

392404
pub(crate) fn span(&self, tcx: TyCtxt<'_>) -> Option<Span> {
393-
let kind = match &*self.kind {
394-
ItemKind::StrippedItem(k) => k,
395-
_ => &*self.kind,
405+
let kind = match &self.kind {
406+
ItemKind::StrippedItem(k) => &*k,
407+
_ => &self.kind,
396408
};
397409
match kind {
398410
ItemKind::ModuleItem(Module { span, .. }) => Some(*span),
@@ -437,7 +449,7 @@ impl Item {
437449
def_id,
438450
name,
439451
kind,
440-
Box::new(Attributes::from_ast(ast_attrs)),
452+
Attributes::from_ast(ast_attrs),
441453
ast_attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
442454
)
443455
}
@@ -446,16 +458,15 @@ impl Item {
446458
def_id: DefId,
447459
name: Option<Symbol>,
448460
kind: ItemKind,
449-
attrs: Box<Attributes>,
461+
attrs: Attributes,
450462
cfg: Option<Arc<Cfg>>,
451463
) -> Item {
452464
trace!("name={name:?}, def_id={def_id:?} cfg={cfg:?}");
453465

454466
Item {
455467
item_id: def_id.into(),
456-
kind: Box::new(kind),
468+
inner: Box::new(ItemInner { kind, attrs }),
457469
name,
458-
attrs,
459470
cfg,
460471
inline_stmt_id: None,
461472
}
@@ -526,16 +537,16 @@ impl Item {
526537
self.type_() == ItemType::Variant
527538
}
528539
pub(crate) fn is_associated_type(&self) -> bool {
529-
matches!(&*self.kind, AssocTypeItem(..) | StrippedItem(box AssocTypeItem(..)))
540+
matches!(self.kind, AssocTypeItem(..) | StrippedItem(box AssocTypeItem(..)))
530541
}
531542
pub(crate) fn is_ty_associated_type(&self) -> bool {
532-
matches!(&*self.kind, TyAssocTypeItem(..) | StrippedItem(box TyAssocTypeItem(..)))
543+
matches!(self.kind, TyAssocTypeItem(..) | StrippedItem(box TyAssocTypeItem(..)))
533544
}
534545
pub(crate) fn is_associated_const(&self) -> bool {
535-
matches!(&*self.kind, AssocConstItem(..) | StrippedItem(box AssocConstItem(..)))
546+
matches!(self.kind, AssocConstItem(..) | StrippedItem(box AssocConstItem(..)))
536547
}
537548
pub(crate) fn is_ty_associated_const(&self) -> bool {
538-
matches!(&*self.kind, TyAssocConstItem(..) | StrippedItem(box TyAssocConstItem(..)))
549+
matches!(self.kind, TyAssocConstItem(..) | StrippedItem(box TyAssocConstItem(..)))
539550
}
540551
pub(crate) fn is_method(&self) -> bool {
541552
self.type_() == ItemType::Method
@@ -562,14 +573,14 @@ impl Item {
562573
self.type_() == ItemType::Keyword
563574
}
564575
pub(crate) fn is_stripped(&self) -> bool {
565-
match *self.kind {
576+
match self.kind {
566577
StrippedItem(..) => true,
567578
ImportItem(ref i) => !i.should_be_displayed,
568579
_ => false,
569580
}
570581
}
571582
pub(crate) fn has_stripped_entries(&self) -> Option<bool> {
572-
match *self.kind {
583+
match self.kind {
573584
StructItem(ref struct_) => Some(struct_.has_stripped_entries()),
574585
UnionItem(ref union_) => Some(union_.has_stripped_entries()),
575586
EnumItem(ref enum_) => Some(enum_.has_stripped_entries()),
@@ -612,7 +623,7 @@ impl Item {
612623
}
613624

614625
pub(crate) fn is_default(&self) -> bool {
615-
match *self.kind {
626+
match self.kind {
616627
ItemKind::MethodItem(_, Some(defaultness)) => {
617628
defaultness.has_value() && !defaultness.is_final()
618629
}
@@ -640,7 +651,7 @@ impl Item {
640651
};
641652
hir::FnHeader { safety: sig.safety(), abi: sig.abi(), constness, asyncness }
642653
}
643-
let header = match *self.kind {
654+
let header = match self.kind {
644655
ItemKind::ForeignFunctionItem(_, safety) => {
645656
let def_id = self.def_id().unwrap();
646657
let abi = tcx.fn_sig(def_id).skip_binder().abi();
@@ -679,7 +690,7 @@ impl Item {
679690
ItemId::DefId(def_id) => def_id,
680691
};
681692

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

25672578
use super::*;
25682579
// tidy-alphabetical-start
2569-
static_assert_size!(Crate, 64); // frequently moved by-value
2580+
static_assert_size!(Crate, 56); // frequently moved by-value
25702581
static_assert_size!(DocFragment, 32);
25712582
static_assert_size!(GenericArg, 32);
25722583
static_assert_size!(GenericArgs, 32);
25732584
static_assert_size!(GenericParamDef, 40);
25742585
static_assert_size!(Generics, 16);
2575-
static_assert_size!(Item, 56);
2586+
static_assert_size!(Item, 48);
25762587
static_assert_size!(ItemKind, 48);
25772588
static_assert_size!(PathSegment, 40);
25782589
static_assert_size!(Type, 32);

src/librustdoc/clean/utils.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate {
3535
// understood by rustdoc.
3636
let mut module = clean_doc_module(&module, cx);
3737

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

282282
for item in items {
283-
let target = match *item.kind {
283+
let target = match item.kind {
284284
ItemKind::AssocTypeItem(ref t, _) => &t.type_,
285285
_ => continue,
286286
};

0 commit comments

Comments
 (0)