Skip to content

Commit ebfc7aa

Browse files
committed
Auto merge of rust-lang#100803 - klensy:do-not-encode-preinterned-symbols, r=bjorn3
Symbols: do not write string values of preinterned symbols into compiled artifacts r? `@bjorn3` Followup for rust-lang#98851 rust-lang#98851 (comment)
2 parents 4a24f08 + f632948 commit ebfc7aa

File tree

6 files changed

+57
-23
lines changed

6 files changed

+57
-23
lines changed

compiler/rustc_macros/src/symbols.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
195195
#n,
196196
});
197197
}
198-
let _ = counter; // for future use
199198

200199
let output = quote! {
201200
const SYMBOL_DIGITS_BASE: u32 = #digits_base;
201+
const PREINTERNED_SYMBOLS_COUNT: u32 = #counter;
202202

203203
#[doc(hidden)]
204204
#[allow(non_upper_case_globals)]

compiler/rustc_metadata/src/rmeta/decoder.rs

+4
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,10 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for Symbol {
631631

632632
sym
633633
}
634+
SYMBOL_PREINTERNED => {
635+
let symbol_index = d.read_u32();
636+
Symbol::new_from_decoded(symbol_index)
637+
}
634638
_ => unreachable!(),
635639
}
636640
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -317,17 +317,24 @@ impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span {
317317

318318
impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Symbol {
319319
fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) {
320-
match s.symbol_table.entry(*self) {
321-
Entry::Vacant(o) => {
322-
s.opaque.emit_u8(SYMBOL_STR);
323-
let pos = s.opaque.position();
324-
o.insert(pos);
325-
s.emit_str(self.as_str());
326-
}
327-
Entry::Occupied(o) => {
328-
let x = o.get().clone();
329-
s.emit_u8(SYMBOL_OFFSET);
330-
s.emit_usize(x);
320+
// if symbol preinterned, emit tag and symbol index
321+
if self.is_preinterned() {
322+
s.opaque.emit_u8(SYMBOL_PREINTERNED);
323+
s.opaque.emit_u32(self.as_u32());
324+
} else {
325+
// otherwise write it as string or as offset to it
326+
match s.symbol_table.entry(*self) {
327+
Entry::Vacant(o) => {
328+
s.opaque.emit_u8(SYMBOL_STR);
329+
let pos = s.opaque.position();
330+
o.insert(pos);
331+
s.emit_str(self.as_str());
332+
}
333+
Entry::Occupied(o) => {
334+
let x = o.get().clone();
335+
s.emit_u8(SYMBOL_OFFSET);
336+
s.emit_usize(x);
337+
}
331338
}
332339
}
333340
}

compiler/rustc_metadata/src/rmeta/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ const TAG_PARTIAL_SPAN: u8 = 2;
448448
// Tags for encoding Symbol's
449449
const SYMBOL_STR: u8 = 0;
450450
const SYMBOL_OFFSET: u8 = 1;
451+
const SYMBOL_PREINTERNED: u8 = 2;
451452

452453
pub fn provide(providers: &mut Providers) {
453454
encoder::provide(providers);

compiler/rustc_query_impl/src/on_disk_cache.rs

+23-11
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const TAG_EXPN_DATA: u8 = 1;
4242
// Tags for encoding Symbol's
4343
const SYMBOL_STR: u8 = 0;
4444
const SYMBOL_OFFSET: u8 = 1;
45+
const SYMBOL_PREINTERNED: u8 = 2;
4546

4647
/// Provides an interface to incremental compilation data cached from the
4748
/// previous compilation session. This data will eventually include the results
@@ -745,6 +746,10 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for Symbol {
745746

746747
sym
747748
}
749+
SYMBOL_PREINTERNED => {
750+
let symbol_index = d.read_u32();
751+
Symbol::new_from_decoded(symbol_index)
752+
}
748753
_ => unreachable!(),
749754
}
750755
}
@@ -939,17 +944,24 @@ impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Span {
939944
// copy&paste impl from rustc_metadata
940945
impl<'a, 'tcx> Encodable<CacheEncoder<'a, 'tcx>> for Symbol {
941946
fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) {
942-
match s.symbol_table.entry(*self) {
943-
Entry::Vacant(o) => {
944-
s.encoder.emit_u8(SYMBOL_STR);
945-
let pos = s.encoder.position();
946-
o.insert(pos);
947-
s.emit_str(self.as_str());
948-
}
949-
Entry::Occupied(o) => {
950-
let x = o.get().clone();
951-
s.emit_u8(SYMBOL_OFFSET);
952-
s.emit_usize(x);
947+
// if symbol preinterned, emit tag and symbol index
948+
if self.is_preinterned() {
949+
s.encoder.emit_u8(SYMBOL_PREINTERNED);
950+
s.encoder.emit_u32(self.as_u32());
951+
} else {
952+
// otherwise write it as string or as offset to it
953+
match s.symbol_table.entry(*self) {
954+
Entry::Vacant(o) => {
955+
s.encoder.emit_u8(SYMBOL_STR);
956+
let pos = s.encoder.position();
957+
o.insert(pos);
958+
s.emit_str(self.as_str());
959+
}
960+
Entry::Occupied(o) => {
961+
let x = o.get().clone();
962+
s.emit_u8(SYMBOL_OFFSET);
963+
s.emit_usize(x);
964+
}
953965
}
954966
}
955967
}

compiler/rustc_span/src/symbol.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1804,6 +1804,11 @@ impl Symbol {
18041804
Symbol(SymbolIndex::from_u32(n))
18051805
}
18061806

1807+
/// for use in Decoder only
1808+
pub fn new_from_decoded(n: u32) -> Self {
1809+
Self::new(n)
1810+
}
1811+
18071812
/// Maps a string to its interned representation.
18081813
pub fn intern(string: &str) -> Self {
18091814
with_session_globals(|session_globals| session_globals.symbol_interner.intern(string))
@@ -2028,6 +2033,11 @@ impl Symbol {
20282033
pub fn can_be_raw(self) -> bool {
20292034
self != kw::Empty && self != kw::Underscore && !self.is_path_segment_keyword()
20302035
}
2036+
2037+
/// Is this symbol was interned in compiler's `symbols!` macro
2038+
pub fn is_preinterned(self) -> bool {
2039+
self.as_u32() < PREINTERNED_SYMBOLS_COUNT
2040+
}
20312041
}
20322042

20332043
impl Ident {

0 commit comments

Comments
 (0)