Skip to content

Commit 3c790b3

Browse files
committed
Encode fn_sig separately.
Closures do not have a `fn_sig`, so no reason to encode one.
1 parent 3d9ba07 commit 3c790b3

File tree

1 file changed

+91
-38
lines changed

1 file changed

+91
-38
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 91 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,82 @@ fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) ->
11931193
}
11941194
}
11951195

1196+
fn should_encode_fn_sig(def_kind: DefKind) -> bool {
1197+
match def_kind {
1198+
DefKind::Fn | DefKind::AssocFn | DefKind::Ctor(_, CtorKind::Fn) => true,
1199+
1200+
DefKind::Struct
1201+
| DefKind::Union
1202+
| DefKind::Enum
1203+
| DefKind::Variant
1204+
| DefKind::Field
1205+
| DefKind::Const
1206+
| DefKind::Static(..)
1207+
| DefKind::Ctor(..)
1208+
| DefKind::TyAlias
1209+
| DefKind::OpaqueTy
1210+
| DefKind::ImplTraitPlaceholder
1211+
| DefKind::ForeignTy
1212+
| DefKind::Impl { .. }
1213+
| DefKind::AssocConst
1214+
| DefKind::Closure
1215+
| DefKind::Generator
1216+
| DefKind::ConstParam
1217+
| DefKind::AnonConst
1218+
| DefKind::InlineConst
1219+
| DefKind::AssocTy
1220+
| DefKind::TyParam
1221+
| DefKind::Trait
1222+
| DefKind::TraitAlias
1223+
| DefKind::Mod
1224+
| DefKind::ForeignMod
1225+
| DefKind::Macro(..)
1226+
| DefKind::Use
1227+
| DefKind::LifetimeParam
1228+
| DefKind::GlobalAsm
1229+
| DefKind::ExternCrate => false,
1230+
}
1231+
}
1232+
1233+
fn should_encode_constness(def_kind: DefKind) -> bool {
1234+
match def_kind {
1235+
DefKind::Fn
1236+
| DefKind::AssocFn
1237+
| DefKind::Closure
1238+
| DefKind::Impl { of_trait: true }
1239+
| DefKind::Variant
1240+
| DefKind::Ctor(..) => true,
1241+
1242+
DefKind::Struct
1243+
| DefKind::Union
1244+
| DefKind::Enum
1245+
| DefKind::Field
1246+
| DefKind::Const
1247+
| DefKind::AssocConst
1248+
| DefKind::AnonConst
1249+
| DefKind::Static(..)
1250+
| DefKind::TyAlias
1251+
| DefKind::OpaqueTy
1252+
| DefKind::Impl { of_trait: false }
1253+
| DefKind::ImplTraitPlaceholder
1254+
| DefKind::ForeignTy
1255+
| DefKind::Generator
1256+
| DefKind::ConstParam
1257+
| DefKind::InlineConst
1258+
| DefKind::AssocTy
1259+
| DefKind::TyParam
1260+
| DefKind::Trait
1261+
| DefKind::TraitAlias
1262+
| DefKind::Mod
1263+
| DefKind::ForeignMod
1264+
| DefKind::Macro(..)
1265+
| DefKind::Use
1266+
| DefKind::LifetimeParam
1267+
| DefKind::GlobalAsm
1268+
| DefKind::ExternCrate => false,
1269+
}
1270+
}
1271+
11961272
fn should_encode_const(def_kind: DefKind) -> bool {
11971273
match def_kind {
11981274
DefKind::Const | DefKind::AssocConst | DefKind::AnonConst | DefKind::InlineConst => true,
@@ -1305,6 +1381,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13051381
let v = self.tcx.variances_of(def_id);
13061382
record_array!(self.tables.variances_of[def_id] <- v);
13071383
}
1384+
if should_encode_fn_sig(def_kind) {
1385+
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
1386+
}
13081387
if should_encode_generics(def_kind) {
13091388
let g = tcx.generics_of(def_id);
13101389
record!(self.tables.generics_of[def_id] <- g);
@@ -1315,11 +1394,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13151394
if should_encode_type(tcx, local_id, def_kind) {
13161395
record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id));
13171396
}
1397+
if should_encode_constness(def_kind) {
1398+
self.tables.constness.set_some(def_id.index, self.tcx.constness(def_id));
1399+
}
13181400
if let DefKind::Fn | DefKind::AssocFn = def_kind {
13191401
self.tables.asyncness.set_some(def_id.index, tcx.asyncness(def_id));
1320-
self.tables.constness.set_some(def_id.index, tcx.constness(def_id));
13211402
record_array!(self.tables.fn_arg_names[def_id] <- tcx.fn_arg_names(def_id));
1322-
record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
13231403
self.tables.is_intrinsic.set(def_id.index, tcx.is_intrinsic(def_id));
13241404
}
13251405
if let DefKind::TyParam = def_kind {
@@ -1333,6 +1413,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13331413
record!(self.tables.super_predicates_of[def_id] <- self.tcx.super_predicates_of(def_id));
13341414
record!(self.tables.implied_predicates_of[def_id] <- self.tcx.implied_predicates_of(def_id));
13351415
}
1416+
if let DefKind::Generator = def_kind {
1417+
self.encode_info_for_generator(local_id);
1418+
}
13361419
if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind {
13371420
self.encode_info_for_adt(local_id);
13381421
}
@@ -1396,16 +1479,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13961479
};
13971480
record!(self.tables.variant_data[variant.def_id] <- data);
13981481

