1
Fork 0

Remove almost all uses of specialization from the metadata encoding code

This commit is contained in:
bjorn3 2023-12-31 20:27:35 +00:00
parent 6ed37bdc42
commit 8d598b0d58
7 changed files with 307 additions and 290 deletions

View file

@ -61,7 +61,7 @@ pub use hygiene::{DesugaringKind, ExpnKind, MacroKind};
pub use hygiene::{ExpnData, ExpnHash, ExpnId, LocalExpnId, SyntaxContext};
use rustc_data_structures::stable_hasher::HashingControls;
pub mod def_id;
use def_id::{CrateNum, DefId, DefPathHash, LocalDefId, StableCrateId, LOCAL_CRATE};
use def_id::{CrateNum, DefId, DefIndex, DefPathHash, LocalDefId, StableCrateId, LOCAL_CRATE};
pub mod edit_distance;
mod span_encoding;
pub use span_encoding::{Span, DUMMY_SP};
@ -1021,6 +1021,14 @@ impl Default for Span {
pub trait SpanEncoder: Encoder {
fn encode_span(&mut self, span: Span);
fn encode_symbol(&mut self, symbol: Symbol);
fn encode_expn_id(&mut self, expn_id: ExpnId);
fn encode_syntax_context(&mut self, syntax_context: SyntaxContext);
/// As a local identifier, a `CrateNum` is only meaningful within its context, e.g. within a tcx.
/// Therefore, make sure to include the context when encode a `CrateNum`.
fn encode_crate_num(&mut self, crate_num: CrateNum);
fn encode_def_index(&mut self, def_index: DefIndex);
fn encode_def_id(&mut self, def_id: DefId);
}
impl SpanEncoder for FileEncoder {
@ -1029,6 +1037,31 @@ impl SpanEncoder for FileEncoder {
span.lo.encode(self);
span.hi.encode(self);
}
fn encode_symbol(&mut self, symbol: Symbol) {
self.emit_str(symbol.as_str());
}
fn encode_expn_id(&mut self, _expn_id: ExpnId) {
panic!("cannot encode `ExpnId` with `FileEncoder`");
}
fn encode_syntax_context(&mut self, _syntax_context: SyntaxContext) {
panic!("cannot encode `SyntaxContext` with `FileEncoder`");
}
fn encode_crate_num(&mut self, crate_num: CrateNum) {
self.emit_u32(crate_num.as_u32());
}
fn encode_def_index(&mut self, _def_index: DefIndex) {
panic!("cannot encode `DefIndex` with `FileEncoder`");
}
fn encode_def_id(&mut self, def_id: DefId) {
def_id.krate.encode(self);
def_id.index.encode(self);
}
}
impl<E: SpanEncoder> Encodable<E> for Span {
@ -1037,8 +1070,50 @@ impl<E: SpanEncoder> Encodable<E> for Span {
}
}
impl<E: SpanEncoder> Encodable<E> for Symbol {
fn encode(&self, s: &mut E) {
s.encode_symbol(*self);
}
}
impl<E: SpanEncoder> Encodable<E> for ExpnId {
fn encode(&self, s: &mut E) {
s.encode_expn_id(*self)
}
}
impl<E: SpanEncoder> Encodable<E> for SyntaxContext {
fn encode(&self, s: &mut E) {
s.encode_syntax_context(*self)
}
}
impl<E: SpanEncoder> Encodable<E> for CrateNum {
fn encode(&self, s: &mut E) {
s.encode_crate_num(*self)
}
}
impl<E: SpanEncoder> Encodable<E> for DefIndex {
fn encode(&self, s: &mut E) {
s.encode_def_index(*self)
}
}
impl<E: SpanEncoder> Encodable<E> for DefId {
fn encode(&self, s: &mut E) {
s.encode_def_id(*self)
}
}
pub trait SpanDecoder: Decoder {
fn decode_span(&mut self) -> Span;
fn decode_symbol(&mut self) -> Symbol;
fn decode_expn_id(&mut self) -> ExpnId;
fn decode_syntax_context(&mut self) -> SyntaxContext;
fn decode_crate_num(&mut self) -> CrateNum;
fn decode_def_index(&mut self) -> DefIndex;
fn decode_def_id(&mut self) -> DefId;
}
impl SpanDecoder for MemDecoder<'_> {
@ -1048,6 +1123,30 @@ impl SpanDecoder for MemDecoder<'_> {
Span::new(lo, hi, SyntaxContext::root(), None)
}
fn decode_symbol(&mut self) -> Symbol {
Symbol::intern(self.read_str())
}
fn decode_expn_id(&mut self) -> ExpnId {
panic!("cannot decode `ExpnId` with `MemDecoder`");
}
fn decode_syntax_context(&mut self) -> SyntaxContext {
panic!("cannot decode `SyntaxContext` with `MemDecoder`");
}
fn decode_crate_num(&mut self) -> CrateNum {
CrateNum::from_u32(self.read_u32())
}
fn decode_def_index(&mut self) -> DefIndex {
panic!("cannot decode `DefIndex` with `MemDecoder`");
}
fn decode_def_id(&mut self) -> DefId {
DefId { krate: Decodable::decode(self), index: Decodable::decode(self) }
}
}
impl<D: SpanDecoder> Decodable<D> for Span {
@ -1056,6 +1155,42 @@ impl<D: SpanDecoder> Decodable<D> for Span {
}
}
impl<D: SpanDecoder> Decodable<D> for Symbol {
fn decode(s: &mut D) -> Symbol {
s.decode_symbol()
}
}
impl<D: SpanDecoder> Decodable<D> for ExpnId {
fn decode(s: &mut D) -> ExpnId {
s.decode_expn_id()
}
}
impl<D: SpanDecoder> Decodable<D> for SyntaxContext {
fn decode(s: &mut D) -> SyntaxContext {
s.decode_syntax_context()
}
}
impl<D: SpanDecoder> Decodable<D> for CrateNum {
fn decode(s: &mut D) -> CrateNum {
s.decode_crate_num()
}
}
impl<D: SpanDecoder> Decodable<D> for DefIndex {
fn decode(s: &mut D) -> DefIndex {
s.decode_def_index()
}
}
impl<D: SpanDecoder> Decodable<D> for DefId {
fn decode(s: &mut D) -> DefId {
s.decode_def_id()
}
}
/// Insert `source_map` into the session globals for the duration of the
/// closure's execution.
pub fn set_source_map<T, F: FnOnce() -> T>(source_map: Lrc<SourceMap>, f: F) -> T {