Skip to content

Commit ce1f806

Browse files
committed
Use ExternalSyntaxContext for not yet remapped contexts
1 parent e66d7fc commit ce1f806

3 files changed

Lines changed: 29 additions & 7 deletions

File tree

‎compiler/rustc_metadata/src/rmeta/encoder.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2042,7 +2042,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
20422042
&Rc::clone(&self.hygiene_ctxt),
20432043
&mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table),
20442044
|(this, syntax_contexts, _, _), ctxt, ctxt_data| {
2045-
syntax_contexts.set_some(ctxt, this.lazy(ctxt_data));
2045+
let external_ctxt = ExternalSyntaxContext::from(ctxt);
2046+
syntax_contexts.set_some(external_ctxt, this.lazy(ctxt_data));
20462047
},
20472048
|(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| {
20482049
if let Some(index) = index.as_local() {

‎compiler/rustc_metadata/src/rmeta/mod.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ use rustc_serialize::opaque::FileEncoder;
4646
use rustc_session::config::mitigation_coverage::DeniedPartialMitigation;
4747
use rustc_session::config::{SymbolManglingVersion, TargetModifier};
4848
use rustc_span::edition::Edition;
49-
use rustc_span::hygiene::{ExpnIndex, MacroKind, SyntaxContextKey};
50-
use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Ident, Span, Symbol, SyntaxContext};
49+
use rustc_span::hygiene::{ExpnIndex, ExternalSyntaxContext, MacroKind, SyntaxContextKey};
50+
use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Ident, Span, Symbol};
5151
use rustc_target::spec::{PanicStrategy, TargetTuple};
5252
use table::TableBuilder;
5353

@@ -207,7 +207,7 @@ enum LazyState {
207207
}
208208

209209
type SyntaxContextTable =
210-
LazyTable<SyntaxContext, SyntaxContext, Option<LazyValue<SyntaxContextKey>>>;
210+
LazyTable<ExternalSyntaxContext, ExternalSyntaxContext, Option<LazyValue<SyntaxContextKey>>>;
211211
type ExpnDataTable = LazyTable<ExpnIndex, ExpnIndex, Option<LazyValue<ExpnData>>>;
212212
type ExpnHashTable = LazyTable<ExpnIndex, ExpnIndex, Option<LazyValue<ExpnHash>>>;
213213

‎compiler/rustc_span/src/hygiene.rs‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ impl HygieneEncodeContext {
14061406
pub struct HygieneDecodeContext {
14071407
// A cache mapping raw serialized per-crate syntax context ids to corresponding decoded
14081408
// `SyntaxContext`s in the current global `HygieneData`.
1409-
remapped_ctxts: Lock<IndexVec<SyntaxContext, Option<SyntaxContext>>>,
1409+
remapped_ctxts: Lock<IndexVec<ExternalSyntaxContext, Option<SyntaxContext>>>,
14101410
}
14111411

14121412
/// Register an expansion which has been decoded from the on-disk-cache for the local crate.
@@ -1473,16 +1473,37 @@ pub fn decode_expn_id(
14731473
register_expn_id(krate, index, expn_data, hash)
14741474
}
14751475

1476+
rustc_index::newtype_index! {
1477+
/// A syntax context that is obtained from metadata of an external crate and
1478+
/// was not yet remapped to local `SyntaxContext`.
1479+
#[encodable]
1480+
pub struct ExternalSyntaxContext {}
1481+
}
1482+
1483+
impl ExternalSyntaxContext {
1484+
#[inline(always)]
1485+
pub fn is_root(self) -> bool {
1486+
self.as_u32() == 0
1487+
}
1488+
}
1489+
1490+
impl From<SyntaxContext> for ExternalSyntaxContext {
1491+
#[inline]
1492+
fn from(ctxt: SyntaxContext) -> Self {
1493+
ExternalSyntaxContext::from_u32(ctxt.as_u32())
1494+
}
1495+
}
1496+
14761497
// Decodes `SyntaxContext`, using the provided `HygieneDecodeContext`
14771498
// to track which `SyntaxContext`s we have already decoded.
14781499
// The provided closure will be invoked to deserialize a `SyntaxContextData`
14791500
// if we haven't already seen the id of the `SyntaxContext` we are deserializing.
14801501
pub fn decode_syntax_context<D: Decoder>(
14811502
d: &mut D,
14821503
context: &HygieneDecodeContext,
1483-
decode_data: impl FnOnce(&mut D, SyntaxContext) -> SyntaxContextKey,
1504+
decode_data: impl FnOnce(&mut D, ExternalSyntaxContext) -> SyntaxContextKey,
14841505
) -> SyntaxContext {
1485-
let external_ctxt = SyntaxContext::from_u32(Decodable::decode(d));
1506+
let external_ctxt: ExternalSyntaxContext = Decodable::decode(d);
14861507
if external_ctxt.is_root() {
14871508
trace!("decode_syntax_context: deserialized root");
14881509
// The root is special

0 commit comments

Comments
 (0)