Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,10 +1545,12 @@ impl CrateMetadata {

fn get_proc_macro_quoted_span(&self, tcx: TyCtxt<'_>, index: usize) -> Span {
self.root
.tables
.proc_macro_data
.as_ref()
.unwrap_or_else(|| panic!("missing proc macro data"))
.proc_macro_quoted_spans
.get(self, index)
.unwrap_or_else(|| panic!("Missing proc macro quoted span: {index:?}"))
.unwrap_or_else(|| panic!("missing proc macro quoted span: {index:?}"))
.decode((self, tcx))
}

Expand Down
104 changes: 63 additions & 41 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ impl<'a, 'tcx> TyEncoder<'tcx> for EncodeContext<'a, 'tcx> {

// Shorthand for `$self.$tables.$table.set_some($def_id.index, $self.lazy($value))`, which would
// normally need extra variables to avoid errors about multiple mutable borrows.
// Also those macros greatly simplify refactorings and modifications of writing to tables.
macro_rules! record {
($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
{
Expand All @@ -406,6 +407,22 @@ macro_rules! record {
}};
}

macro_rules! record_non_lazy {
($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
{
$self.$tables.$table.set_some($def_id.index, $value);
}
}};
}

macro_rules! record_defaulted {

@aerooneqq aerooneqq Sep 17, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming of those two macros is debatable, I am not sure that it 100% fits them.

View changes since the review

($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
{
$self.$tables.$table.set($def_id.index, $value);
}
}};
}

// Shorthand for `$self.$tables.$table.set_some($def_id.index, $self.lazy_array($value))`, which would
// normally need extra variables to avoid errors about multiple mutable borrows.
macro_rules! record_array {
Expand Down Expand Up @@ -519,18 +536,18 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
for def_id in std::iter::once(CRATE_DEF_ID)
.chain(self.tcx.resolutions(()).proc_macros.iter().copied())
{
let def_key = self.lazy(defs.def_key(def_id));
let def_key = defs.def_key(def_id);
let def_path_hash = defs.def_path_hash(def_id);
self.tables.def_keys.set_some(def_id.local_def_index, def_key);
self.tables
.def_path_hashes
.set(def_id.local_def_index, def_path_hash.local_hash().as_u64());
let def_id = def_id.to_def_id();

record!(self.tables.def_keys[def_id] <- def_key);
record_defaulted!(self.tables.def_path_hashes[def_id] <- def_path_hash.local_hash().as_u64())
}
} else {
for (def_index, def_key, def_path_hash) in defs.enumerated_keys_and_path_hashes() {
let def_key = self.lazy(def_key);
self.tables.def_keys.set_some(def_index, def_key);
self.tables.def_path_hashes.set(def_index, def_path_hash.local_hash().as_u64());
let def_id = LocalDefId { local_def_index: def_index }.to_def_id();
record!(self.tables.def_keys[def_id] <- def_key);
record_defaulted!(self.tables.def_path_hashes[def_id] <- def_path_hash.local_hash().as_u64())
}
}
}
Expand Down Expand Up @@ -1405,7 +1422,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
if state.is_doc_hidden {
attr_flags |= AttrFlags::IS_DOC_HIDDEN;
}
self.tables.attr_flags.set(def_id.local_def_index, attr_flags);

record_defaulted!(self.tables.attr_flags[def_id.to_def_id()] <- attr_flags)
}

