Skip to content
Merged
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
24 changes: 15 additions & 9 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,15 +467,21 @@ impl<'a, 'tcx> SpanDecoder for MetadataDecodeContext<'a, 'tcx> {
let tcx = self.tcx;

let cname = cdata.root.name();
rustc_span::hygiene::decode_syntax_context(self, &cdata.hygiene_context, |_, id| {
debug!("SpecializedDecoder<SyntaxContext>: decoding {}", id);
cdata
.root
.syntax_contexts
.get(cdata, id)
.unwrap_or_else(|| panic!("Missing SyntaxContext {id:?} for crate {cname:?}"))
.decode((cdata, tcx))
})
rustc_span::hygiene::decode_syntax_context(
self,
&cdata.hygiene_context,
|_, external_ctxt| {
debug!("SpecializedDecoder<SyntaxContext>: decoding {:?}", external_ctxt);
cdata
.root
.syntax_contexts
.get(cdata, external_ctxt)
.unwrap_or_else(|| {
panic!("Missing SyntaxContext {external_ctxt:?} for crate {cname:?}")
})
.decode((cdata, tcx))
},
)
}

fn decode_expn_id(&mut self) -> ExpnId {
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ pub(super) struct EncodeContext<'a, 'tcx> {
// Used for both `Symbol`s and `ByteSymbol`s.
symbol_index_table: FxHashMap<u32, usize>,
local_defs_remapping: LocalDefsRemapping,
syntax_context_remapping: HygieneRemappingResults<SyntaxContext>,
local_expn_id_remapping: HygieneRemappingResults<LocalExpnId>,
syntax_context_remapping: HygieneRemappingResults<SyntaxContext, RemappedSyntaxContext>,
local_expn_id_remapping: HygieneRemappingResults<LocalExpnId, RemappedLocalExpnIndex>,
}

/// If the current crate is a proc-macro, returns early with `LazyArray::default()`.
Expand Down Expand Up @@ -198,12 +198,12 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
}
expn_id.krate.encode(self);

let expn_index = match expn_id.as_local() {
Some(local_expn) => self.local_expn_id_remapping.map_id(local_expn).as_raw(),
None => expn_id.local_id,
let raw_expn_index = match expn_id.as_local() {
Some(local_expn) => self.local_expn_id_remapping.map_id(local_expn).as_u32(),
None => expn_id.local_id.as_u32(),
};

expn_index.encode(self);
raw_expn_index.encode(self);
}

