Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc_metadata: Deduplicate code between encode_info_for_item, encode_info_for_foreign_item, encode_info_for_trait_item, and encode_info_for_impl_item #78081

Closed
wants to merge 3 commits into from
Closed
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
86 changes: 37 additions & 49 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,9 +758,7 @@ impl EncodeContext<'a, 'tcx> {
// FIXME(eddyb) is this ever used?
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_bounds(def_id);
self.encode_optimized_mir(def_id.expect_local());
self.encode_promoted_mir(def_id.expect_local());
}
Expand Down Expand Up @@ -789,9 +787,7 @@ impl EncodeContext<'a, 'tcx> {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_bounds(def_id);
self.encode_optimized_mir(def_id.expect_local());
self.encode_promoted_mir(def_id.expect_local());
}
Expand Down Expand Up @@ -868,9 +864,7 @@ impl EncodeContext<'a, 'tcx> {
self.encode_stability(def_id);
self.encode_deprecation(def_id);
self.encode_item_type(def_id);
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_bounds(def_id);
}

fn encode_struct_ctor(&mut self, adt_def: &ty::AdtDef, def_id: DefId) {
Expand All @@ -896,9 +890,7 @@ impl EncodeContext<'a, 'tcx> {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_bounds(def_id);
self.encode_optimized_mir(def_id.expect_local());
self.encode_promoted_mir(def_id.expect_local());
}
Expand All @@ -908,14 +900,11 @@ impl EncodeContext<'a, 'tcx> {
record!(self.tables.generics[def_id] <- self.tcx.generics_of(def_id));
}

fn encode_explicit_predicates(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_explicit_predicates({:?})", def_id);
fn encode_bounds(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_bounds({:?})", def_id);
self.encode_generics(def_id);
record!(self.tables.explicit_predicates[def_id] <-
self.tcx.explicit_predicates_of(def_id));
}

fn encode_inferred_outlives(&mut self, def_id: DefId) {
debug!("EncodeContext::encode_inferred_outlives({:?})", def_id);
let inferred_outlives = self.tcx.inferred_outlives_of(def_id);
if !inferred_outlives.is_empty() {
record!(self.tables.inferred_outlives[def_id] <- inferred_outlives);
Expand Down Expand Up @@ -1013,9 +1002,7 @@ impl EncodeContext<'a, 'tcx> {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_bounds(def_id);

// This should be kept in sync with `PrefetchVisitor.visit_trait_item`.
self.encode_optimized_mir(def_id.expect_local());
Expand Down Expand Up @@ -1086,9 +1073,7 @@ impl EncodeContext<'a, 'tcx> {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_bounds(def_id);

// The following part should be kept in sync with `PrefetchVisitor.visit_impl_item`.

Expand Down Expand Up @@ -1196,12 +1181,34 @@ impl EncodeContext<'a, 'tcx> {
self.lazy(rendered_const)
}

/// Common encoding operations between [`encode_info_for_item`] and [`encode_info_for_foreign_item`].
///
/// [`encode_info_for_item`]: EncodeContext::encode_info_for_item
/// [`encode_info_for_foreign_item`]: EncodeContext::encode_info_for_foreign_item
fn encode_info_for_item_foreign_item_common(
&mut self,
def_id: DefId,
attrs: &'_ [ast::Attribute],
ident: Ident,
) {
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
record!(self.tables.attributes[def_id] <- attrs);
self.encode_ident_span(def_id, ident);
self.encode_stability(def_id);
self.encode_const_stability(def_id);
self.encode_deprecation(def_id);
self.encode_inherent_implementations(def_id);
}

fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'tcx>) {
let tcx = self.tcx;

debug!("EncodeContext::encode_info_for_item({:?})", def_id);

self.encode_ident_span(def_id, item.ident);
// Must be at top of function, so ident span encoding happens early
// Otherwise later operations panic with 'Missing ident span ...'
self.encode_info_for_item_foreign_item_common(def_id, item.attrs, item.ident);

record!(self.tables.kind[def_id] <- match item.kind {
hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic,
Expand Down Expand Up @@ -1314,9 +1321,6 @@ impl EncodeContext<'a, 'tcx> {
hir::ItemKind::ExternCrate(_) |
hir::ItemKind::Use(..) => bug!("cannot encode info for item {:?}", item),
});
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
record!(self.tables.attributes[def_id] <- item.attrs);
record!(self.tables.expn_that_defined[def_id] <- self.tcx.expansion_that_defined(def_id));
// FIXME(eddyb) there should be a nicer way to do this.
match item.kind {
Expand Down Expand Up @@ -1351,9 +1355,6 @@ impl EncodeContext<'a, 'tcx> {
}
_ => {}
}
self.encode_stability(def_id);
self.encode_const_stability(def_id);
self.encode_deprecation(def_id);
match item.kind {
hir::ItemKind::Static(..)
| hir::ItemKind::Const(..)
Expand All @@ -1374,7 +1375,6 @@ impl EncodeContext<'a, 'tcx> {
record!(self.tables.impl_trait_ref[def_id] <- trait_ref);
}
}
self.encode_inherent_implementations(def_id);
match item.kind {
hir::ItemKind::Enum(..)
| hir::ItemKind::Struct(..)
Expand All @@ -1394,9 +1394,7 @@ impl EncodeContext<'a, 'tcx> {
| hir::ItemKind::OpaqueTy(..)
| hir::ItemKind::Trait(..)
| hir::ItemKind::TraitAlias(..) => {
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_bounds(def_id);
}
_ => {}
}
Expand Down Expand Up @@ -1486,9 +1484,7 @@ impl EncodeContext<'a, 'tcx> {
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(qualifs, const_data));
record!(self.tables.span[def_id.to_def_id()] <- self.tcx.def_span(def_id));
self.encode_item_type(def_id.to_def_id());
self.encode_generics(def_id.to_def_id());
self.encode_explicit_predicates(def_id.to_def_id());
self.encode_inferred_outlives(def_id.to_def_id());
self.encode_bounds(def_id.to_def_id());
self.encode_optimized_mir(def_id);
self.encode_promoted_mir(def_id);
}
Expand Down Expand Up @@ -1703,6 +1699,8 @@ impl EncodeContext<'a, 'tcx> {

debug!("EncodeContext::encode_info_for_foreign_item({:?})", def_id);

self.encode_info_for_item_foreign_item_common(def_id, nitem.attrs, nitem.ident);

record!(self.tables.kind[def_id] <- match nitem.kind {
hir::ForeignItemKind::Fn(_, ref names, _) => {
let data = FnData {
Expand All @@ -1720,22 +1718,12 @@ impl EncodeContext<'a, 'tcx> {
hir::ForeignItemKind::Static(_, hir::Mutability::Not) => EntryKind::ForeignImmStatic,
hir::ForeignItemKind::Type => EntryKind::ForeignType,
});
record!(self.tables.visibility[def_id] <- self.tcx.visibility(def_id));
record!(self.tables.span[def_id] <- nitem.span);
record!(self.tables.attributes[def_id] <- nitem.attrs);
self.encode_ident_span(def_id, nitem.ident);
self.encode_stability(def_id);
self.encode_const_stability(def_id);
self.encode_deprecation(def_id);
self.encode_item_type(def_id);
self.encode_inherent_implementations(def_id);
if let hir::ForeignItemKind::Fn(..) = nitem.kind {
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
self.encode_variances_of(def_id);
}
self.encode_generics(def_id);
self.encode_explicit_predicates(def_id);
self.encode_inferred_outlives(def_id);
self.encode_bounds(def_id);
}
}

Expand Down