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

@ -1,11 +1,11 @@
use crate::{HashStableContext, Symbol};
use crate::{HashStableContext, SpanDecoder, SpanEncoder, Symbol};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher, ToStableHashKey};
use rustc_data_structures::unhash::Unhasher;
use rustc_data_structures::AtomicRef;
use rustc_index::Idx;
use rustc_macros::HashStable_Generic;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_serialize::{Decodable, Encodable};
use std::fmt;
use std::hash::{BuildHasherDefault, Hash, Hasher};
@ -46,20 +46,6 @@ impl fmt::Display for CrateNum {
}
}
/// 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`.
impl<E: Encoder> Encodable<E> for CrateNum {
default fn encode(&self, s: &mut E) {
s.emit_u32(self.as_u32());
}
}
impl<D: Decoder> Decodable<D> for CrateNum {
default fn decode(d: &mut D) -> CrateNum {
CrateNum::from_u32(d.read_u32())
}
}
/// A `DefPathHash` is a fixed-size representation of a `DefPath` that is
/// stable across crate and compilation session boundaries. It consists of two
/// separate 64-bit hashes. The first uniquely identifies the crate this
@ -220,18 +206,6 @@ rustc_index::newtype_index! {
}
}
impl<E: Encoder> Encodable<E> for DefIndex {
default fn encode(&self, _: &mut E) {
panic!("cannot encode `DefIndex` with `{}`", std::any::type_name::<E>());
}
}
impl<D: Decoder> Decodable<D> for DefIndex {
default fn decode(_: &mut D) -> DefIndex {
panic!("cannot decode `DefIndex` with `{}`", std::any::type_name::<D>());
}
}
/// A `DefId` identifies a particular *definition*, by combining a crate
/// index and a def index.
///
@ -347,19 +321,6 @@ impl From<LocalDefId> for DefId {
}
}
impl<E: Encoder> Encodable<E> for DefId {
default fn encode(&self, s: &mut E) {
self.krate.encode(s);
self.index.encode(s);
}
}
impl<D: Decoder> Decodable<D> for DefId {
default fn decode(d: &mut D) -> DefId {
DefId { krate: Decodable::decode(d), index: Decodable::decode(d) }
}
}
pub fn default_def_id_debug(def_id: DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DefId").field("krate", &def_id.krate).field("index", &def_id.index).finish()
}
@ -423,13 +384,13 @@ impl fmt::Debug for LocalDefId {
}
}
impl<E: Encoder> Encodable<E> for LocalDefId {
impl<E: SpanEncoder> Encodable<E> for LocalDefId {
fn encode(&self, s: &mut E) {
self.to_def_id().encode(s);
}
}
impl<D: Decoder> Decodable<D> for LocalDefId {
impl<D: SpanDecoder> Decodable<D> for LocalDefId {
fn decode(d: &mut D) -> LocalDefId {
DefId::decode(d).expect_local()
}

View file

@ -27,7 +27,7 @@
use crate::def_id::{CrateNum, DefId, StableCrateId, CRATE_DEF_ID, LOCAL_CRATE};
use crate::edition::Edition;
use crate::symbol::{kw, sym, Symbol};
use crate::{with_session_globals, HashStableContext, Span, DUMMY_SP};
use crate::{with_session_globals, HashStableContext, Span, SpanDecoder, SpanEncoder, DUMMY_SP};
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::stable_hasher::{Hash64, HashStable, HashingControls, StableHasher};
@ -1431,30 +1431,18 @@ fn for_all_expns_in(
}
}
impl<E: Encoder> Encodable<E> for LocalExpnId {
impl<E: SpanEncoder> Encodable<E> for LocalExpnId {
fn encode(&self, e: &mut E) {
self.to_expn_id().encode(e);
}
}
impl<E: Encoder> Encodable<E> for ExpnId {
default fn encode(&self, _: &mut E) {
panic!("cannot encode `ExpnId` with `{}`", std::any::type_name::<E>());
}
}
impl<D: Decoder> Decodable<D> for LocalExpnId {
impl<D: SpanDecoder> Decodable<D> for LocalExpnId {
fn decode(d: &mut D) -> Self {
ExpnId::expect_local(ExpnId::decode(d))
}
}
impl<D: Decoder> Decodable<D> for ExpnId {
default fn decode(_: &mut D) -> Self {
panic!("cannot decode `ExpnId` with `{}`", std::any::type_name::<D>());
}
}
pub fn raw_encode_syntax_context<E: Encoder>(
ctxt: SyntaxContext,
context: &HygieneEncodeContext,
@ -1466,18 +1454,6 @@ pub fn raw_encode_syntax_context<E: Encoder>(
ctxt.0.encode(e);
}
impl<E: Encoder> Encodable<E> for SyntaxContext {
default fn encode(&self, _: &mut E) {
panic!("cannot encode `SyntaxContext` with `{}`", std::any::type_name::<E>());
}
}
impl<D: Decoder> Decodable<D> for SyntaxContext {
default fn decode(_: &mut D) -> Self {
panic!("cannot decode `SyntaxContext` with `{}`", std::any::type_name::<D>());
}
}
/// Updates the `disambiguator` field of the corresponding `ExpnData`
/// such that the `Fingerprint` of the `ExpnData` does not collide with
/// any other `ExpnIds`.

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 {

View file

@ -7,7 +7,6 @@ use rustc_data_structures::fx::FxIndexSet;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
use rustc_data_structures::sync::Lock;
use rustc_macros::HashStable_Generic;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use std::fmt;
use std::hash::{Hash, Hasher};
@ -2075,19 +2074,6 @@ impl ToString for Symbol {
}
}
impl<S: Encoder> Encodable<S> for Symbol {
default fn encode(&self, s: &mut S) {
s.emit_str(self.as_str());
}
}
impl<D: Decoder> Decodable<D> for Symbol {
#[inline]
default fn decode(d: &mut D) -> Symbol {
Symbol::intern(d.read_str())
}
}
impl<CTX> HashStable<CTX> for Symbol {
#[inline]
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {