Skip to content

Commit 618138b

Browse files
committed
Store fn constness in impl_constness.
1 parent f2bf484 commit 618138b

File tree

3 files changed

+32
-49
lines changed

3 files changed

+32
-49
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,9 +1454,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
14541454
// don't serialize constness for tuple variant and tuple struct constructors.
14551455
fn is_const_fn_raw(self, id: DefIndex) -> bool {
14561456
let constness = match self.kind(id) {
1457-
EntryKind::AssocFn(data) => data.decode(self).fn_data.constness,
1458-
EntryKind::Fn(data) => data.decode(self).constness,
1459-
EntryKind::ForeignFn(data) => data.decode(self).constness,
1457+
EntryKind::AssocFn(_) | EntryKind::Fn | EntryKind::ForeignFn => {
1458+
self.root.tables.impl_constness.get(self, id).unwrap().decode(self)
1459+
}
14601460
EntryKind::Variant(..) | EntryKind::Struct(..) => hir::Constness::Const,
14611461
_ => hir::Constness::NotConst,
14621462
};
@@ -1465,7 +1465,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
14651465

14661466
fn is_foreign_item(self, id: DefIndex) -> bool {
14671467
match self.kind(id) {
1468-
EntryKind::ForeignStatic | EntryKind::ForeignFn(_) => true,
1468+
EntryKind::ForeignStatic | EntryKind::ForeignFn => true,
14691469
_ => false,
14701470
}
14711471
}

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,22 +1195,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
11951195
record!(self.tables.rendered_const[def_id] <- rendered);
11961196
}
11971197
ty::AssocKind::Fn => {
1198-
let fn_data = if let hir::TraitItemKind::Fn(m_sig, m) = &ast_item.kind {
1199-
match *m {
1200-
hir::TraitFn::Required(ref names) => {
1201-
record!(self.tables.fn_arg_names[def_id] <- *names)
1202-
}
1203-
hir::TraitFn::Provided(body) => {
1204-
record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body))
1205-
}
1206-
};
1207-
record!(self.tables.asyncness[def_id] <- m_sig.header.asyncness);
1208-
FnData { constness: hir::Constness::NotConst }
1209-
} else {
1210-
bug!()
1198+
let hir::TraitItemKind::Fn(m_sig, m) = &ast_item.kind else { bug!() };
1199+
match *m {
1200+
hir::TraitFn::Required(ref names) => {
1201+
record!(self.tables.fn_arg_names[def_id] <- *names)
1202+
}
1203+
hir::TraitFn::Provided(body) => {
1204+
record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body))
1205+
}
12111206
};
1207+
record!(self.tables.asyncness[def_id] <- m_sig.header.asyncness);
1208+
record!(self.tables.impl_constness[def_id] <- hir::Constness::NotConst);
12121209
record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData {
1213-
fn_data,
12141210
container,
12151211
has_self: trait_item.fn_has_self_parameter,
12161212
})));
@@ -1265,22 +1261,17 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12651261
}
12661262
}
12671263
ty::AssocKind::Fn => {
1268-
let fn_data = if let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind {
1269-
record!(self.tables.asyncness[def_id] <- sig.header.asyncness);
1270-
record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
1271-
FnData {
1272-
// Can be inside `impl const Trait`, so using sig.header.constness is not reliable
1273-
constness: if self.tcx.is_const_fn_raw(def_id) {
1274-
hir::Constness::Const
1275-
} else {
1276-
hir::Constness::NotConst
1277-
},
1278-
}
1264+
let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind else { bug!() };
1265+
record!(self.tables.asyncness[def_id] <- sig.header.asyncness);
1266+
record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
1267+
// Can be inside `impl const Trait`, so using sig.header.constness is not reliable
1268+
let constness = if self.tcx.is_const_fn_raw(def_id) {
1269+
hir::Constness::Const
12791270
} else {
1280-
bug!()
1271+
hir::Constness::NotConst
12811272
};
1273+
record!(self.tables.impl_constness[def_id] <- constness);
12821274
record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData {
1283-
fn_data,
12841275
container,
12851276
has_self: impl_item.fn_has_self_parameter,
12861277
})));
@@ -1402,9 +1393,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14021393
hir::ItemKind::Fn(ref sig, .., body) => {
14031394
record!(self.tables.asyncness[def_id] <- sig.header.asyncness);
14041395
record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
1405-
let data = FnData { constness: sig.header.constness };
1406-
1407-
EntryKind::Fn(self.lazy(data))
1396+
record!(self.tables.impl_constness[def_id] <- sig.header.constness);
1397+
EntryKind::Fn
14081398
}
14091399
hir::ItemKind::Macro(ref macro_def, _) => {
14101400
EntryKind::MacroDef(self.lazy(&*macro_def.body), macro_def.macro_rules)
@@ -1897,14 +1887,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18971887
hir::ForeignItemKind::Fn(_, ref names, _) => {
18981888
record!(self.tables.asyncness[def_id] <- hir::IsAsync::NotAsync);
18991889
record!(self.tables.fn_arg_names[def_id] <- *names);
1900-
let data = FnData {
1901-
constness: if self.tcx.is_const_fn_raw(def_id) {
1902-
hir::Constness::Const
1903-
} else {
1904-
hir::Constness::NotConst
1905-
},
1890+
let constness = if self.tcx.is_const_fn_raw(def_id) {
1891+
hir::Constness::Const
1892+
} else {
1893+
hir::Constness::NotConst
19061894
};
1907-
record!(self.tables.kind[def_id] <- EntryKind::ForeignFn(self.lazy(data)));
1895+
record!(self.tables.impl_constness[def_id] <- constness);
1896+
record!(self.tables.kind[def_id] <- EntryKind::ForeignFn);
19081897
}
19091898
hir::ForeignItemKind::Static(..) => {
19101899
record!(self.tables.kind[def_id] <- EntryKind::ForeignStatic);

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ enum EntryKind {
353353
Variant(Lazy<VariantData>),
354354
Struct(Lazy<VariantData>),
355355
Union(Lazy<VariantData>),
356-
Fn(Lazy<FnData>),
357-
ForeignFn(Lazy<FnData>),
356+
Fn,
357+
ForeignFn,
358358
Mod(Lazy<[ModChild]>),
359359
MacroDef(Lazy<ast::MacArgs>, /*macro_rules*/ bool),
360360
ProcMacro(MacroKind),
@@ -368,11 +368,6 @@ enum EntryKind {
368368
TraitAlias,
369369
}
370370

371-
#[derive(MetadataEncodable, MetadataDecodable)]
372-
struct FnData {
373-
constness: hir::Constness,
374-
}
375-
376371
#[derive(TyEncodable, TyDecodable)]
377372
struct VariantData {
378373
ctor_kind: CtorKind,
@@ -430,7 +425,6 @@ impl AssocContainer {
430425

431426
#[derive(MetadataEncodable, MetadataDecodable)]
432427
struct AssocFnData {
433-
fn_data: FnData,
434428
container: AssocContainer,
435429
has_self: bool,
436430
}

0 commit comments

Comments
 (0)