fn encode_span(&mut self, span: Span) {
Expand Down Expand Up @@ -2087,12 +2087,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
&Rc::clone(&self.hygiene_ctxt),
&mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table),
|(this, syntax_contexts, _, _), ctxt, ctxt_data| {
let context = this.syntax_context_remapping.map_id(ctxt);
syntax_contexts.set_some(context.as_u32(), this.lazy(ctxt_data));
let ctxt = this.syntax_context_remapping.map_id(ctxt);
syntax_contexts.set_some(ctxt, this.lazy(ctxt_data));
},
|(this, _, expn_data_table, expn_hash_table), expn_id, expn_data, hash| {
if let Some(expn_id) = expn_id.as_local() {
let expn_index = this.local_expn_id_remapping.map_id(expn_id).as_raw();
let expn_index = this.local_expn_id_remapping.map_id(expn_id);

expn_data_table.set_some(expn_index, this.lazy(expn_data.expect("local expn")));
expn_hash_table.set_some(expn_index, this.lazy(hash));
Expand Down
20 changes: 16 additions & 4 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use rustc_serialize::opaque::FileEncoder;
use rustc_session::config::mitigation_coverage::DeniedPartialMitigation;
use rustc_session::config::{SymbolManglingVersion, TargetModifier};
use rustc_span::edition::Edition;
use rustc_span::hygiene::{ExpnIndex, MacroKind, SyntaxContextKey};
use rustc_span::hygiene::{DecodedSyntaxContext, ExpnIndex, MacroKind, SyntaxContextKey};
use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Ident, Span, Symbol};
use rustc_target::spec::{PanicStrategy, TargetTuple};
use table::TableBuilder;
Expand Down Expand Up @@ -209,9 +209,10 @@ enum LazyState {
Previous(NonZero<usize>),
}

type SyntaxContextTable = LazyTable<u32, u32, Option<LazyValue<SyntaxContextKey>>>;
type ExpnDataTable = LazyTable<ExpnIndex, ExpnIndex, Option<LazyValue<ExpnData>>>;
type ExpnHashTable = LazyTable<ExpnIndex, ExpnIndex, Option<LazyValue<ExpnHash>>>;
type SyntaxContextTable =
LazyTable<RemappedSyntaxContext, DecodedSyntaxContext, Option<LazyValue<SyntaxContextKey>>>;
type ExpnDataTable = LazyTable<RemappedLocalExpnIndex, ExpnIndex, Option<LazyValue<ExpnData>>>;
type ExpnHashTable = LazyTable<RemappedLocalExpnIndex, ExpnIndex, Option<LazyValue<ExpnHash>>>;

#[derive(MetadataEncodable, LazyDecodable)]
pub(crate) struct ProcMacroData {
Expand Down Expand Up @@ -343,6 +344,17 @@ pub(crate) struct CrateRootUnhashed {
dep_extra_filenames: IndexVec<CrateNum, String>,
}

rustc_index::newtype_index! {
#[max = 0x1FFF_FFFF] // `u32::MAX` >> 3, as `SyntaxContext` should be fit in 29 bits (see `span_encoding`).
#[default]
pub(super) struct RemappedSyntaxContext {}
}

rustc_index::newtype_index! {
#[default]
pub(super) struct RemappedLocalExpnIndex {}
}

#[derive(Default)]
pub(crate) struct LocalDefsRemapping {
/// See `Definitions::first_non_det_index`, it is just cached version of it.
Expand Down
22 changes: 13 additions & 9 deletions compiler/rustc_middle/src/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,15 +552,19 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Vec<u8> {
impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
fn decode_syntax_context(&mut self) -> SyntaxContext {
let syntax_contexts = self.syntax_contexts;
rustc_span::hygiene::decode_syntax_context(self, self.hygiene_context, |this, id| {
// This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
// We look up the position of the associated `SyntaxData` and decode it.
let pos = syntax_contexts.get(&id).unwrap();
this.with_position(pos.to_usize(), |decoder| {
let data: SyntaxContextKey = decode_tagged(decoder, TAG_SYNTAX_CONTEXT);
data
})
})
rustc_span::hygiene::decode_syntax_context(
self,
self.hygiene_context,
|this, external_ctxt| {
// This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing.
// We look up the position of the associated `SyntaxData` and decode it.
let pos = syntax_contexts.get(&external_ctxt.as_u32()).unwrap();
this.with_position(pos.to_usize(), |decoder| {
let data: SyntaxContextKey = decode_tagged(decoder, TAG_SYNTAX_CONTEXT);
data
})
},
)
}

fn decode_expn_id(&mut self) -> ExpnId {
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_mir_transform/src/coverage/expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ pub(crate) fn build_expn_tree(
let raw_spans = from_mir::extract_raw_spans_from_mir(mir_body, hir_info, graph);

let mut nodes = FxIndexMap::default();
let new_node = |&context: &SyntaxContext| ExpnNode::for_context(context);
let new_node = |context: SyntaxContext| ExpnNode::for_context(context);

for from_mir::RawSpanFromMir { raw_span, bcb } in raw_spans {
let span_with_bcb = SpanWithBcb { span: raw_span, bcb };

// Create a node for this span's enclosing expansion, and add the span to it.
let context = span_with_bcb.span.ctxt();
let node = nodes.entry(context).or_insert_with_key(new_node);
let node = nodes.entry(context).or_insert_with_key(|&c| new_node(c));
node.spans.push(span_with_bcb);

// Now walk up the expansion call-site chain, creating nodes and registering children.
Expand All @@ -123,7 +123,7 @@ pub(crate) fn build_expn_tree(
let entry = nodes.entry(context);
let node_existed = matches!(entry, IndexEntry::Occupied(_));

let node = entry.or_insert_with_key(new_node);
let node = entry.or_insert_with_key(|&c| new_node(c));
node.child_contexts.insert(prev);

if node_existed {
Expand Down
Loading
Loading