Skip to content

Commit

Permalink
Add rendered_const table.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Feb 19, 2022
1 parent f8fd973 commit 7bacdb7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 35 deletions.
11 changes: 1 addition & 10 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Expand Up @@ -1193,7 +1193,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
let name = self.item_name(id);

let (kind, container, has_self) = match self.kind(id) {
EntryKind::AssocConst(container, _) => (ty::AssocKind::Const, container, false),
EntryKind::AssocConst(container) => (ty::AssocKind::Const, container, false),
EntryKind::AssocFn(data) => {
let data = data.decode(self);
(ty::AssocKind::Fn, data.container, data.has_self)
Expand Down Expand Up @@ -1411,15 +1411,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
tcx.arena.alloc_from_iter(self.root.exported_symbols.decode((self, tcx)))
}

fn get_rendered_const(self, id: DefIndex) -> String {
match self.kind(id) {
EntryKind::AnonConst(data)
| EntryKind::Const(data)
| EntryKind::AssocConst(_, data) => data.decode(self).0,
_ => bug!(),
}
}

fn get_macro(self, id: DefIndex, sess: &Session) -> MacroDef {
match self.kind(id) {
EntryKind::MacroDef(macro_def) => macro_def.decode((self, sess)),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Expand Up @@ -137,6 +137,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
impl_constness => { table }
coerce_unsized_info => { table }
mir_const_qualif => { table }
rendered_const => { table }

trait_def => { cdata.get_trait_def(def_id.index, tcx.sess) }
adt_def => { cdata.get_adt_def(def_id.index, tcx) }
Expand All @@ -154,7 +155,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
generator_kind => { cdata.generator_kind(def_id.index) }
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
fn_arg_names => { cdata.get_fn_param_names(tcx, def_id.index) }
rendered_const => { cdata.get_rendered_const(def_id.index) }
trait_of_item => { cdata.get_trait_of_item(def_id.index) }
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) }
Expand Down
29 changes: 13 additions & 16 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Expand Up @@ -1188,13 +1188,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
&(&self.tcx.hir() as &dyn intravisit::Map<'_>),
|s| s.print_trait_item(ast_item),
);
let rendered_const = self.lazy(RenderedConst(rendered));

record!(self.tables.kind[def_id] <- EntryKind::AssocConst(
container,
rendered_const,
));
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(container));
record!(self.tables.mir_const_qualif[def_id] <- mir::ConstQualifs::default());
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 {
Expand Down Expand Up @@ -1256,12 +1253,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
ty::AssocKind::Const => {
if let hir::ImplItemKind::Const(_, body_id) = ast_item.kind {
let qualifs = self.tcx.at(ast_item.span).mir_const_qualif(def_id);
let const_data = self.encode_rendered_const_for_body(body_id);

record!(self.tables.kind[def_id] <- EntryKind::AssocConst(
container,
self.encode_rendered_const_for_body(body_id))
);
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(container));
record!(self.tables.mir_const_qualif[def_id] <- qualifs);
record!(self.tables.rendered_const[def_id] <- const_data);
} else {
bug!()
}
Expand Down Expand Up @@ -1385,14 +1381,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
}

fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> Lazy<RenderedConst> {
fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> String {
let hir = self.tcx.hir();
let body = hir.body(body_id);
let rendered = rustc_hir_pretty::to_string(&(&hir as &dyn intravisit::Map<'_>), |s| {
rustc_hir_pretty::to_string(&(&hir as &dyn intravisit::Map<'_>), |s| {
s.print_expr(&body.value)
});
let rendered_const = &RenderedConst(rendered);
self.lazy(rendered_const)
})
}

fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'tcx>) {
Expand All @@ -1407,8 +1401,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic,
hir::ItemKind::Const(_, body_id) => {
let qualifs = self.tcx.at(item.span).mir_const_qualif(def_id);
let const_data = self.encode_rendered_const_for_body(body_id);
record!(self.tables.mir_const_qualif[def_id] <- qualifs);
EntryKind::Const(self.encode_rendered_const_for_body(body_id))
record!(self.tables.rendered_const[def_id] <- const_data);
EntryKind::Const
}
hir::ItemKind::Fn(ref sig, .., body) => {
let data = FnData {
Expand Down Expand Up @@ -1604,8 +1600,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let const_data = self.encode_rendered_const_for_body(body_id);
let qualifs = self.tcx.mir_const_qualif(def_id);

record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst(const_data));
record!(self.tables.kind[def_id.to_def_id()] <- EntryKind::AnonConst);
record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs);
record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data);
self.encode_item_type(def_id.to_def_id());
}

Expand Down
12 changes: 4 additions & 8 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Expand Up @@ -309,6 +309,7 @@ define_tables! {
// FIXME(eddyb) perhaps compute this on the fly if cheap enough?
coerce_unsized_info: Table<DefIndex, Lazy!(ty::adjustment::CoerceUnsizedInfo)>,
mir_const_qualif: Table<DefIndex, Lazy!(mir::ConstQualifs)>,
rendered_const: Table<DefIndex, Lazy!(String)>,

trait_item_def_id: Table<DefIndex, Lazy<DefId>>,
inherent_impls: Table<DefIndex, Lazy<[DefIndex]>>,
Expand All @@ -325,8 +326,8 @@ define_tables! {

#[derive(Copy, Clone, MetadataEncodable, MetadataDecodable)]
enum EntryKind {
AnonConst(Lazy<RenderedConst>),
Const(Lazy<RenderedConst>),
AnonConst,
Const,
ImmStatic,
MutStatic,
ForeignImmStatic,
Expand Down Expand Up @@ -354,15 +355,10 @@ enum EntryKind {
Impl,
AssocFn(Lazy<AssocFnData>),
AssocType(AssocContainer),
AssocConst(AssocContainer, Lazy<RenderedConst>),
AssocConst(AssocContainer),
TraitAlias,
}

/// Contains a constant which has been rendered to a String.
/// Used by rustdoc.
#[derive(Encodable, Decodable)]
struct RenderedConst(String);

#[derive(MetadataEncodable, MetadataDecodable)]
struct FnData {
asyncness: hir::IsAsync,
Expand Down

0 comments on commit 7bacdb7

Please sign in to comment.