Skip to content

Commit 712463d

Browse files
committed
Auto merge of #129789 - notriddle:notriddle/inline-stmt-local, r=GuillaumeGomez
rustdoc: use strategic boxing to shrink `clean::Item` * `inline_stmt_id` is never a cross-crate DefId, so save space by not storing it. * Instead of two inner boxes for `Item`, use one.
2 parents c2f74c3 + 6590336 commit 712463d

28 files changed

+215
-197
lines changed

src/librustdoc/clean/auto_trait.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,19 @@ fn synthesize_auto_trait_impl<'tcx>(
115115

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

src/librustdoc/clean/blanket_impl.rs

+37-35
Original file line numberDiff line numberDiff line change
@@ -84,42 +84,44 @@ pub(crate) fn synthesize_blanket_impls(
8484

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

src/librustdoc/clean/inline.rs

+33-36
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55

66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_hir::def::{DefKind, Res};
8-
use rustc_hir::def_id::{DefId, DefIdSet, LocalModDefId};
8+
use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId, LocalModDefId};
99
use rustc_hir::Mutability;
1010
use rustc_metadata::creader::{CStore, LoadedMacro};
1111
use rustc_middle::ty::fast_reject::SimplifiedType;
@@ -43,7 +43,7 @@ pub(crate) fn try_inline(
4343
cx: &mut DocContext<'_>,
4444
res: Res,
4545
name: Symbol,
46-
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
46+
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
4747
visited: &mut DefIdSet,
4848
) -> Option<Vec<clean::Item>> {
4949
let did = res.opt_def_id()?;
@@ -152,14 +152,8 @@ pub(crate) fn try_inline(
152152
};
153153

154154
cx.inlined.insert(did.into());
155-
let mut item = crate::clean::generate_item_with_correct_attrs(
156-
cx,
157-
kind,
158-
did,
159-
name,
160-
import_def_id.and_then(|def_id| def_id.as_local()),
161-
None,
162-
);
155+
let mut item =
156+
crate::clean::generate_item_with_correct_attrs(cx, kind, did, name, import_def_id, None);
163157
// The visibility needs to reflect the one from the reexport and not from the "source" DefId.
164158
item.inline_stmt_id = import_def_id;
165159
ret.push(item);
@@ -198,7 +192,7 @@ pub(crate) fn try_inline_glob(
198192
visited,
199193
inlined_names,
200194
Some(&reexports),
201-
Some((attrs, Some(import.owner_id.def_id.to_def_id()))),
195+
Some((attrs, Some(import.owner_id.def_id))),
202196
);
203197
items.retain(|item| {
204198
if let Some(name) = item.name {
@@ -372,7 +366,7 @@ fn build_type_alias(
372366
pub(crate) fn build_impls(
373367
cx: &mut DocContext<'_>,
374368
did: DefId,
375-
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
369+
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
376370
ret: &mut Vec<clean::Item>,
377371
) {
378372
let _prof_timer = cx.tcx.sess.prof.generic_activity("build_inherent_impls");
@@ -405,7 +399,7 @@ pub(crate) fn build_impls(
405399
pub(crate) fn merge_attrs(
406400
cx: &mut DocContext<'_>,
407401
old_attrs: &[ast::Attribute],
408-
new_attrs: Option<(&[ast::Attribute], Option<DefId>)>,
402+
new_attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
409403
) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
410404
// NOTE: If we have additional attributes (from a re-export),
411405
// always insert them first. This ensure that re-export
@@ -416,7 +410,7 @@ pub(crate) fn merge_attrs(
416410
both.extend_from_slice(old_attrs);
417411
(
418412
if let Some(item_id) = item_id {
419-
Attributes::from_ast_with_additional(old_attrs, (inner, item_id))
413+
Attributes::from_ast_with_additional(old_attrs, (inner, item_id.to_def_id()))
420414
} else {
421415
Attributes::from_ast(&both)
422416
},
@@ -431,7 +425,7 @@ pub(crate) fn merge_attrs(
431425
pub(crate) fn build_impl(
432426
cx: &mut DocContext<'_>,
433427
did: DefId,
434-
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
428+
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
435429
ret: &mut Vec<clean::Item>,
436430
) {
437431
if !cx.inlined.insert(did.into()) {
@@ -623,7 +617,7 @@ pub(crate) fn build_impl(
623617
ImplKind::Normal
624618
},
625619
})),
626-
Box::new(merged_attrs),
620+
merged_attrs,
627621
cfg,
628622
));
629623
}
@@ -641,7 +635,7 @@ fn build_module_items(
641635
visited: &mut DefIdSet,
642636
inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
643637
allowed_def_ids: Option<&DefIdSet>,
644-
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
638+
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
645639
) -> Vec<clean::Item> {
646640
let mut items = Vec::new();
647641

@@ -673,27 +667,29 @@ fn build_module_items(
673667
let prim_ty = clean::PrimitiveType::from(p);
674668
items.push(clean::Item {
675669
name: None,
676-
attrs: Box::default(),
677670
// We can use the item's `DefId` directly since the only information ever used
678671
// from it is `DefId.krate`.
679672
item_id: ItemId::DefId(did),
680-
kind: Box::new(clean::ImportItem(clean::Import::new_simple(
681-
item.ident.name,
682-
clean::ImportSource {
683-
path: clean::Path {
684-
res,
685-
segments: thin_vec![clean::PathSegment {
686-
name: prim_ty.as_sym(),
687-
args: clean::GenericArgs::AngleBracketed {
688-
args: Default::default(),
689-
constraints: ThinVec::new(),
690-
},
691-
}],
673+
inner: Box::new(clean::ItemInner {
674+
attrs: Default::default(),
675+
kind: clean::ImportItem(clean::Import::new_simple(
676+
item.ident.name,
677+
clean::ImportSource {
678+
path: clean::Path {
679+
res,
680+
segments: thin_vec![clean::PathSegment {
681+
name: prim_ty.as_sym(),
682+
args: clean::GenericArgs::AngleBracketed {
683+
args: Default::default(),
684+
constraints: ThinVec::new(),
685+
},
686+
}],
687+
},
688+
did: None,
692689
},
693-
did: None,
694-
},
695-
true,
696-
))),
690+
true,
691+
)),
692+
}),
697693
cfg: None,
698694
inline_stmt_id: None,
699695
});
@@ -745,15 +741,16 @@ fn build_macro(
745741
cx: &mut DocContext<'_>,
746742
def_id: DefId,
747743
name: Symbol,
748-
import_def_id: Option<DefId>,
744+
import_def_id: Option<LocalDefId>,
749745
macro_kind: MacroKind,
750746
is_doc_hidden: bool,
751747
) -> clean::ItemKind {
752748
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.tcx) {
753749
LoadedMacro::MacroDef(item_def, _) => match macro_kind {
754750
MacroKind::Bang => {
755751
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
756-
let vis = cx.tcx.visibility(import_def_id.unwrap_or(def_id));
752+
let vis =
753+
cx.tcx.visibility(import_def_id.map(|d| d.to_def_id()).unwrap_or(def_id));
757754
clean::MacroItem(clean::Macro {
758755
source: utils::display_macro_source(
759756
cx,

src/librustdoc/clean/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ fn generate_item_with_correct_attrs(
203203
let attrs = Attributes::from_ast_iter(attrs.iter().map(|(attr, did)| (&**attr, *did)), false);
204204

205205
let name = renamed.or(Some(name));
206-
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, Box::new(attrs), cfg);
207-
item.inline_stmt_id = import_id.map(|local| local.to_def_id());
206+
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, attrs, cfg);
207+
item.inline_stmt_id = import_id;
208208
item
209209
}
210210

@@ -2927,7 +2927,7 @@ fn clean_extern_crate<'tcx>(
29272927
})
29282928
&& !cx.output_format.is_json();
29292929

2930-
let krate_owner_def_id = krate.owner_id.to_def_id();
2930+
let krate_owner_def_id = krate.owner_id.def_id;
29312931
if please_inline {
29322932
if let Some(items) = inline::try_inline(
29332933
cx,
@@ -2941,7 +2941,7 @@ fn clean_extern_crate<'tcx>(
29412941
}
29422942

29432943
vec![Item::from_def_id_and_parts(
2944-
krate_owner_def_id,
2944+
krate_owner_def_id.to_def_id(),
29452945
Some(name),
29462946
ExternCrateItem { src: orig_name },
29472947
cx,
@@ -2988,7 +2988,7 @@ fn clean_use_statement_inner<'tcx>(
29882988
let inline_attr = attrs.lists(sym::doc).get_word_attr(sym::inline);
29892989
let pub_underscore = visibility.is_public() && name == kw::Underscore;
29902990
let current_mod = cx.tcx.parent_module_from_def_id(import.owner_id.def_id);
2991-
let import_def_id = import.owner_id.def_id.to_def_id();
2991+
let import_def_id = import.owner_id.def_id;
29922992

29932993
// The parent of the module in which this import resides. This
29942994
// is the same as `current_mod` if that's already the top
@@ -3071,7 +3071,7 @@ fn clean_use_statement_inner<'tcx>(
30713071
)
30723072
{
30733073
items.push(Item::from_def_id_and_parts(
3074-
import_def_id,
3074+
import_def_id.to_def_id(),
30753075
None,
30763076
ImportItem(Import::new_simple(name, resolve_use_source(cx, path), false)),
30773077
cx,
@@ -3081,7 +3081,7 @@ fn clean_use_statement_inner<'tcx>(
30813081
Import::new_simple(name, resolve_use_source(cx, path), true)
30823082
};
30833083

3084-
vec![Item::from_def_id_and_parts(import_def_id, None, ImportItem(inner), cx)]
3084+
vec![Item::from_def_id_and_parts(import_def_id.to_def_id(), None, ImportItem(inner), cx)]
30853085
}
30863086

30873087
fn clean_maybe_renamed_foreign_item<'tcx>(

0 commit comments

Comments
 (0)