1399-
self.tables.constness.set_some(variant.def_id.index, hir::Constness::Const);
14001482
record_array!(self.tables.associated_item_or_field_def_ids[variant.def_id] <- variant.fields.iter().map(|f| {
14011483
assert!(f.did.is_local());
14021484
f.did.index
14031485
}));
14041486

14051487
if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor {
1406-
self.tables.constness.set_some(ctor_def_id.index, hir::Constness::Const);
14071488
let fn_sig = tcx.fn_sig(ctor_def_id);
1408-
record!(self.tables.fn_sig[ctor_def_id] <- fn_sig);
14091489
// FIXME only encode signature for ctor_def_id
14101490
record!(self.tables.fn_sig[variant.def_id] <- fn_sig);
14111491
}
@@ -1627,9 +1707,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16271707
matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias { .. }),
16281708
);
16291709
}
1630-
hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => {
1710+
hir::ItemKind::Impl(hir::Impl { defaultness, .. }) => {
16311711
self.tables.defaultness.set_some(def_id.index, *defaultness);
1632-
self.tables.constness.set_some(def_id.index, *constness);
16331712
self.tables.impl_polarity.set_some(def_id.index, self.tcx.impl_polarity(def_id));
16341713

16351714
if let Some(trait_ref) = self.tcx.impl_trait_ref(def_id) {
@@ -1689,28 +1768,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16891768
}
16901769

16911770
#[instrument(level = "debug", skip(self))]
1692-
fn encode_info_for_closure(&mut self, def_id: LocalDefId) {
1693-
// NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic,
1694-
// including on the signature, which is inferred in `typeck`.
1771+
fn encode_info_for_generator(&mut self, def_id: LocalDefId) {
16951772
let typeck_result: &'tcx ty::TypeckResults<'tcx> = self.tcx.typeck(def_id);
1696-
let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id);
1697-
let ty = typeck_result.node_type(hir_id);
1698-
match ty.kind() {
1699-
ty::Generator(..) => {
1700-
let data = self.tcx.generator_kind(def_id).unwrap();
1701-
let generator_diagnostic_data = typeck_result.get_generator_diagnostic_data();
1702-
record!(self.tables.generator_kind[def_id.to_def_id()] <- data);
1703-
record!(self.tables.generator_diagnostic_data[def_id.to_def_id()] <- generator_diagnostic_data);
1704-
}
1705-
1706-
ty::Closure(_, substs) => {
1707-
let constness = self.tcx.constness(def_id.to_def_id());
1708-
self.tables.constness.set_some(def_id.to_def_id().index, constness);
1709-
record!(self.tables.fn_sig[def_id.to_def_id()] <- ty::EarlyBinder::bind(substs.as_closure().sig()));
1710-
}
1711-
1712-
_ => bug!("closure that is neither generator nor closure"),
1713-
}
1773+
let data = self.tcx.generator_kind(def_id).unwrap();
1774+
let generator_diagnostic_data = typeck_result.get_generator_diagnostic_data();
1775+
record!(self.tables.generator_kind[def_id.to_def_id()] <- data);
1776+
record!(self.tables.generator_diagnostic_data[def_id.to_def_id()] <- generator_diagnostic_data);
17141777
}
17151778

17161779
fn encode_native_libraries(&mut self) -> LazyArray<NativeLib> {
@@ -2059,10 +2122,6 @@ impl<'a, 'tcx> Visitor<'tcx> for EncodeContext<'a, 'tcx> {
20592122
fn nested_visit_map(&mut self) -> Self::Map {
20602123
self.tcx.hir()
20612124
}
2062-
fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
2063-
intravisit::walk_expr(self, ex);
2064-
self.encode_info_for_expr(ex);
2065-
}
20662125
fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
20672126
intravisit::walk_item(self, item);
20682127
self.encode_info_for_item(item);
@@ -2087,12 +2146,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20872146
}
20882147
}
20892148
}
2090-
2091-
fn encode_info_for_expr(&mut self, expr: &hir::Expr<'_>) {
2092-
if let hir::ExprKind::Closure(closure) = expr.kind {
2093-
self.encode_info_for_closure(closure.def_id);
2094-
}
2095-
}
20962149
}
20972150

20982151
/// Used to prefetch queries which will be needed later by metadata encoding.

0 commit comments

Comments
 (0)