Skip to content
10 changes: 6 additions & 4 deletions compiler/rustc_hir_id/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use rustc_span::def_id::DefPathHash;
use rustc_span::def_id::{
CRATE_DEF_INDEX, CrateNum, DefIndex, LOCAL_CRATE, LocalDefId, LocalDefIdMap, StableCrateId,
};
use rustc_span::hygiene::{HygieneRemapper, LocalExpnIdRemapper, SyntaxContextRemapper};
use rustc_span::{SpanEncoder, Symbol, kw, sym};
use tracing::{debug, instrument};

Expand Down Expand Up @@ -305,10 +306,11 @@ impl Definitions {
/// This function indicates that the order of def id allocations
/// may be non-deterministic after it was called.
pub fn commit_end_of_determinism(&mut self) {
assert!(
self.def_path_hash_to_index.after_parallel_alloc.replace(Default::default()).is_none(),
"this function should be called only once"
)
let prev = self.def_path_hash_to_index.after_parallel_alloc.replace(Default::default());
assert!(prev.is_none(), "this function should be called only once");

SyntaxContextRemapper::with(|mut r| r.commit_end_of_determinism());
LocalExpnIdRemapper::with(|mut r| r.commit_end_of_determinism());
}

#[inline(always)]
Expand Down
48 changes: 35 additions & 13 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_session::config::mitigation_coverage::DeniedPartialMitigation;
use rustc_session::config::{OptLevel, OutputType, TargetModifier};
use rustc_span::def_id::CRATE_MOD_ID;
use rustc_span::hygiene::HygieneEncodeContext;
use rustc_span::hygiene::{
HygieneEncodeContext, HygieneRemapper, HygieneRemappingResults, LocalExpnIdRemapper,
SyntaxContextRemapper,
};
use rustc_span::{
ByteSymbol, ExternalSource, FileName, SourceFile, SpanData, SpanEncoder, StableSourceFileId,
Symbol, SyntaxContext, bug, span_bug, sym,
ByteSymbol, ExternalSource, FileName, LocalExpnId, SourceFile, SpanData, SpanEncoder,
StableSourceFileId, Symbol, SyntaxContext, bug, span_bug, sym,
};
use rustc_structures::CrateType;
use tracing::{debug, instrument, trace};
Expand Down Expand Up @@ -78,6 +81,8 @@ pub(super) struct EncodeContext<'a, 'tcx> {
hygiene_ctxt: Rc<RefCell<HygieneEncodeContext>>,
// Used for both `Symbol`s and `ByteSymbol`s.
symbol_index_table: FxHashMap<u32, usize>,
syntax_context_remapping: HygieneRemappingResults<SyntaxContext>,
local_expn_id_remapping: HygieneRemappingResults<LocalExpnId>,
}

/// If the current crate is a proc-macro, returns early with `LazyArray::default()`.
Expand Down Expand Up @@ -169,8 +174,8 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
}

fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) {
let idx = self.hygiene_ctxt.borrow_mut().get_syntax_ctxt_encoding_index(syntax_context);
idx.encode(self);
self.hygiene_ctxt.borrow_mut().enqueue_syntax_context_for_encoding(syntax_context);
self.emit_u32(self.syntax_context_remapping.map_id(syntax_context).as_u32());
}

fn encode_expn_id(&mut self, expn_id: ExpnId) {
Expand All @@ -182,7 +187,13 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> {
self.hygiene_ctxt.borrow_mut().schedule_expn_data_for_encoding(expn_id);
}
expn_id.krate.encode(self);
expn_id.local_id.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,
};

expn_index.encode(self);
}

fn encode_span(&mut self, span: Span) {
Expand Down Expand Up @@ -2041,15 +2052,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
HygieneEncodeContext::encode(
&Rc::clone(&self.hygiene_ctxt),
&mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table),
|(this, syntax_contexts, _, _), index, ctxt_data| {
syntax_contexts.set_some(index, this.lazy(ctxt_data));
|(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));
},
|(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| {
if let Some(index) = index.as_local() {
expn_data_table
.set_some(index.as_raw(), this.lazy(expn_data.expect("local expn")));
|(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();

expn_hash_table.set_some(index.as_raw(), this.lazy(hash));
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 Expand Up @@ -2718,6 +2730,14 @@ fn with_encode_metadata_header(
let required_source_files = Some(FxIndexSet::default());
drop(source_map_files);

let (syntax_context_remapping, local_expn_id_remapping) =
tcx.with_stable_hashing_context(|mut ctx| {
(
SyntaxContextRemapper::with(|r| r.remapping_results(&mut ctx)),
LocalExpnIdRemapper::with(|r| r.remapping_results(&mut ctx)),
)
});

let mut ecx = EncodeContext {
opaque: encoder,
metadata_hasher: Arc::clone(&metadata_hasher),
Expand All @@ -2734,6 +2754,8 @@ fn with_encode_metadata_header(
is_proc_macro: tcx.crate_types().contains(&CrateType::ProcMacro),
hygiene_ctxt: Default::default(),
symbol_index_table: Default::default(),
syntax_context_remapping,
local_expn_id_remapping,
};

// Encode the rustc version string in a predictable location.
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,10 @@ impl OnDiskCache {
HygieneEncodeContext::encode(
&Rc::clone(&encoder.hygiene_context),
&mut encoder,
|encoder, index, ctxt_data| {
|encoder, ctxt, ctxt_data| {
let pos = AbsoluteBytePos::new(encoder.position());
encoder.encode_tagged(TAG_SYNTAX_CONTEXT, ctxt_data);
syntax_contexts.insert(index, pos);
syntax_contexts.insert(ctxt.as_u32(), pos);
},
|encoder, expn_id, data, hash| {
if expn_id.krate == LOCAL_CRATE {
Expand Down Expand Up @@ -838,8 +838,8 @@ impl<'tcx> CacheEncoder<'tcx> {

impl<'tcx> SpanEncoder for CacheEncoder<'tcx> {
fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) {
let idx = self.hygiene_context.borrow_mut().get_syntax_ctxt_encoding_index(syntax_context);
idx.encode(self);
self.hygiene_context.borrow_mut().enqueue_syntax_context_for_encoding(syntax_context);
self.emit_u32(syntax_context.as_u32());
}

fn encode_expn_id(&mut self, expn_id: ExpnId) {
Expand Down
Loading
Loading