fn encode_def_ids(&mut self) {
Expand All @@ -1422,7 +1440,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
for local_id in tcx.iter_local_def_id() {
let def_id = local_id.to_def_id();
let def_kind = tcx.def_kind(local_id);
self.tables.def_kind.set_some(def_id.index, def_kind);
record_non_lazy!(self.tables.def_kind[def_id] <- def_kind);

// The `DefCollector` will sometimes create unnecessary `DefId`s
// for trivial const arguments which are directly lowered to
Expand Down Expand Up @@ -1506,11 +1524,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}
if should_encode_constness(def_kind) {
let constness = self.tcx.constness(def_id);
self.tables.constness.set(def_id.index, constness);
record_defaulted!(self.tables.constness[def_id] <- constness)
}
if let DefKind::Fn | DefKind::AssocFn = def_kind {
let asyncness = tcx.asyncness(def_id);
self.tables.asyncness.set(def_id.index, asyncness);
record_defaulted!(self.tables.asyncness[def_id] <- asyncness);
record_array!(self.tables.fn_arg_idents[def_id] <- tcx.fn_arg_idents(def_id));
}
if let Some(name) = tcx.intrinsic(def_id) {
Expand Down Expand Up @@ -1556,22 +1574,20 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
if let DefKind::Closure | DefKind::SyntheticCoroutineBody = def_kind
&& let Some(coroutine_kind) = self.tcx.coroutine_kind(def_id)
{
self.tables.coroutine_kind.set(def_id.index, Some(coroutine_kind))
record_defaulted!(self.tables.coroutine_kind[def_id] <- Some(coroutine_kind))
}
if def_kind == DefKind::Closure
&& tcx.type_of(def_id).skip_binder().is_coroutine_closure()
{
let coroutine_for_closure = self.tcx.coroutine_for_closure(def_id);
self.tables
.coroutine_for_closure
.set_some(def_id.index, coroutine_for_closure.into());
record_non_lazy!(self.tables.coroutine_for_closure[def_id] <- coroutine_for_closure.into());

// If this async closure has a by-move body, record it too.
if tcx.needs_coroutine_by_move_body_def_id(coroutine_for_closure) {
self.tables.coroutine_by_move_body_def_id.set_some(
coroutine_for_closure.index,
self.tcx.coroutine_by_move_body_def_id(coroutine_for_closure).into(),
);
let id = self.tcx.coroutine_by_move_body_def_id(coroutine_for_closure);
record_non_lazy!(
self.tables.coroutine_by_move_body_def_id[coroutine_for_closure] <- id.into()
)
}
}
if let DefKind::Static { .. } = def_kind {
Expand Down Expand Up @@ -1601,9 +1617,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
self.encode_info_for_macro(local_id);
}
if let DefKind::TyAlias = def_kind {
self.tables
.type_alias_is_checked
.set(def_id.index, self.tcx.type_alias_is_checked(def_id));
record_defaulted!(self.tables.type_alias_is_checked[def_id] <- self.tcx.type_alias_is_checked(def_id));

if self.tcx.type_alias_is_checked(def_id) {
record!(self.tables.args_known_to_outlive_alias_params[def_id] <- tcx.args_known_to_outlive_alias_params(def_id));
}
Expand Down Expand Up @@ -1714,7 +1729,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}));

for field in &variant.fields {
self.tables.safety.set(field.did.index, field.safety);
record_defaulted!(self.tables.safety[field.did] <- field.safety);
record!(
self.tables.mut_restriction[field.did] <- field.mut_restriction
);
Expand Down Expand Up @@ -1787,7 +1802,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let item = tcx.associated_item(def_id);

if matches!(item.container, AssocContainer::Trait | AssocContainer::TraitImpl(_)) {
self.tables.defaultness.set(def_id.index, item.defaultness(tcx));
record_defaulted!(self.tables.defaultness[def_id] <- item.defaultness(tcx));
}

record!(self.tables.assoc_container[def_id] <- item.container);
Expand Down Expand Up @@ -1840,9 +1855,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
debug!("EntryBuilder::encode_mir({:?})", def_id);
if encode_opt {
record!(self.tables.optimized_mir[def_id.to_def_id()] <- tcx.optimized_mir(def_id));
self.tables
.cross_crate_inlinable
.set(def_id.to_def_id().index, self.tcx.cross_crate_inlinable(def_id));

record_defaulted!(self.tables.cross_crate_inlinable[def_id.to_def_id()] <- self.tcx.cross_crate_inlinable(def_id));

record!(self.tables.closure_saved_names_of_captured_variables[def_id.to_def_id()]
<- tcx.closure_saved_names_of_captured_variables(def_id));

Expand Down Expand Up @@ -1950,7 +1965,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let tcx = self.tcx;

let (_, macro_def, _) = tcx.hir_expect_item(def_id).expect_macro();
self.tables.is_macro_rules.set(def_id.local_def_index, macro_def.macro_rules);
record_defaulted!(self.tables.is_macro_rules[def_id.to_def_id()] <- macro_def.macro_rules);
record!(self.tables.macro_definition[def_id.to_def_id()] <- &*macro_def.body);
}

Expand Down Expand Up @@ -2000,12 +2015,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let tcx = self.tcx;
let proc_macro_decls_static = tcx.proc_macro_decls_static(()).unwrap().local_def_index;
let stability = tcx.lookup_stability(CRATE_DEF_ID);
for (i, span) in self.tcx.sess.proc_macro_quoted_spans() {
let span = self.lazy(span);
self.tables.proc_macro_quoted_spans.set_some(i, span);
}

self.tables.def_kind.set_some(LOCAL_CRATE.as_def_id().index, DefKind::Mod);
record_non_lazy!(self.tables.def_kind[LOCAL_CRATE.as_def_id()] <- DefKind::Mod);
record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id()));
self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local());
let vis = tcx
Expand Down Expand Up @@ -2064,7 +2075,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
def_key.disambiguated_data.data = DefPathData::MacroNs(name);

