Skip to content

Commit 10c3757

Browse files
committed
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.
2 parents 517385b + 13ad232 commit 10c3757

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

Diff for: src/librustc_metadata/rmeta/decoder.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
780780

781781
fn get_variant(
782782
&self,
783-
tcx: TyCtxt<'tcx>,
784783
kind: &EntryKind,
785784
index: DefIndex,
786785
parent_did: DefId,
@@ -805,7 +804,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
805804
let ctor_did = data.ctor.map(|index| self.local_def_id(index));
806805

807806
ty::VariantDef::new(
808-
tcx,
809807
self.item_ident(index, sess),
810808
variant_did,
811809
ctor_did,
@@ -826,6 +824,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
826824
adt_kind,
827825
parent_did,
828826
false,
827+
data.is_non_exhaustive,
829828
)
830829
}
831830

@@ -847,10 +846,10 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
847846
.get(self, item_id)
848847
.unwrap_or(Lazy::empty())
849848
.decode(self)
850-
.map(|index| self.get_variant(tcx, &self.kind(index), index, did, tcx.sess))
849+
.map(|index| self.get_variant(&self.kind(index), index, did, tcx.sess))
851850
.collect()
852851
} else {
853-
std::iter::once(self.get_variant(tcx, &kind, item_id, did, tcx.sess)).collect()
852+
std::iter::once(self.get_variant(&kind, item_id, did, tcx.sess)).collect()
854853
};
855854

856855
tcx.alloc_adt_def(did, adt_kind, variants, repr)

Diff for: src/librustc_metadata/rmeta/encoder.rs

+5
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,7 @@ impl EncodeContext<'a, 'tcx> {
738738
ctor_kind: variant.ctor_kind,
739739
discr: variant.discr,
740740
ctor: variant.ctor_def_id.map(|did| did.index),
741+
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
741742
};
742743

743744
let enum_id = tcx.hir().as_local_hir_id(def.did.expect_local());
@@ -782,6 +783,7 @@ impl EncodeContext<'a, 'tcx> {
782783
ctor_kind: variant.ctor_kind,
783784
discr: variant.discr,
784785
ctor: Some(def_id.index),
786+
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
785787
};
786788

787789
// Variant constructors have the same visibility as the parent enums, unless marked as
@@ -886,6 +888,7 @@ impl EncodeContext<'a, 'tcx> {
886888
ctor_kind: variant.ctor_kind,
887889
discr: variant.discr,
888890
ctor: Some(def_id.index),
891+
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
889892
};
890893

891894
let struct_id = tcx.hir().as_local_hir_id(adt_def.did.expect_local());
@@ -1235,6 +1238,7 @@ impl EncodeContext<'a, 'tcx> {
12351238
ctor_kind: variant.ctor_kind,
12361239
discr: variant.discr,
12371240
ctor,
1241+
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
12381242
}), adt_def.repr)
12391243
}
12401244
hir::ItemKind::Union(..) => {
@@ -1245,6 +1249,7 @@ impl EncodeContext<'a, 'tcx> {
12451249
ctor_kind: variant.ctor_kind,
12461250
discr: variant.discr,
12471251
ctor: None,
1252+
is_non_exhaustive: variant.is_field_list_non_exhaustive(),
12481253
}), adt_def.repr)
12491254
}
12501255
hir::ItemKind::Impl { defaultness, .. } => {

Diff for: src/librustc_metadata/rmeta/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ struct VariantData {
346346
discr: ty::VariantDiscr,
347347
/// If this is unit or tuple-variant/struct, then this is the index of the ctor id.
348348
ctor: Option<DefIndex>,
349+
is_non_exhaustive: bool,
349350
}
350351

351352
#[derive(RustcEncodable, RustcDecodable)]

Diff for: src/librustc_middle/ty/mod.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,6 @@ impl<'tcx> VariantDef {
20462046
/// If someone speeds up attribute loading to not be a performance concern, they can
20472047
/// remove this hack and use the constructor `DefId` everywhere.
20482048
pub fn new(
2049-
tcx: TyCtxt<'tcx>,
20502049
ident: Ident,
20512050
variant_did: Option<DefId>,
20522051
ctor_def_id: Option<DefId>,
@@ -2056,6 +2055,7 @@ impl<'tcx> VariantDef {
20562055
adt_kind: AdtKind,
20572056
parent_did: DefId,
20582057
recovered: bool,
2058+
is_field_list_non_exhaustive: bool,
20592059
) -> Self {
20602060
debug!(
20612061
"VariantDef::new(ident = {:?}, variant_did = {:?}, ctor_def_id = {:?}, discr = {:?},
@@ -2064,14 +2064,8 @@ impl<'tcx> VariantDef {
20642064
);
20652065

20662066
let mut flags = VariantFlags::NO_VARIANT_FLAGS;
2067-
if adt_kind == AdtKind::Struct && tcx.has_attr(parent_did, sym::non_exhaustive) {
2068-
debug!("found non-exhaustive field list for {:?}", parent_did);
2069-
flags = flags | VariantFlags::IS_FIELD_LIST_NON_EXHAUSTIVE;
2070-
} else if let Some(variant_did) = variant_did {
2071-
if tcx.has_attr(variant_did, sym::non_exhaustive) {
2072-
debug!("found non-exhaustive field list for {:?}", variant_did);
2073-
flags = flags | VariantFlags::IS_FIELD_LIST_NON_EXHAUSTIVE;
2074-
}
2067+
if is_field_list_non_exhaustive {
2068+
flags |= VariantFlags::IS_FIELD_LIST_NON_EXHAUSTIVE;
20752069
}
20762070

20772071
VariantDef {

Diff for: src/librustc_typeck/collect.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,6 @@ fn convert_variant(
858858
_ => false,
859859
};
860860
ty::VariantDef::new(
861-
tcx,
862861
ident,
863862
variant_did.map(LocalDefId::to_def_id),
864863
ctor_did.map(LocalDefId::to_def_id),
@@ -868,6 +867,10 @@ fn convert_variant(
868867
adt_kind,
869868
parent_did.to_def_id(),
870869
recovered,
870+
adt_kind == AdtKind::Struct && tcx.has_attr(parent_did.to_def_id(), sym::non_exhaustive)
871+
|| variant_did.map_or(false, |variant_did| {
872+
tcx.has_attr(variant_did.to_def_id(), sym::non_exhaustive)
873+
}),
871874
)
872875
}
873876

0 commit comments

Comments
 (0)