Skip to content

Encode ADT flags rather than recreating them from scratch at decoding time #127466

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion compiler/rustc_feature/src/builtin_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
// Internal attributes: Type system related:
// ==========================================================================

gated!(fundamental, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::Yes, experimental!(fundamental)),
gated!(fundamental, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No, experimental!(fundamental)),
gated!(
may_dangle, Normal, template!(Word), WarnFollowing,
EncodeCrossCrate::No, dropck_eyepatch,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,7 @@ fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
}
_ => bug!("{:?} is not an ADT", item.owner_id.def_id),
};
tcx.mk_adt_def(def_id.to_def_id(), kind, variants, repr, is_anonymous)
tcx.mk_adt_def(def_id, kind, variants, repr, is_anonymous)
}

fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
DefKind::Union => ty::AdtKind::Union,
_ => bug!("get_adt_def called on a non-ADT {:?}", did),
};

let repr = self.root.tables.repr_options.get(self, item_id).unwrap().decode(self);
let flags = self.root.tables.adt_flags.get(self, item_id).unwrap().decode(self);

let mut variants: Vec<_> = if let ty::AdtKind::Enum = adt_kind {
self.root
Expand All @@ -1174,12 +1176,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

variants.sort_by_key(|(idx, _)| *idx);

tcx.mk_adt_def(
tcx.mk_adt_def_from_flags(
did,
adt_kind,
variants.into_iter().map(|(_, variant)| variant).collect(),
flags,
repr,
false,
)
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let tcx = self.tcx;
let adt_def = tcx.adt_def(def_id);
record!(self.tables.repr_options[def_id] <- adt_def.repr());
record!(self.tables.adt_flags[def_id] <- adt_def.flags());

let params_in_repr = self.tcx.params_in_repr(def_id);
record!(self.tables.params_in_repr[def_id] <- params_in_repr);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ define_tables! {
expn_that_defined: Table<DefIndex, LazyValue<ExpnId>>,
params_in_repr: Table<DefIndex, LazyValue<BitSet<u32>>>,
repr_options: Table<DefIndex, LazyValue<ReprOptions>>,
adt_flags: Table<DefIndex, LazyValue<ty::AdtFlags>>,
// `def_keys` and `def_path_hashes` represent a lazy version of a
// `DefPathTable`. This allows us to avoid deserializing an entire
// `DefPathTable` up front, since we may only ever use a few
Expand Down
14 changes: 12 additions & 2 deletions compiler/rustc_middle/src/ty/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_data_structures::stable_hasher::HashingControls;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_errors::ErrorGuaranteed;
use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::{self as hir, LangItem};
use rustc_index::{IndexSlice, IndexVec};
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
Expand Down Expand Up @@ -253,15 +253,25 @@ impl Into<DataTypeKind> for AdtKind {
}

impl AdtDefData {
pub(super) fn new_from_flags(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this ctor is kinda redundant, i could also just make all the fields pub(super) -- OR just pull TyCtxt::mk_adt_def_from_flags/TyCtxt::mk_adt_def into a new impl in this module. I don't really care.

did: DefId,
variants: IndexVec<VariantIdx, VariantDef>,
flags: AdtFlags,
repr: ReprOptions,
) -> Self {
AdtDefData { did, variants, flags, repr }
}

/// Creates a new `AdtDefData`.
pub(super) fn new(
tcx: TyCtxt<'_>,
did: DefId,
did: LocalDefId,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting this to local so we enforce that AdtDefData::new is called only on local def ids.

kind: AdtKind,
variants: IndexVec<VariantIdx, VariantDef>,
repr: ReprOptions,
is_anonymous: bool,
) -> Self {
let did = did.to_def_id();
debug!(
"AdtDef::new({:?}, {:?}, {:?}, {:?}, {:?})",
did, kind, variants, repr, is_anonymous
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,9 +1408,19 @@ impl<'tcx> TyCtxt<'tcx> {
self.arena.alloc(Steal::new(promoted))
}

pub fn mk_adt_def(
pub fn mk_adt_def_from_flags(
self,
did: DefId,
variants: IndexVec<VariantIdx, ty::VariantDef>,
flags: ty::AdtFlags,
repr: ReprOptions,
) -> ty::AdtDef<'tcx> {
self.mk_adt_def_from_data(ty::AdtDefData::new_from_flags(did, variants, flags, repr))
}

pub fn mk_adt_def(
self,
did: LocalDefId,
kind: AdtKind,
variants: IndexVec<VariantIdx, ty::VariantDef>,
repr: ReprOptions,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/parameterized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ trivially_parameterized_over_tcx! {
crate::middle::lib_features::FeatureStability,
crate::middle::resolve_bound_vars::ObjectLifetimeDefault,
crate::mir::ConstQualifs,
ty::AdtFlags,
ty::AssocItemContainer,
ty::Asyncness,
ty::DeducedParamAttrs,
Expand Down
Loading