Skip to content

Commit

Permalink
Auto merge of #74887 - Mark-Simulacrum:cache-non-exhaustive, r=petroc…
Browse files Browse the repository at this point in the history
…henkov

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.
  • Loading branch information
bors committed Jul 29, 2020
2 parents 517385b + 13ad232 commit 10c3757
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
7 changes: 3 additions & 4 deletions src/librustc_metadata/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

fn get_variant(
&self,
tcx: TyCtxt<'tcx>,
kind: &EntryKind,
index: DefIndex,
parent_did: DefId,
Expand All @@ -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,
Expand All @@ -826,6 +824,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
adt_kind,
parent_did,
false,
data.is_non_exhaustive,
)
}

Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_metadata/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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(..) => {
Expand All @@ -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, .. } => {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_metadata/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
12 changes: 3 additions & 9 deletions src/librustc_middle/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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 = {:?},
Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
}),
)
}

Expand Down

0 comments on commit 10c3757

Please sign in to comment.