Skip to content

Commit

Permalink
Store fn constness in impl_constness.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Mar 31, 2022
1 parent f2bf484 commit 618138b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 49 deletions.
8 changes: 4 additions & 4 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,9 +1454,9 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
// don't serialize constness for tuple variant and tuple struct constructors.
fn is_const_fn_raw(self, id: DefIndex) -> bool {
let constness = match self.kind(id) {
EntryKind::AssocFn(data) => data.decode(self).fn_data.constness,
EntryKind::Fn(data) => data.decode(self).constness,
EntryKind::ForeignFn(data) => data.decode(self).constness,
EntryKind::AssocFn(_) | EntryKind::Fn | EntryKind::ForeignFn => {
self.root.tables.impl_constness.get(self, id).unwrap().decode(self)
}
EntryKind::Variant(..) | EntryKind::Struct(..) => hir::Constness::Const,
_ => hir::Constness::NotConst,
};
Expand All @@ -1465,7 +1465,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {

fn is_foreign_item(self, id: DefIndex) -> bool {
match self.kind(id) {
EntryKind::ForeignStatic | EntryKind::ForeignFn(_) => true,
EntryKind::ForeignStatic | EntryKind::ForeignFn => true,
_ => false,
}
}
Expand Down
63 changes: 26 additions & 37 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,22 +1195,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
record!(self.tables.rendered_const[def_id] <- rendered);
}
ty::AssocKind::Fn => {
let fn_data = if let hir::TraitItemKind::Fn(m_sig, m) = &ast_item.kind {
match *m {
hir::TraitFn::Required(ref names) => {
record!(self.tables.fn_arg_names[def_id] <- *names)
}
hir::TraitFn::Provided(body) => {
record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body))
}
};
record!(self.tables.asyncness[def_id] <- m_sig.header.asyncness);
FnData { constness: hir::Constness::NotConst }
} else {
bug!()
let hir::TraitItemKind::Fn(m_sig, m) = &ast_item.kind else { bug!() };
match *m {
hir::TraitFn::Required(ref names) => {
record!(self.tables.fn_arg_names[def_id] <- *names)
}
hir::TraitFn::Provided(body) => {
record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body))
}
};
record!(self.tables.asyncness[def_id] <- m_sig.header.asyncness);
record!(self.tables.impl_constness[def_id] <- hir::Constness::NotConst);
record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData {
fn_data,
container,
has_self: trait_item.fn_has_self_parameter,
})));
Expand Down Expand Up @@ -1265,22 +1261,17 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
}
ty::AssocKind::Fn => {
let fn_data = if let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind {
record!(self.tables.asyncness[def_id] <- sig.header.asyncness);
record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
FnData {
// Can be inside `impl const Trait`, so using sig.header.constness is not reliable
constness: if self.tcx.is_const_fn_raw(def_id) {
hir::Constness::Const
} else {
hir::Constness::NotConst
},
}
let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind else { bug!() };
record!(self.tables.asyncness[def_id] <- sig.header.asyncness);
record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
// Can be inside `impl const Trait`, so using sig.header.constness is not reliable
let constness = if self.tcx.is_const_fn_raw(def_id) {
hir::Constness::Const
} else {
bug!()
hir::Constness::NotConst
};
record!(self.tables.impl_constness[def_id] <- constness);
record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData {
fn_data,
container,
has_self: impl_item.fn_has_self_parameter,
})));
Expand Down Expand Up @@ -1402,9 +1393,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
hir::ItemKind::Fn(ref sig, .., body) => {
record!(self.tables.asyncness[def_id] <- sig.header.asyncness);
record!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body));
let data = FnData { constness: sig.header.constness };

EntryKind::Fn(self.lazy(data))
record!(self.tables.impl_constness[def_id] <- sig.header.constness);
EntryKind::Fn
}
hir::ItemKind::Macro(ref macro_def, _) => {
EntryKind::MacroDef(self.lazy(&*macro_def.body), macro_def.macro_rules)
Expand Down Expand Up @@ -1897,14 +1887,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
hir::ForeignItemKind::Fn(_, ref names, _) => {
record!(self.tables.asyncness[def_id] <- hir::IsAsync::NotAsync);
record!(self.tables.fn_arg_names[def_id] <- *names);
let data = FnData {
constness: if self.tcx.is_const_fn_raw(def_id) {
hir::Constness::Const
} else {
hir::Constness::NotConst
},
let constness = if self.tcx.is_const_fn_raw(def_id) {
hir::Constness::Const
} else {
hir::Constness::NotConst
};
record!(self.tables.kind[def_id] <- EntryKind::ForeignFn(self.lazy(data)));
record!(self.tables.impl_constness[def_id] <- constness);
record!(self.tables.kind[def_id] <- EntryKind::ForeignFn);
}
hir::ForeignItemKind::Static(..) => {
record!(self.tables.kind[def_id] <- EntryKind::ForeignStatic);
Expand Down
10 changes: 2 additions & 8 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ enum EntryKind {
Variant(Lazy<VariantData>),
Struct(Lazy<VariantData>),
Union(Lazy<VariantData>),
Fn(Lazy<FnData>),
ForeignFn(Lazy<FnData>),
Fn,
ForeignFn,
Mod(Lazy<[ModChild]>),
MacroDef(Lazy<ast::MacArgs>, /*macro_rules*/ bool),
ProcMacro(MacroKind),
Expand All @@ -368,11 +368,6 @@ enum EntryKind {
TraitAlias,
}

#[derive(MetadataEncodable, MetadataDecodable)]
struct FnData {
constness: hir::Constness,
}

#[derive(TyEncodable, TyDecodable)]
struct VariantData {
ctor_kind: CtorKind,
Expand Down Expand Up @@ -430,7 +425,6 @@ impl AssocContainer {

#[derive(MetadataEncodable, MetadataDecodable)]
struct AssocFnData {
fn_data: FnData,
container: AssocContainer,
has_self: bool,
}
Expand Down

0 comments on commit 618138b

Please sign in to comment.