let def_id = id.to_def_id();
self.tables.def_kind.set_some(def_id.index, DefKind::Macro(macro_kind.into()));
record_non_lazy!(self.tables.def_kind[def_id] <- DefKind::Macro(macro_kind.into()));

self.encode_attrs(id);
record!(self.tables.def_keys[def_id] <- def_key);
record!(self.tables.def_ident_span[def_id] <- span);
Expand All @@ -2077,7 +2089,19 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

let macros = self.lazy_array(macros);

Some(ProcMacroData { proc_macro_decls_static, stability, macros })
let mut proc_macro_quoted_spans = TableBuilder::default();
for (i, span) in self.tcx.sess.proc_macro_quoted_spans() {
proc_macro_quoted_spans.set_some(i, self.lazy(span));
}

let proc_macro_quoted_spans = proc_macro_quoted_spans.encode(&mut self.opaque);

Some(ProcMacroData {
proc_macro_decls_static,
stability,
macros,
proc_macro_quoted_spans,
})
} else {
None
}
Expand Down Expand Up @@ -2229,11 +2253,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

let impl_is_fully_generic_for_reflection =
tcx.impl_is_fully_generic_for_reflection(def_id);
self.tables
.impl_is_fully_generic_for_reflection
.set(def_id.index, impl_is_fully_generic_for_reflection);

self.tables.defaultness.set(def_id.index, tcx.defaultness(def_id));
record_defaulted!(self.tables.impl_is_fully_generic_for_reflection[def_id] <- impl_is_fully_generic_for_reflection);
record_defaulted!(self.tables.defaultness[def_id] <- tcx.defaultness(def_id));

let trait_ref = header.trait_ref.instantiate_identity().skip_norm_wip();
let simplified_self_ty = fast_reject::simplify_type(
Expand All @@ -2250,7 +2272,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
if let Ok(mut an) = trait_def.ancestors(tcx, def_id)
&& let Some(specialization_graph::Node::Impl(parent)) = an.nth(1)
{
self.tables.impl_parent.set_some(def_id.index, parent.into());
record_non_lazy!(self.tables.impl_parent[def_id] <- parent.into());
}

// if this is an impl of `CoerceUnsized`, create its
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ pub(crate) struct ProcMacroData {
proc_macro_decls_static: DefIndex,
stability: Option<hir::Stability>,
macros: LazyArray<(DefIndex, LazyValue<ProcMacroKind>)>,
proc_macro_quoted_spans: LazyTable<usize, Option<LazyValue<Span>>>,
}

#[derive(MetadataEncodable, LazyDecodable)]
Expand Down Expand Up @@ -470,7 +471,6 @@ define_tables! {
// `DefPathTable` up front, since we may only ever use a few
// definitions from any given crate.
def_keys: Table<DefIndex, LazyValue<DefKey>>,
proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
variant_data: Table<DefIndex, LazyValue<VariantData>>,
assoc_container: Table<DefIndex, LazyValue<ty::AssocContainer>>,
macro_definition: Table<DefIndex, LazyValue<ast::DelimArgs>>,
Expand Down
Loading