Skip to content

don't store opaque info during encoding #139241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
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
6 changes: 2 additions & 4 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ use rustc_serialize::opaque::FileEncoder;
use rustc_session::config::{SymbolManglingVersion, TargetModifier};
use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib};
use rustc_span::edition::Edition;
use rustc_span::hygiene::{
ExpnIndex, MacroKind, SyntaxContextDataNonRecursive as SyntaxContextData,
};
use rustc_span::hygiene::{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 @@ -195,7 +193,7 @@ enum LazyState {
Previous(NonZero<usize>),
}

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

Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_middle/src/query/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use rustc_serialize::opaque::{FileEncodeResult, FileEncoder, IntEncodedWithFixed
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_session::Session;
use rustc_span::hygiene::{
ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext,
SyntaxContextDataNonRecursive as SyntaxContextData,
ExpnId, HygieneDecodeContext, HygieneEncodeContext, SyntaxContext, SyntaxContextKey,
};
use rustc_span::source_map::Spanned;
use rustc_span::{
Expand Down Expand Up @@ -567,7 +566,7 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
// 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: SyntaxContextData = decode_tagged(decoder, TAG_SYNTAX_CONTEXT);
let data: SyntaxContextKey = decode_tagged(decoder, TAG_SYNTAX_CONTEXT);
data
})
})
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/parameterized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ trivially_parameterized_over_tcx! {
rustc_span::Span,
rustc_span::Symbol,
rustc_span::def_id::DefPathHash,
rustc_span::hygiene::SyntaxContextDataNonRecursive,
rustc_span::hygiene::SyntaxContextKey,
rustc_span::Ident,
rustc_type_ir::Variance,
rustc_hir::Attribute,
Expand Down
74 changes: 18 additions & 56 deletions compiler/rustc_span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl !PartialOrd for SyntaxContext {}

/// If this part of two syntax contexts is equal, then the whole syntax contexts should be equal.
/// The other fields are only for caching.
type SyntaxContextKey = (SyntaxContext, ExpnId, Transparency);
pub type SyntaxContextKey = (SyntaxContext, ExpnId, Transparency);

#[derive(Clone, Copy, Debug)]
struct SyntaxContextData {
Expand All @@ -71,17 +71,6 @@ struct SyntaxContextData {
dollar_crate_name: Symbol,
}

/// Same as `SyntaxContextData`, but `opaque(_and_semitransparent)` cannot be recursive
/// and use `None` if they need to refer to self. Used for encoding and decoding metadata.
#[derive(Encodable, Decodable)]
pub struct SyntaxContextDataNonRecursive {
outer_expn: ExpnId,
outer_transparency: Transparency,
parent: SyntaxContext,
opaque: Option<SyntaxContext>,
opaque_and_semitransparent: Option<SyntaxContext>,
}

impl SyntaxContextData {
fn new(
(parent, outer_expn, outer_transparency): SyntaxContextKey,
Expand Down Expand Up @@ -122,19 +111,6 @@ impl SyntaxContextData {
}
}

impl SyntaxContextDataNonRecursive {
fn recursive(&self, ctxt: SyntaxContext) -> SyntaxContextData {
SyntaxContextData {
outer_expn: self.outer_expn,
outer_transparency: self.outer_transparency,
parent: self.parent,
opaque: self.opaque.unwrap_or(ctxt),
opaque_and_semitransparent: self.opaque_and_semitransparent.unwrap_or(ctxt),
dollar_crate_name: kw::DollarCrate,
}
}
}

rustc_index::newtype_index! {
/// A unique ID associated with a macro invocation and expansion.
#[orderable]
Expand Down Expand Up @@ -658,19 +634,6 @@ impl HygieneData {
SyntaxContextData::new(key, opaque, opaque_and_semitransparent);
ctxt
}

fn non_recursive_ctxt(&self, ctxt: SyntaxContext) -> SyntaxContextDataNonRecursive {
debug_assert!(!self.syntax_context_data[ctxt.0 as usize].is_decode_placeholder());
let data = &self.syntax_context_data[ctxt.0 as usize];
SyntaxContextDataNonRecursive {
outer_expn: data.outer_expn,
outer_transparency: data.outer_transparency,
parent: data.parent,
opaque: (data.opaque != ctxt).then_some(data.opaque),
opaque_and_semitransparent: (data.opaque_and_semitransparent != ctxt)
.then_some(data.opaque_and_semitransparent),
}
}
}

pub fn walk_chain(span: Span, to: SyntaxContext) -> Span {
Expand Down Expand Up @@ -1300,7 +1263,7 @@ impl HygieneEncodeContext {
pub fn encode<T>(
&self,
encoder: &mut T,
mut encode_ctxt: impl FnMut(&mut T, u32, &SyntaxContextDataNonRecursive),
mut encode_ctxt: impl FnMut(&mut T, u32, &SyntaxContextKey),
mut encode_expn: impl FnMut(&mut T, ExpnId, &ExpnData, ExpnHash),
) {
// When we serialize a `SyntaxContextData`, we may end up serializing
Expand Down Expand Up @@ -1425,10 +1388,7 @@ pub fn decode_expn_id(
// to track which `SyntaxContext`s we have already decoded.
// The provided closure will be invoked to deserialize a `SyntaxContextData`
// if we haven't already seen the id of the `SyntaxContext` we are deserializing.
pub fn decode_syntax_context<
D: Decoder,
F: FnOnce(&mut D, u32) -> SyntaxContextDataNonRecursive,
>(
pub fn decode_syntax_context<D: Decoder, F: FnOnce(&mut D, u32) -> SyntaxContextKey>(
d: &mut D,
context: &HygieneDecodeContext,
decode_data: F,
Expand All @@ -1450,16 +1410,9 @@ pub fn decode_syntax_context<

// Don't try to decode data while holding the lock, since we need to
// be able to recursively decode a SyntaxContext
let ctxt_data = decode_data(d, raw_id);

let ctxt = HygieneData::with(|hygiene_data| {
let ctxt_key = (ctxt_data.parent, ctxt_data.outer_expn, ctxt_data.outer_transparency);
*hygiene_data.syntax_context_map.entry(ctxt_key).or_insert_with(|| {
let ctxt = SyntaxContext::from_usize(hygiene_data.syntax_context_data.len());
hygiene_data.syntax_context_data.push(ctxt_data.recursive(ctxt));
ctxt
})
});
let (parent, expn_id, transparency) = decode_data(d, raw_id);
let ctxt =
HygieneData::with(|hygiene_data| hygiene_data.alloc_ctxt(parent, expn_id, transparency));

let mut inner = context.inner.lock();
let new_len = raw_id as usize + 1;
Expand All @@ -1471,12 +1424,21 @@ pub fn decode_syntax_context<
ctxt
}

fn for_all_ctxts_in<F: FnMut(u32, SyntaxContext, &SyntaxContextDataNonRecursive)>(
fn for_all_ctxts_in<F: FnMut(u32, SyntaxContext, &SyntaxContextKey)>(
ctxts: impl Iterator<Item = SyntaxContext>,
mut f: F,
) {
let all_data: Vec<_> =
HygieneData::with(|data| ctxts.map(|ctxt| (ctxt, data.non_recursive_ctxt(ctxt))).collect());
let all_data: Vec<_> = HygieneData::with(|data| {
ctxts
.map(|ctxt| {
(ctxt, {
let item = data.syntax_context_data[ctxt.0 as usize];
debug_assert!(!item.is_decode_placeholder());
item.key()
})
})
.collect()
});
for (ctxt, data) in all_data.into_iter() {
f(ctxt.0, ctxt, &data);
}
Expand Down
Loading