Auto merge of #74887 - Mark-Simulacrum:cache-non-exhaustive, r=petrochenkov

Cache non-exhaustive separately from attributes

This prevents cross-crate attribute loading from metadata just for non_exhaustive checking; cross-crate attribute loading implies disk reading and is relatively slow.
This commit is contained in:
bors 2020-07-29 04:59:37 +00:00
commit 10c375700c
5 changed files with 16 additions and 14 deletions

View File

@ -780,7 +780,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
fn get_variant(
&self,
tcx: TyCtxt<'tcx>,
kind: &EntryKind,
index: DefIndex,
parent_did: DefId,
@ -805,7 +804,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
let ctor_did = data.ctor.map(|index| self.local_def_id(index));
ty::VariantDef::new(
tcx,
self.item_ident(index, sess),
variant_did,
ctor_did,
@ -826,6 +824,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
adt_kind,
parent_did,
false,
data.is_non_exhaustive,
)
}
@ -847,10 +846,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
.get(self, item_id)
.unwrap_or(Lazy::empty())
.decode(self)
.map(|index| self.get_variant(tcx, &self.kind(index), index, did, tcx.sess))
.map(|index| self.get_variant(&self.kind(index), index, did, tcx.sess))
.collect()
} else {
std::iter::once(self.get_variant(tcx, &kind, item_id, did, tcx.sess)).collect()
std::iter::once(self.get_variant(&kind, item_id, did, tcx.sess)).collect()
};
tcx.alloc_adt_def(did, adt_kind, variants, repr)

View File

@ -738,6 +738,7 @@ impl EncodeContext<'a, 'tcx> {
ctor_kind: variant.ctor_kind,
discr: variant.discr,
ctor: variant.ctor_def_id.map(|did| did.index),
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
};
let enum_id = tcx.hir().as_local_hir_id(def.did.expect_local());
@ -782,6 +783,7 @@ impl EncodeContext<'a, 'tcx> {
ctor_kind: variant.ctor_kind,
discr: variant.discr,
ctor: Some(def_id.index),
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
};
// Variant constructors have the same visibility as the parent enums, unless marked as
@ -886,6 +888,7 @@ impl EncodeContext<'a, 'tcx> {
ctor_kind: variant.ctor_kind,
discr: variant.discr,
ctor: Some(def_id.index),
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
};
let struct_id = tcx.hir().as_local_hir_id(adt_def.did.expect_local());
@ -1235,6 +1238,7 @@ impl EncodeContext<'a, 'tcx> {
ctor_kind: variant.ctor_kind,
discr: variant.discr,
ctor,
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
}), adt_def.repr)
}
hir::ItemKind::Union(..) => {
@ -1245,6 +1249,7 @@ impl EncodeContext<'a, 'tcx> {
ctor_kind: variant.ctor_kind,
discr: variant.discr,
ctor: None,
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
}), adt_def.repr)
}
hir::ItemKind::Impl { defaultness, .. } => {

View File

@ -346,6 +346,7 @@ struct VariantData {
discr: ty::VariantDiscr,
/// If this is unit or tuple-variant/struct, then this is the index of the ctor id.
ctor: Option<DefIndex>,
is_non_exhaustive: bool,
}
#[derive(RustcEncodable, RustcDecodable)]

View File

@ -2046,7 +2046,6 @@ impl<'tcx> VariantDef {
/// If someone speeds up attribute loading to not be a performance concern, they can
/// remove this hack and use the constructor `DefId` everywhere.
pub fn new(
tcx: TyCtxt<'tcx>,
ident: Ident,
variant_did: Option<DefId>,
ctor_def_id: Option<DefId>,
@ -2056,6 +2055,7 @@ impl<'tcx> VariantDef {
adt_kind: AdtKind,
parent_did: DefId,
recovered: bool,
is_field_list_non_exhaustive: bool,
) -> Self {
debug!(
"VariantDef::new(ident = {:?}, variant_did = {:?}, ctor_def_id = {:?}, discr = {:?},
@ -2064,14 +2064,8 @@ impl<'tcx> VariantDef {
);
let mut flags = VariantFlags::NO_VARIANT_FLAGS;
if adt_kind == AdtKind::Struct && tcx.has_attr(parent_did, sym::non_exhaustive) {
debug!("found non-exhaustive field list for {:?}", parent_did);
flags = flags | VariantFlags::IS_FIELD_LIST_NON_EXHAUSTIVE;
} else if let Some(variant_did) = variant_did {
if tcx.has_attr(variant_did, sym::non_exhaustive) {
debug!("found non-exhaustive field list for {:?}", variant_did);
flags = flags | VariantFlags::IS_FIELD_LIST_NON_EXHAUSTIVE;
}
if is_field_list_non_exhaustive {
flags |= VariantFlags::IS_FIELD_LIST_NON_EXHAUSTIVE;
}
VariantDef {

View File

@ -858,7 +858,6 @@ fn convert_variant(
_ => false,
};
ty::VariantDef::new(
tcx,
ident,
variant_did.map(LocalDefId::to_def_id),
ctor_did.map(LocalDefId::to_def_id),
@ -868,6 +867,10 @@ fn convert_variant(
adt_kind,
parent_did.to_def_id(),
recovered,
adt_kind == AdtKind::Struct && tcx.has_attr(parent_did.to_def_id(), sym::non_exhaustive)
|| variant_did.map_or(false, |variant_did| {
tcx.has_attr(variant_did.to_def_id(), sym::non_exhaustive)
}),
)
}