Skip to content

Commit a28aaf0

Browse files
committed
Auto merge of #129789 - notriddle:notriddle/inline-stmt-local, r=<try>
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 0d63418 + a3127ca commit a28aaf0

28 files changed

+214
-196
lines changed

src/librustdoc/clean/auto_trait.rs

+12-10
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

+37-35
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

+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;
@@ -42,7 +42,7 @@ pub(crate) fn try_inline(
4242
cx: &mut DocContext<'_>,
4343
res: Res,
4444
name: Symbol,
45-
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
45+
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
4646
visited: &mut DefIdSet,
4747
) -> Option<Vec<clean::Item>> {
4848
let did = res.opt_def_id()?;
@@ -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.and_then(|def_id| def_id.as_local()),
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);
@@ -197,7 +191,7 @@ pub(crate) fn try_inline_glob(
197191
visited,
198192
inlined_names,
199193
Some(&reexports),
200-
Some((attrs, Some(import.owner_id.def_id.to_def_id()))),
194+
Some((attrs, Some(import.owner_id.def_id))),
201195
);
202196
items.retain(|item| {
203197
if let Some(name) = item.name {
@@ -371,7 +365,7 @@ fn build_type_alias(
371365
pub(crate) fn build_impls(
372366
cx: &mut DocContext<'_>,
373367
did: DefId,
374-
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
368+
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
375369
ret: &mut Vec<clean::Item>,
376370
) {
377371
let _prof_timer = cx.tcx.sess.prof.generic_activity("build_inherent_impls");
@@ -404,7 +398,7 @@ pub(crate) fn build_impls(
404398
pub(crate) fn merge_attrs(
405399
cx: &mut DocContext<'_>,
406400
old_attrs: &[ast::Attribute],
407-
new_attrs: Option<(&[ast::Attribute], Option<DefId>)>,
401+
new_attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
408402
) -> (clean::Attributes, Option<Arc<clean::cfg::Cfg>>) {
409403
// NOTE: If we have additional attributes (from a re-export),
410404
// always insert them first. This ensure that re-export
@@ -415,7 +409,7 @@ pub(crate) fn merge_attrs(
415409
both.extend_from_slice(old_attrs);
416410
(
417411
if let Some(item_id) = item_id {
418-
Attributes::from_ast_with_additional(old_attrs, (inner, item_id))
412+
Attributes::from_ast_with_additional(old_attrs, (inner, item_id.to_def_id()))
419413
} else {
420414
Attributes::from_ast(&both)
421415
},
@@ -430,7 +424,7 @@ pub(crate) fn merge_attrs(
430424
pub(crate) fn build_impl(
431425
cx: &mut DocContext<'_>,
432426
did: DefId,
433-
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
427+
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
434428
ret: &mut Vec<clean::Item>,
435429
) {
436430
if !cx.inlined.insert(did.into()) {
@@ -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
}
@@ -640,7 +634,7 @@ fn build_module_items(
640634
visited: &mut DefIdSet,
641635
inlined_names: &mut FxHashSet<(ItemType, Symbol)>,
642636
allowed_def_ids: Option<&DefIdSet>,
643-
attrs: Option<(&[ast::Attribute], Option<DefId>)>,
637+
attrs: Option<(&[ast::Attribute], Option<LocalDefId>)>,
644638
) -> Vec<clean::Item> {
645639
let mut items = Vec::new();
646640

@@ -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
});
@@ -744,15 +740,16 @@ fn build_macro(
744740
cx: &mut DocContext<'_>,
745741
def_id: DefId,
746742
name: Symbol,
747-
import_def_id: Option<DefId>,
743+
import_def_id: Option<LocalDefId>,
748744
macro_kind: MacroKind,
749745
is_doc_hidden: bool,
750746
) -> clean::ItemKind {
751747
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.tcx) {
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.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

+7-7
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ 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);
206-
item.inline_stmt_id = import_id.map(|local| local.to_def_id());
205+
let mut item = Item::from_def_id_and_attrs_and_parts(def_id, name, kind, attrs, cfg);
206+
item.inline_stmt_id = import_id;
207207
item
208208
}
209209

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

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

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

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

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

30863086
fn clean_maybe_renamed_foreign_item<'tcx>(

0 commit comments

Comments
 (0)