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: 5 additions & 1 deletion 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::{HygieneEntityRemapper, LocalExpansionRemapper, SyntaxContextRemapper};
use rustc_span::{SpanEncoder, Symbol, kw, sym};
use tracing::{debug, instrument};

Expand Down Expand Up @@ -308,7 +309,10 @@ impl Definitions {
assert!(
self.def_path_hash_to_index.after_parallel_alloc.replace(Default::default()).is_none(),
"this function should be called only once"
)
);

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

#[inline(always)]
Expand Down
66 changes: 59 additions & 7 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ 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, HygieneEntityRemapper, LocalExpansionRemapper, SyntaxContextRemapper,
};
use rustc_span::{
ByteSymbol, ExternalSource, FileName, SourceFile, SpanData, SpanEncoder, StableSourceFileId,
Symbol, SyntaxContext, bug, span_bug, sym,
Expand All @@ -49,6 +51,30 @@ use crate::diagnostics::{FailCreateFileEncoder, FailWriteFile};
use crate::eii::EiiMapEncodedKeyValue;
use crate::rmeta::*;

#[derive(Default)]
struct HygieneRemappingResults {
remapping: Vec<u32>,
first_non_det_index: u32,
last_idx: u32,
}

impl HygieneRemappingResults {
fn new((idx, last_idx, remapping): (u32, u32, Vec<u32>)) -> HygieneRemappingResults {
HygieneRemappingResults { remapping, last_idx, first_non_det_index: idx }
}

#[inline]
fn map_id(&self, idx: u32) -> u32 {
// New hygiene entities can be created during metadata encoding,
// so we need both checks.
if idx < self.first_non_det_index || idx > self.last_idx {
idx
} else {
self.remapping[(idx - self.first_non_det_index) as usize]
}
}
}

pub(super) struct EncodeContext<'a, 'tcx> {
opaque: FileEncoder<'a>,
metadata_hasher: Arc<Mutex<XxHash3_128>>,
Expand Down Expand Up @@ -78,6 +104,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>,
s_ctxt_remapping: HygieneRemappingResults,
local_expn_remapping: HygieneRemappingResults,
}

/// If the current crate is a proc-macro, returns early with `LazyArray::default()`.
Expand Down Expand Up @@ -170,7 +198,7 @@ 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.map_syntax_context(idx).encode(self);
}

fn encode_expn_id(&mut self, expn_id: ExpnId) {
Expand All @@ -182,7 +210,12 @@ 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);

if let Some(local_expn) = expn_id.as_local() {
self.map_local_expn(local_expn.as_u32()).encode(self);
} else {
expn_id.local_id.encode(self);
}
}

fn encode_span(&mut self, span: Span) {
Expand Down Expand Up @@ -469,6 +502,16 @@ macro_rules! record_defaulted_array {
}

impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
#[inline]
fn map_syntax_context(&self, idx: u32) -> u32 {
self.s_ctxt_remapping.map_id(idx)
}

#[inline]
fn map_local_expn(&self, idx: u32) -> u32 {
self.local_expn_remapping.map_id(idx)
}

fn emit_lazy_distance(&mut self, position: NonZero<usize>) {
let pos = position.get();
let distance = match self.lazy_state {
Expand Down Expand Up @@ -2042,14 +2085,15 @@ 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, _, _), index, ctxt_data| {
let index = this.map_syntax_context(index);
syntax_contexts.set_some(index, 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")));
if let Some(idx) = index.as_local() {
let idx = this.map_local_expn(idx.as_u32()).into();

expn_hash_table.set_some(index.as_raw(), this.lazy(hash));
expn_data_table.set_some(idx, this.lazy(expn_data.expect("local expn")));
expn_hash_table.set_some(idx, this.lazy(hash));
}
},
);
Expand Down Expand Up @@ -2628,6 +2672,12 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path, ref_path: Option<&Path>) {
with_encode_metadata_header(tcx, path, |ecx| {
// Encode all the entries and extra information in the crate,
// culminating in the `CrateRoot` which points to all of it.
let s_ctxt_remapping = SyntaxContextRemapper::with(|r| r.create_remapping());
let l_expn_remapping = LocalExpansionRemapper::with(|r| r.create_remapping());

ecx.s_ctxt_remapping = HygieneRemappingResults::new(s_ctxt_remapping);
ecx.local_expn_remapping = HygieneRemappingResults::new(l_expn_remapping);

let (root, unhashed) = ecx.encode_crate_root();

// Flush buffer to ensure backing file has the correct size.
Expand Down Expand Up @@ -2734,6 +2784,8 @@ fn with_encode_metadata_header(
is_proc_macro: tcx.crate_types().contains(&CrateType::ProcMacro),
hygiene_ctxt: Default::default(),
symbol_index_table: Default::default(),
s_ctxt_remapping: Default::default(),
local_expn_remapping: Default::default(),
};

// Encode the rustc version string in a predictable location.
Expand Down
Loading
Loading