From 6ed37bdc4260bdf64acf0cb2c728d6724c94ac3d Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 31 Dec 2023 19:35:32 +0000 Subject: [PATCH 1/6] Avoid specialization for the Span Encodable and Decodable impls --- Cargo.lock | 1 + compiler/rustc_abi/src/lib.rs | 14 ++-- compiler/rustc_ast/src/tokenstream.rs | 8 +-- .../rustc_data_structures/src/sorted_map.rs | 2 +- compiler/rustc_data_structures/src/svh.rs | 2 +- compiler/rustc_data_structures/src/unord.rs | 6 +- compiler/rustc_index/src/bit_set.rs | 8 +-- compiler/rustc_macros/src/lib.rs | 2 + compiler/rustc_macros/src/serialize.rs | 16 +++++ compiler/rustc_metadata/src/rmeta/decoder.rs | 18 ++--- compiler/rustc_metadata/src/rmeta/encoder.rs | 22 +++---- .../rustc_middle/src/query/on_disk_cache.rs | 65 ++++++++++--------- compiler/rustc_serialize/tests/opaque.rs | 16 ++--- compiler/rustc_span/src/lib.rs | 46 +++++++++---- compiler/rustc_type_ir/Cargo.toml | 2 + compiler/rustc_type_ir/src/codec.rs | 6 +- 16 files changed, 140 insertions(+), 94 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b8192e333fe91..1948e6c2e5ef6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4644,6 +4644,7 @@ dependencies = [ "rustc_index", "rustc_macros", "rustc_serialize", + "rustc_span", "smallvec", ] diff --git a/compiler/rustc_abi/src/lib.rs b/compiler/rustc_abi/src/lib.rs index 549927d58987c..ea194e10defd6 100644 --- a/compiler/rustc_abi/src/lib.rs +++ b/compiler/rustc_abi/src/lib.rs @@ -16,7 +16,7 @@ use rustc_data_structures::stable_hasher::StableOrd; #[cfg(feature = "nightly")] use rustc_macros::HashStable_Generic; #[cfg(feature = "nightly")] -use rustc_macros::{Decodable, Encodable}; +use rustc_macros::{Decodable_Generic, Encodable_Generic}; #[cfg(feature = "nightly")] use std::iter::Step; @@ -30,7 +30,7 @@ pub use layout::LayoutCalculator; pub trait HashStableContext {} #[derive(Clone, Copy, PartialEq, Eq, Default)] -#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))] +#[cfg_attr(feature = "nightly", derive(Encodable_Generic, Decodable_Generic, HashStable_Generic))] pub struct ReprFlags(u8); bitflags! { @@ -52,7 +52,7 @@ bitflags! { rustc_data_structures::external_bitflags_debug! { ReprFlags } #[derive(Copy, Clone, Debug, Eq, PartialEq)] -#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))] +#[cfg_attr(feature = "nightly", derive(Encodable_Generic, Decodable_Generic, HashStable_Generic))] pub enum IntegerType { /// Pointer-sized integer type, i.e. `isize` and `usize`. The field shows signedness, e.g. /// `Pointer(true)` means `isize`. @@ -73,7 +73,7 @@ impl IntegerType { /// Represents the repr options provided by the user. #[derive(Copy, Clone, Debug, Eq, PartialEq, Default)] -#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))] +#[cfg_attr(feature = "nightly", derive(Encodable_Generic, Decodable_Generic, HashStable_Generic))] pub struct ReprOptions { pub int: Option, pub align: Option, @@ -412,7 +412,7 @@ impl FromStr for Endian { /// Size of a type in bytes. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))] +#[cfg_attr(feature = "nightly", derive(Encodable_Generic, Decodable_Generic, HashStable_Generic))] pub struct Size { raw: u64, } @@ -636,7 +636,7 @@ impl Step for Size { /// Alignment of a type in bytes (always a power of two). #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] -#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))] +#[cfg_attr(feature = "nightly", derive(Encodable_Generic, Decodable_Generic, HashStable_Generic))] pub struct Align { pow2: u8, } @@ -777,7 +777,7 @@ impl AbiAndPrefAlign { /// Integers, also used for enum discriminants. #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] -#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))] +#[cfg_attr(feature = "nightly", derive(Encodable_Generic, Decodable_Generic, HashStable_Generic))] pub enum Integer { I8, I16, diff --git a/compiler/rustc_ast/src/tokenstream.rs b/compiler/rustc_ast/src/tokenstream.rs index 4c0c496584eb8..503862e6f0180 100644 --- a/compiler/rustc_ast/src/tokenstream.rs +++ b/compiler/rustc_ast/src/tokenstream.rs @@ -21,8 +21,8 @@ use crate::AttrVec; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::{self, Lrc}; use rustc_macros::HashStable_Generic; -use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; -use rustc_span::{sym, Span, Symbol, DUMMY_SP}; +use rustc_serialize::{Decodable, Encodable}; +use rustc_span::{sym, Span, SpanDecoder, SpanEncoder, Symbol, DUMMY_SP}; use smallvec::{smallvec, SmallVec}; use std::borrow::Cow; @@ -158,14 +158,14 @@ impl fmt::Debug for LazyAttrTokenStream { } } -impl Encodable for LazyAttrTokenStream { +impl Encodable for LazyAttrTokenStream { fn encode(&self, s: &mut S) { // Used by AST json printing. Encodable::encode(&self.to_attr_token_stream(), s); } } -impl Decodable for LazyAttrTokenStream { +impl Decodable for LazyAttrTokenStream { fn decode(_d: &mut D) -> Self { panic!("Attempted to decode LazyAttrTokenStream"); } diff --git a/compiler/rustc_data_structures/src/sorted_map.rs b/compiler/rustc_data_structures/src/sorted_map.rs index ed2e558bffa60..1436628139fde 100644 --- a/compiler/rustc_data_structures/src/sorted_map.rs +++ b/compiler/rustc_data_structures/src/sorted_map.rs @@ -16,7 +16,7 @@ pub use index_map::SortedIndexMultiMap; /// stores data in a more compact way. It also supports accessing contiguous /// ranges of elements as a slice, and slices of already sorted elements can be /// inserted efficiently. -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)] +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable_Generic, Decodable_Generic)] pub struct SortedMap { data: Vec<(K, V)>, } diff --git a/compiler/rustc_data_structures/src/svh.rs b/compiler/rustc_data_structures/src/svh.rs index 71679086f16c3..1cfc9fecd47d7 100644 --- a/compiler/rustc_data_structures/src/svh.rs +++ b/compiler/rustc_data_structures/src/svh.rs @@ -10,7 +10,7 @@ use std::fmt; use crate::stable_hasher; -#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable, Decodable, Hash)] +#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable_Generic, Decodable_Generic, Hash)] pub struct Svh { hash: Fingerprint, } diff --git a/compiler/rustc_data_structures/src/unord.rs b/compiler/rustc_data_structures/src/unord.rs index 47c56eba7adc8..9f8ddbd4256ed 100644 --- a/compiler/rustc_data_structures/src/unord.rs +++ b/compiler/rustc_data_structures/src/unord.rs @@ -185,7 +185,7 @@ trait UnordCollection {} /// /// See [MCP 533](https://github.com/rust-lang/compiler-team/issues/533) /// for more information. -#[derive(Debug, Eq, PartialEq, Clone, Encodable, Decodable)] +#[derive(Debug, Eq, PartialEq, Clone, Encodable_Generic, Decodable_Generic)] pub struct UnordSet { inner: FxHashSet, } @@ -362,7 +362,7 @@ impl> HashStable for UnordSet { /// /// See [MCP 533](https://github.com/rust-lang/compiler-team/issues/533) /// for more information. -#[derive(Debug, Eq, PartialEq, Clone, Encodable, Decodable)] +#[derive(Debug, Eq, PartialEq, Clone, Encodable_Generic, Decodable_Generic)] pub struct UnordMap { inner: FxHashMap, } @@ -558,7 +558,7 @@ impl, V: HashStable> HashStable fo /// /// See [MCP 533](https://github.com/rust-lang/compiler-team/issues/533) /// for more information. -#[derive(Default, Debug, Eq, PartialEq, Clone, Encodable, Decodable)] +#[derive(Default, Debug, Eq, PartialEq, Clone, Encodable_Generic, Decodable_Generic)] pub struct UnordBag { inner: Vec, } diff --git a/compiler/rustc_index/src/bit_set.rs b/compiler/rustc_index/src/bit_set.rs index d730ef58deba6..cba02548eb541 100644 --- a/compiler/rustc_index/src/bit_set.rs +++ b/compiler/rustc_index/src/bit_set.rs @@ -10,7 +10,7 @@ use arrayvec::ArrayVec; use smallvec::{smallvec, SmallVec}; #[cfg(feature = "nightly")] -use rustc_macros::{Decodable, Encodable}; +use rustc_macros::{Decodable_Generic, Encodable_Generic}; use crate::{Idx, IndexVec}; @@ -112,7 +112,7 @@ macro_rules! bit_relations_inherent_impls { /// to or greater than the domain size. All operations that involve two bitsets /// will panic if the bitsets have differing domain sizes. /// -#[cfg_attr(feature = "nightly", derive(Decodable, Encodable))] +#[cfg_attr(feature = "nightly", derive(Decodable_Generic, Encodable_Generic))] #[derive(Eq, PartialEq, Hash)] pub struct BitSet { domain_size: usize, @@ -1590,7 +1590,7 @@ impl From> for GrowableBitSet { /// /// All operations that involve a row and/or column index will panic if the /// index exceeds the relevant bound. -#[cfg_attr(feature = "nightly", derive(Decodable, Encodable))] +#[cfg_attr(feature = "nightly", derive(Decodable_Generic, Encodable_Generic))] #[derive(Clone, Eq, PartialEq, Hash)] pub struct BitMatrix { num_rows: usize, @@ -2020,7 +2020,7 @@ impl std::fmt::Debug for FiniteBitSet { /// A fixed-sized bitset type represented by an integer type. Indices outwith than the range /// representable by `T` are considered set. -#[cfg_attr(feature = "nightly", derive(Decodable, Encodable))] +#[cfg_attr(feature = "nightly", derive(Decodable_Generic, Encodable_Generic))] #[derive(Copy, Clone, Eq, PartialEq)] pub struct FiniteBitSet(pub T); diff --git a/compiler/rustc_macros/src/lib.rs b/compiler/rustc_macros/src/lib.rs index f558b74be9a99..f5d942b924e6c 100644 --- a/compiler/rustc_macros/src/lib.rs +++ b/compiler/rustc_macros/src/lib.rs @@ -56,6 +56,8 @@ decl_derive!( hash_stable::hash_stable_no_context_derive ); +decl_derive!([Decodable_Generic] => serialize::decodable_generic_derive); +decl_derive!([Encodable_Generic] => serialize::encodable_generic_derive); decl_derive!([Decodable] => serialize::decodable_derive); decl_derive!([Encodable] => serialize::encodable_derive); decl_derive!([TyDecodable] => serialize::type_decodable_derive); diff --git a/compiler/rustc_macros/src/serialize.rs b/compiler/rustc_macros/src/serialize.rs index 047066ac6815e..9ca7ce09ba6c7 100644 --- a/compiler/rustc_macros/src/serialize.rs +++ b/compiler/rustc_macros/src/serialize.rs @@ -31,6 +31,14 @@ pub fn meta_decodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2: } pub fn decodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream { + let decoder_ty = quote! { __D }; + s.add_impl_generic(parse_quote! {#decoder_ty: ::rustc_span::SpanDecoder}); + s.add_bounds(synstructure::AddBounds::Generics); + + decodable_body(s, decoder_ty) +} + +pub fn decodable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream { let decoder_ty = quote! { __D }; s.add_impl_generic(parse_quote! {#decoder_ty: ::rustc_serialize::Decoder}); s.add_bounds(synstructure::AddBounds::Generics); @@ -129,6 +137,14 @@ pub fn meta_encodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2: } pub fn encodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream { + let encoder_ty = quote! { __E }; + s.add_impl_generic(parse_quote! { #encoder_ty: ::rustc_span::SpanEncoder}); + s.add_bounds(synstructure::AddBounds::Generics); + + encodable_body(s, encoder_ty, false) +} + +pub fn encodable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream { let encoder_ty = quote! { __E }; s.add_impl_generic(parse_quote! { #encoder_ty: ::rustc_serialize::Encoder}); s.add_bounds(synstructure::AddBounds::Generics); diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 2de29db9e5c84..2dffd5446a312 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -26,7 +26,7 @@ use rustc_serialize::{Decodable, Decoder}; use rustc_session::cstore::{CrateSource, ExternCrate}; use rustc_session::Session; use rustc_span::symbol::kw; -use rustc_span::{BytePos, Pos, SpanData, SyntaxContext, DUMMY_SP}; +use rustc_span::{BytePos, Pos, SpanData, SpanDecoder, SyntaxContext, DUMMY_SP}; use proc_macro::bridge::client::ProcMacro; use std::iter::TrustedLen; @@ -505,22 +505,22 @@ impl<'a, 'tcx> Decodable> for ExpnId { } } -impl<'a, 'tcx> Decodable> for Span { - fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> Span { - let start = decoder.position(); - let tag = SpanTag(decoder.peek_byte()); +impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> { + fn decode_span(&mut self) -> Span { + let start = self.position(); + let tag = SpanTag(self.peek_byte()); let data = if tag.kind() == SpanKind::Indirect { // Skip past the tag we just peek'd. - decoder.read_u8(); - let offset_or_position = decoder.read_usize(); + self.read_u8(); + let offset_or_position = self.read_usize(); let position = if tag.is_relative_offset() { start - offset_or_position } else { offset_or_position }; - decoder.with_position(position, SpanData::decode) + self.with_position(position, SpanData::decode) } else { - SpanData::decode(decoder) + SpanData::decode(self) }; Span::new(data.lo, data.hi, data.ctxt, data.parent) } diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index aca7a66596e64..dbd82d3ab79be 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -27,7 +27,7 @@ use rustc_session::config::{CrateType, OptLevel}; use rustc_span::hygiene::HygieneEncodeContext; use rustc_span::symbol::sym; use rustc_span::{ - ExternalSource, FileName, SourceFile, SpanData, StableSourceFileId, SyntaxContext, + ExternalSource, FileName, SourceFile, SpanData, SpanEncoder, StableSourceFileId, SyntaxContext, }; use std::borrow::Borrow; use std::collections::hash_map::Entry; @@ -166,29 +166,29 @@ impl<'a, 'tcx> Encodable> for ExpnId { } } -impl<'a, 'tcx> Encodable> for Span { - fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { - match s.span_shorthands.entry(*self) { +impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> { + fn encode_span(&mut self, span: Span) { + match self.span_shorthands.entry(span) { Entry::Occupied(o) => { // If an offset is smaller than the absolute position, we encode with the offset. // This saves space since smaller numbers encode in less bits. let last_location = *o.get(); // This cannot underflow. Metadata is written with increasing position(), so any // previously saved offset must be smaller than the current position. - let offset = s.opaque.position() - last_location; + let offset = self.opaque.position() - last_location; if offset < last_location { - SpanTag::indirect(true).encode(s); - offset.encode(s); + SpanTag::indirect(true).encode(self); + offset.encode(self); } else { - SpanTag::indirect(false).encode(s); - last_location.encode(s); + SpanTag::indirect(false).encode(self); + last_location.encode(self); } } Entry::Vacant(v) => { - let position = s.opaque.position(); + let position = self.opaque.position(); v.insert(position); // Data is encoded with a SpanTag prefix (see below). - self.data().encode(s); + span.data().encode(self); } } } diff --git a/compiler/rustc_middle/src/query/on_disk_cache.rs b/compiler/rustc_middle/src/query/on_disk_cache.rs index 0577d22d85086..f8a34e05eb059 100644 --- a/compiler/rustc_middle/src/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/query/on_disk_cache.rs @@ -22,7 +22,8 @@ use rustc_span::hygiene::{ }; use rustc_span::source_map::SourceMap; use rustc_span::{ - BytePos, ExpnData, ExpnHash, Pos, RelativeBytePos, SourceFile, Span, StableSourceFileId, + BytePos, ExpnData, ExpnHash, Pos, RelativeBytePos, SourceFile, Span, SpanDecoder, SpanEncoder, + StableSourceFileId, }; use rustc_span::{CachingSourceMapView, Symbol}; use std::collections::hash_map::Entry; @@ -648,19 +649,19 @@ impl<'a, 'tcx> Decodable> for ExpnId { } } -impl<'a, 'tcx> Decodable> for Span { - fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Self { - let ctxt = SyntaxContext::decode(decoder); - let parent = Option::::decode(decoder); - let tag: u8 = Decodable::decode(decoder); +impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> { + fn decode_span(&mut self) -> Span { + let ctxt = SyntaxContext::decode(self); + let parent = Option::::decode(self); + let tag: u8 = Decodable::decode(self); if tag == TAG_PARTIAL_SPAN { return Span::new(BytePos(0), BytePos(0), ctxt, parent); } else if tag == TAG_RELATIVE_SPAN { - let dlo = u32::decode(decoder); - let dto = u32::decode(decoder); + let dlo = u32::decode(self); + let dto = u32::decode(self); - let enclosing = decoder.tcx.source_span_untracked(parent.unwrap()).data_untracked(); + let enclosing = self.tcx.source_span_untracked(parent.unwrap()).data_untracked(); let span = Span::new( enclosing.lo + BytePos::from_u32(dlo), enclosing.lo + BytePos::from_u32(dto), @@ -673,12 +674,12 @@ impl<'a, 'tcx> Decodable> for Span { debug_assert_eq!(tag, TAG_FULL_SPAN); } - let file_lo_index = SourceFileIndex::decode(decoder); - let line_lo = usize::decode(decoder); - let col_lo = RelativeBytePos::decode(decoder); - let len = BytePos::decode(decoder); + let file_lo_index = SourceFileIndex::decode(self); + let line_lo = usize::decode(self); + let col_lo = RelativeBytePos::decode(self); + let len = BytePos::decode(self); - let file_lo = decoder.file_index_to_file(file_lo_index); + let file_lo = self.file_index_to_file(file_lo_index); let lo = file_lo.lines()[line_lo - 1] + col_lo; let lo = file_lo.absolute_position(lo); let hi = lo + len; @@ -872,47 +873,47 @@ impl<'a, 'tcx> Encodable> for ExpnId { } } -impl<'a, 'tcx> Encodable> for Span { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { - let span_data = self.data_untracked(); - span_data.ctxt.encode(s); - span_data.parent.encode(s); +impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> { + fn encode_span(&mut self, span: Span) { + let span_data = span.data_untracked(); + span_data.ctxt.encode(self); + span_data.parent.encode(self); if span_data.is_dummy() { - return TAG_PARTIAL_SPAN.encode(s); + return TAG_PARTIAL_SPAN.encode(self); } if let Some(parent) = span_data.parent { - let enclosing = s.tcx.source_span_untracked(parent).data_untracked(); + let enclosing = self.tcx.source_span_untracked(parent).data_untracked(); if enclosing.contains(span_data) { - TAG_RELATIVE_SPAN.encode(s); - (span_data.lo - enclosing.lo).to_u32().encode(s); - (span_data.hi - enclosing.lo).to_u32().encode(s); + TAG_RELATIVE_SPAN.encode(self); + (span_data.lo - enclosing.lo).to_u32().encode(self); + (span_data.hi - enclosing.lo).to_u32().encode(self); return; } } - let pos = s.source_map.byte_pos_to_line_and_col(span_data.lo); + let pos = self.source_map.byte_pos_to_line_and_col(span_data.lo); let partial_span = match &pos { Some((file_lo, _, _)) => !file_lo.contains(span_data.hi), None => true, }; if partial_span { - return TAG_PARTIAL_SPAN.encode(s); + return TAG_PARTIAL_SPAN.encode(self); } let (file_lo, line_lo, col_lo) = pos.unwrap(); let len = span_data.hi - span_data.lo; - let source_file_index = s.source_file_index(file_lo); + let source_file_index = self.source_file_index(file_lo); - TAG_FULL_SPAN.encode(s); - source_file_index.encode(s); - line_lo.encode(s); - col_lo.encode(s); - len.encode(s); + TAG_FULL_SPAN.encode(self); + source_file_index.encode(self); + line_lo.encode(self); + col_lo.encode(self); + len.encode(self); } } diff --git a/compiler/rustc_serialize/tests/opaque.rs b/compiler/rustc_serialize/tests/opaque.rs index 861091688bb2d..45ff85f38d2bf 100644 --- a/compiler/rustc_serialize/tests/opaque.rs +++ b/compiler/rustc_serialize/tests/opaque.rs @@ -1,12 +1,12 @@ #![allow(rustc::internal)] -use rustc_macros::{Decodable, Encodable}; -use rustc_serialize::opaque::{MemDecoder, FileEncoder}; +use rustc_macros::{Decodable_Generic, Encodable_Generic}; +use rustc_serialize::opaque::{FileEncoder, MemDecoder}; use rustc_serialize::{Decodable, Encodable}; use std::fmt::Debug; use std::fs; -#[derive(PartialEq, Clone, Debug, Encodable, Decodable)] +#[derive(PartialEq, Clone, Debug, Encodable_Generic, Decodable_Generic)] struct Struct { a: (), b: u8, @@ -209,7 +209,7 @@ fn test_struct() { }]); } -#[derive(PartialEq, Clone, Debug, Encodable, Decodable)] +#[derive(PartialEq, Clone, Debug, Encodable_Generic, Decodable_Generic)] enum Enum { Variant1, Variant2(usize, u32), @@ -258,7 +258,7 @@ fn test_tuples() { #[test] fn test_unit_like_struct() { - #[derive(Encodable, Decodable, PartialEq, Debug)] + #[derive(Encodable_Generic, Decodable_Generic, PartialEq, Debug)] struct UnitLikeStruct; check_round_trip(vec![UnitLikeStruct]); @@ -266,7 +266,7 @@ fn test_unit_like_struct() { #[test] fn test_box() { - #[derive(Encodable, Decodable, PartialEq, Debug)] + #[derive(Encodable_Generic, Decodable_Generic, PartialEq, Debug)] struct A { foo: Box<[bool]>, } @@ -279,12 +279,12 @@ fn test_box() { fn test_cell() { use std::cell::{Cell, RefCell}; - #[derive(Encodable, Decodable, PartialEq, Debug)] + #[derive(Encodable_Generic, Decodable_Generic, PartialEq, Debug)] struct A { baz: isize, } - #[derive(Encodable, Decodable, PartialEq, Debug)] + #[derive(Encodable_Generic, Decodable_Generic, PartialEq, Debug)] struct B { foo: Cell, bar: RefCell, diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 8f64eed9a870d..0eafbae6e82bf 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -35,6 +35,8 @@ #![feature(rustdoc_internals)] // tidy-alphabetical-end +extern crate self as rustc_span; + #[macro_use] extern crate rustc_macros; @@ -43,6 +45,7 @@ extern crate tracing; use rustc_data_structures::{outline, AtomicRef}; use rustc_macros::HashStable_Generic; +use rustc_serialize::opaque::{FileEncoder, MemDecoder}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; mod caching_source_map_view; @@ -1016,22 +1019,43 @@ impl Default for Span { } } -impl Encodable for Span { - default fn encode(&self, s: &mut E) { - let span = self.data(); - span.lo.encode(s); - span.hi.encode(s); +pub trait SpanEncoder: Encoder { + fn encode_span(&mut self, span: Span); +} + +impl SpanEncoder for FileEncoder { + fn encode_span(&mut self, span: Span) { + let span = span.data(); + span.lo.encode(self); + span.hi.encode(self); + } +} + +impl Encodable for Span { + fn encode(&self, s: &mut E) { + s.encode_span(*self); } } -impl Decodable for Span { - default fn decode(s: &mut D) -> Span { - let lo = Decodable::decode(s); - let hi = Decodable::decode(s); + +pub trait SpanDecoder: Decoder { + fn decode_span(&mut self) -> Span; +} + +impl SpanDecoder for MemDecoder<'_> { + fn decode_span(&mut self) -> Span { + let lo = Decodable::decode(self); + let hi = Decodable::decode(self); Span::new(lo, hi, SyntaxContext::root(), None) } } +impl Decodable for Span { + fn decode(s: &mut D) -> Span { + s.decode_span() + } +} + /// Insert `source_map` into the session globals for the duration of the /// closure's execution. pub fn set_source_map T>(source_map: Lrc, f: F) -> T { @@ -1360,7 +1384,7 @@ impl Clone for SourceFile { } } -impl Encodable for SourceFile { +impl Encodable for SourceFile { fn encode(&self, s: &mut S) { self.name.encode(s); self.src_hash.encode(s); @@ -1434,7 +1458,7 @@ impl Encodable for SourceFile { } } -impl Decodable for SourceFile { +impl Decodable for SourceFile { fn decode(d: &mut D) -> SourceFile { let name: FileName = Decodable::decode(d); let src_hash: SourceFileHash = Decodable::decode(d); diff --git a/compiler/rustc_type_ir/Cargo.toml b/compiler/rustc_type_ir/Cargo.toml index 38f0eb8218015..3a94256f402f9 100644 --- a/compiler/rustc_type_ir/Cargo.toml +++ b/compiler/rustc_type_ir/Cargo.toml @@ -10,6 +10,7 @@ derivative = "2.2.0" rustc_data_structures = { path = "../rustc_data_structures", optional = true } rustc_index = { path = "../rustc_index", default-features = false } rustc_macros = { path = "../rustc_macros", optional = true } +rustc_span = { path = "../rustc_span", optional = true } rustc_serialize = { path = "../rustc_serialize", optional = true } smallvec = { version = "1.8.1" } # tidy-alphabetical-end @@ -20,6 +21,7 @@ nightly = [ "smallvec/may_dangle", "smallvec/union", "rustc_index/nightly", + "rustc_span", "rustc_serialize", "rustc_data_structures", "rustc_macros", diff --git a/compiler/rustc_type_ir/src/codec.rs b/compiler/rustc_type_ir/src/codec.rs index 2fbc8f76fa4f9..71f9eb0ef8a82 100644 --- a/compiler/rustc_type_ir/src/codec.rs +++ b/compiler/rustc_type_ir/src/codec.rs @@ -1,7 +1,7 @@ use crate::{Interner, PredicateKind}; use rustc_data_structures::fx::FxHashMap; -use rustc_serialize::{Decoder, Encoder}; +use rustc_span::{SpanDecoder, SpanEncoder}; /// The shorthand encoding uses an enum's variant index `usize` /// and is offset by this value so it never matches a real variant. @@ -22,7 +22,7 @@ pub trait RefDecodable<'tcx, D: TyDecoder> { fn decode(d: &mut D) -> &'tcx Self; } -pub trait TyEncoder: Encoder { +pub trait TyEncoder: SpanEncoder { type I: Interner; const CLEAR_CROSS_CRATE: bool; @@ -35,7 +35,7 @@ pub trait TyEncoder: Encoder { fn encode_alloc_id(&mut self, alloc_id: &::AllocId); } -pub trait TyDecoder: Decoder { +pub trait TyDecoder: SpanDecoder { type I: Interner; const CLEAR_CROSS_CRATE: bool; From 8d598b0d581872c5222399f500dd5f9f3f3aa1c3 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 31 Dec 2023 20:27:35 +0000 Subject: [PATCH 2/6] Remove almost all uses of specialization from the metadata encoding code --- compiler/rustc_metadata/src/rmeta/decoder.rs | 106 ++++++------ compiler/rustc_metadata/src/rmeta/encoder.rs | 93 +++++------ .../rustc_middle/src/query/on_disk_cache.rs | 158 +++++++----------- compiler/rustc_span/src/def_id.rs | 47 +----- compiler/rustc_span/src/hygiene.rs | 30 +--- compiler/rustc_span/src/lib.rs | 137 ++++++++++++++- compiler/rustc_span/src/symbol.rs | 14 -- 7 files changed, 301 insertions(+), 284 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 2dffd5446a312..d20f4bc7a9417 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -409,21 +409,6 @@ impl<'a, 'tcx> TyDecoder for DecodeContext<'a, 'tcx> { } } -impl<'a, 'tcx> Decodable> for CrateNum { - #[inline] - fn decode(d: &mut DecodeContext<'a, 'tcx>) -> CrateNum { - let cnum = CrateNum::from_u32(d.read_u32()); - d.map_encoded_cnum_to_current(cnum) - } -} - -impl<'a, 'tcx> Decodable> for DefIndex { - #[inline] - fn decode(d: &mut DecodeContext<'a, 'tcx>) -> DefIndex { - DefIndex::from_u32(d.read_u32()) - } -} - impl<'a, 'tcx> Decodable> for ExpnIndex { #[inline] fn decode(d: &mut DecodeContext<'a, 'tcx>) -> ExpnIndex { @@ -439,11 +424,24 @@ impl<'a, 'tcx> Decodable> for ast::AttrId { } } -impl<'a, 'tcx> Decodable> for SyntaxContext { - fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> SyntaxContext { - let cdata = decoder.cdata(); +impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> { + fn decode_crate_num(&mut self) -> CrateNum { + let cnum = CrateNum::from_u32(self.read_u32()); + self.map_encoded_cnum_to_current(cnum) + } - let Some(sess) = decoder.sess else { + fn decode_def_index(&mut self) -> DefIndex { + DefIndex::from_u32(self.read_u32()) + } + + fn decode_def_id(&mut self) -> DefId { + DefId { krate: Decodable::decode(self), index: Decodable::decode(self) } + } + + fn decode_syntax_context(&mut self) -> SyntaxContext { + let cdata = self.cdata(); + + let Some(sess) = self.sess else { bug!( "Cannot decode SyntaxContext without Session.\ You need to explicitly pass `(crate_metadata_ref, tcx)` to `decode` instead of just `crate_metadata_ref`." @@ -451,7 +449,7 @@ impl<'a, 'tcx> Decodable> for SyntaxContext { }; let cname = cdata.root.name(); - rustc_span::hygiene::decode_syntax_context(decoder, &cdata.hygiene_context, |_, id| { + rustc_span::hygiene::decode_syntax_context(self, &cdata.hygiene_context, |_, id| { debug!("SpecializedDecoder: decoding {}", id); cdata .root @@ -461,21 +459,19 @@ impl<'a, 'tcx> Decodable> for SyntaxContext { .decode((cdata, sess)) }) } -} -impl<'a, 'tcx> Decodable> for ExpnId { - fn decode(decoder: &mut DecodeContext<'a, 'tcx>) -> ExpnId { - let local_cdata = decoder.cdata(); + fn decode_expn_id(&mut self) -> ExpnId { + let local_cdata = self.cdata(); - let Some(sess) = decoder.sess else { + let Some(sess) = self.sess else { bug!( "Cannot decode ExpnId without Session. \ You need to explicitly pass `(crate_metadata_ref, tcx)` to `decode` instead of just `crate_metadata_ref`." ); }; - let cnum = CrateNum::decode(decoder); - let index = u32::decode(decoder); + let cnum = CrateNum::decode(self); + let index = u32::decode(self); let expn_id = rustc_span::hygiene::decode_expn_id(cnum, index, |expn_id| { let ExpnId { krate: cnum, local_id: index } = expn_id; @@ -503,9 +499,7 @@ impl<'a, 'tcx> Decodable> for ExpnId { }); expn_id } -} -impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> { fn decode_span(&mut self) -> Span { let start = self.position(); let tag = SpanTag(self.peek_byte()); @@ -524,6 +518,32 @@ impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> { }; Span::new(data.lo, data.hi, data.ctxt, data.parent) } + + fn decode_symbol(&mut self) -> Symbol { + let tag = self.read_u8(); + + match tag { + SYMBOL_STR => { + let s = self.read_str(); + Symbol::intern(s) + } + SYMBOL_OFFSET => { + // read str offset + let pos = self.read_usize(); + + // move to str offset and read + self.opaque.with_position(pos, |d| { + let s = d.read_str(); + Symbol::intern(s) + }) + } + SYMBOL_PREINTERNED => { + let symbol_index = self.read_u32(); + Symbol::new_from_decoded(symbol_index) + } + _ => unreachable!(), + } + } } impl<'a, 'tcx> Decodable> for SpanData { @@ -631,34 +651,6 @@ impl<'a, 'tcx> Decodable> for SpanData { } } -impl<'a, 'tcx> Decodable> for Symbol { - fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self { - let tag = d.read_u8(); - - match tag { - SYMBOL_STR => { - let s = d.read_str(); - Symbol::intern(s) - } - SYMBOL_OFFSET => { - // read str offset - let pos = d.read_usize(); - - // move to str offset and read - d.opaque.with_position(pos, |d| { - let s = d.read_str(); - Symbol::intern(s) - }) - } - SYMBOL_PREINTERNED => { - let symbol_index = d.read_u32(); - Symbol::new_from_decoded(symbol_index) - } - _ => unreachable!(), - } - } -} - impl<'a, 'tcx> Decodable> for &'tcx [(ty::Clause<'tcx>, Span)] { fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self { ty::codec::RefDecodable::decode(d) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index dbd82d3ab79be..2ed4385df0851 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -125,48 +125,45 @@ impl<'a, 'tcx, I, T> Encodable> for LazyTable { } } -impl<'a, 'tcx> Encodable> for CrateNum { +impl<'a, 'tcx> Encodable> for ExpnIndex { fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { - if *self != LOCAL_CRATE && s.is_proc_macro { - panic!("Attempted to encode non-local CrateNum {self:?} for proc-macro crate"); - } s.emit_u32(self.as_u32()); } } -impl<'a, 'tcx> Encodable> for DefIndex { - fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { - s.emit_u32(self.as_u32()); +impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> { + fn encode_crate_num(&mut self, crate_num: CrateNum) { + if crate_num != LOCAL_CRATE && self.is_proc_macro { + panic!("Attempted to encode non-local CrateNum {crate_num:?} for proc-macro crate"); + } + self.emit_u32(crate_num.as_u32()); } -} -impl<'a, 'tcx> Encodable> for ExpnIndex { - fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { - s.emit_u32(self.as_u32()); + fn encode_def_index(&mut self, def_index: DefIndex) { + self.emit_u32(def_index.as_u32()); } -} -impl<'a, 'tcx> Encodable> for SyntaxContext { - fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { - rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_ctxt, s); + fn encode_def_id(&mut self, def_id: DefId) { + def_id.krate.encode(self); + def_id.index.encode(self); } -} -impl<'a, 'tcx> Encodable> for ExpnId { - fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { - if self.krate == LOCAL_CRATE { + fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) { + rustc_span::hygiene::raw_encode_syntax_context(syntax_context, self.hygiene_ctxt, self); + } + + fn encode_expn_id(&mut self, expn_id: ExpnId) { + if expn_id.krate == LOCAL_CRATE { // We will only write details for local expansions. Non-local expansions will fetch // data from the corresponding crate's metadata. // FIXME(#43047) FIXME(#74731) We may eventually want to avoid relying on external // metadata from proc-macro crates. - s.hygiene_ctxt.schedule_expn_data_for_encoding(*self); + self.hygiene_ctxt.schedule_expn_data_for_encoding(expn_id); } - self.krate.encode(s); - self.local_id.encode(s); + expn_id.krate.encode(self); + expn_id.local_id.encode(self); } -} -impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> { fn encode_span(&mut self, span: Span) { match self.span_shorthands.entry(span) { Entry::Occupied(o) => { @@ -192,6 +189,29 @@ impl<'a, 'tcx> SpanEncoder for EncodeContext<'a, 'tcx> { } } } + + fn encode_symbol(&mut self, symbol: Symbol) { + // if symbol preinterned, emit tag and symbol index + if symbol.is_preinterned() { + self.opaque.emit_u8(SYMBOL_PREINTERNED); + self.opaque.emit_u32(symbol.as_u32()); + } else { + // otherwise write it as string or as offset to it + match self.symbol_table.entry(symbol) { + Entry::Vacant(o) => { + self.opaque.emit_u8(SYMBOL_STR); + let pos = self.opaque.position(); + o.insert(pos); + self.emit_str(symbol.as_str()); + } + Entry::Occupied(o) => { + let x = *o.get(); + self.emit_u8(SYMBOL_OFFSET); + self.emit_usize(x); + } + } + } + } } impl<'a, 'tcx> Encodable> for SpanData { @@ -337,31 +357,6 @@ impl<'a, 'tcx> Encodable> for SpanData { } } -impl<'a, 'tcx> Encodable> for Symbol { - fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { - // if symbol preinterned, emit tag and symbol index - if self.is_preinterned() { - s.opaque.emit_u8(SYMBOL_PREINTERNED); - s.opaque.emit_u32(self.as_u32()); - } else { - // otherwise write it as string or as offset to it - match s.symbol_table.entry(*self) { - Entry::Vacant(o) => { - s.opaque.emit_u8(SYMBOL_STR); - let pos = s.opaque.position(); - o.insert(pos); - s.emit_str(self.as_str()); - } - Entry::Occupied(o) => { - let x = *o.get(); - s.emit_u8(SYMBOL_OFFSET); - s.emit_usize(x); - } - } - } - } -} - impl<'a, 'tcx> Encodable> for [u8] { fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) { Encoder::emit_usize(e, self.len()); diff --git a/compiler/rustc_middle/src/query/on_disk_cache.rs b/compiler/rustc_middle/src/query/on_disk_cache.rs index f8a34e05eb059..8d084f5f13118 100644 --- a/compiler/rustc_middle/src/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/query/on_disk_cache.rs @@ -583,10 +583,10 @@ impl<'a, 'tcx> Decodable> for Vec { } } -impl<'a, 'tcx> Decodable> for SyntaxContext { - fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Self { - let syntax_contexts = decoder.syntax_contexts; - rustc_span::hygiene::decode_syntax_context(decoder, decoder.hygiene_context, |this, id| { +impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> { + fn decode_syntax_context(&mut self) -> SyntaxContext { + let syntax_contexts = self.syntax_contexts; + rustc_span::hygiene::decode_syntax_context(self, self.hygiene_context, |this, id| { // This closure is invoked if we haven't already decoded the data for the `SyntaxContext` we are deserializing. // We look up the position of the associated `SyntaxData` and decode it. let pos = syntax_contexts.get(&id).unwrap(); @@ -596,11 +596,9 @@ impl<'a, 'tcx> Decodable> for SyntaxContext { }) }) } -} -impl<'a, 'tcx> Decodable> for ExpnId { - fn decode(decoder: &mut CacheDecoder<'a, 'tcx>) -> Self { - let hash = ExpnHash::decode(decoder); + fn decode_expn_id(&mut self) -> ExpnId { + let hash = ExpnHash::decode(self); if hash.is_root() { return ExpnId::root(); } @@ -609,23 +607,23 @@ impl<'a, 'tcx> Decodable> for ExpnId { return expn_id; } - let krate = decoder.tcx.stable_crate_id_to_crate_num(hash.stable_crate_id()); + let krate = self.tcx.stable_crate_id_to_crate_num(hash.stable_crate_id()); let expn_id = if krate == LOCAL_CRATE { // We look up the position of the associated `ExpnData` and decode it. - let pos = decoder + let pos = self .expn_data .get(&hash) - .unwrap_or_else(|| panic!("Bad hash {:?} (map {:?})", hash, decoder.expn_data)); + .unwrap_or_else(|| panic!("Bad hash {:?} (map {:?})", hash, self.expn_data)); - let data: ExpnData = decoder - .with_position(pos.to_usize(), |decoder| decode_tagged(decoder, TAG_EXPN_DATA)); + let data: ExpnData = + self.with_position(pos.to_usize(), |decoder| decode_tagged(decoder, TAG_EXPN_DATA)); let expn_id = rustc_span::hygiene::register_local_expn_id(data, hash); #[cfg(debug_assertions)] { use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; - let local_hash = decoder.tcx.with_stable_hashing_context(|mut hcx| { + let local_hash = self.tcx.with_stable_hashing_context(|mut hcx| { let mut hasher = StableHasher::new(); expn_id.expn_data().hash_stable(&mut hcx, &mut hasher); hasher.finish() @@ -635,9 +633,9 @@ impl<'a, 'tcx> Decodable> for ExpnId { expn_id } else { - let index_guess = decoder.foreign_expn_data[&hash]; - decoder.tcx.cstore_untracked().expn_hash_to_expn_id( - decoder.tcx.sess, + let index_guess = self.foreign_expn_data[&hash]; + self.tcx.cstore_untracked().expn_hash_to_expn_id( + self.tcx.sess, krate, index_guess, hash, @@ -647,9 +645,7 @@ impl<'a, 'tcx> Decodable> for ExpnId { debug_assert_eq!(expn_id.krate, krate); expn_id } -} -impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> { fn decode_span(&mut self) -> Span { let ctxt = SyntaxContext::decode(self); let parent = Option::::decode(self); @@ -686,72 +682,62 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> { Span::new(lo, hi, ctxt, parent) } -} -// copy&paste impl from rustc_metadata -impl<'a, 'tcx> Decodable> for Symbol { + // copy&paste impl from rustc_metadata #[inline] - fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self { - let tag = d.read_u8(); + fn decode_symbol(&mut self) -> Symbol { + let tag = self.read_u8(); match tag { SYMBOL_STR => { - let s = d.read_str(); + let s = self.read_str(); Symbol::intern(s) } SYMBOL_OFFSET => { // read str offset - let pos = d.read_usize(); + let pos = self.read_usize(); // move to str offset and read - d.opaque.with_position(pos, |d| { + self.opaque.with_position(pos, |d| { let s = d.read_str(); Symbol::intern(s) }) } SYMBOL_PREINTERNED => { - let symbol_index = d.read_u32(); + let symbol_index = self.read_u32(); Symbol::new_from_decoded(symbol_index) } _ => unreachable!(), } } -} -impl<'a, 'tcx> Decodable> for CrateNum { - #[inline] - fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self { - let stable_id = StableCrateId::decode(d); - let cnum = d.tcx.stable_crate_id_to_crate_num(stable_id); + fn decode_crate_num(&mut self) -> CrateNum { + let stable_id = StableCrateId::decode(self); + let cnum = self.tcx.stable_crate_id_to_crate_num(stable_id); cnum } -} -// This impl makes sure that we get a runtime error when we try decode a -// `DefIndex` that is not contained in a `DefId`. Such a case would be problematic -// because we would not know how to transform the `DefIndex` to the current -// context. -impl<'a, 'tcx> Decodable> for DefIndex { - fn decode(_d: &mut CacheDecoder<'a, 'tcx>) -> DefIndex { + // This impl makes sure that we get a runtime error when we try decode a + // `DefIndex` that is not contained in a `DefId`. Such a case would be problematic + // because we would not know how to transform the `DefIndex` to the current + // context. + fn decode_def_index(&mut self) -> DefIndex { panic!("trying to decode `DefIndex` outside the context of a `DefId`") } -} -// Both the `CrateNum` and the `DefIndex` of a `DefId` can change in between two -// compilation sessions. We use the `DefPathHash`, which is stable across -// sessions, to map the old `DefId` to the new one. -impl<'a, 'tcx> Decodable> for DefId { - #[inline] - fn decode(d: &mut CacheDecoder<'a, 'tcx>) -> Self { + // Both the `CrateNum` and the `DefIndex` of a `DefId` can change in between two + // compilation sessions. We use the `DefPathHash`, which is stable across + // sessions, to map the old `DefId` to the new one. + fn decode_def_id(&mut self) -> DefId { // Load the `DefPathHash` which is was we encoded the `DefId` as. - let def_path_hash = DefPathHash::decode(d); + let def_path_hash = DefPathHash::decode(self); // Using the `DefPathHash`, we can lookup the new `DefId`. // Subtle: We only encode a `DefId` as part of a query result. // If we get to this point, then all of the query inputs were green, // which means that the definition with this hash is guaranteed to // still exist in the current compilation session. - d.tcx.def_path_hash_to_def_id(def_path_hash, &mut || { + self.tcx.def_path_hash_to_def_id(def_path_hash, &mut || { panic!("Failed to convert DefPathHash {def_path_hash:?}") }) } @@ -860,20 +846,16 @@ impl<'a, 'tcx> CacheEncoder<'a, 'tcx> { } } -impl<'a, 'tcx> Encodable> for SyntaxContext { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { - rustc_span::hygiene::raw_encode_syntax_context(*self, s.hygiene_context, s); +impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> { + fn encode_syntax_context(&mut self, syntax_context: SyntaxContext) { + rustc_span::hygiene::raw_encode_syntax_context(syntax_context, self.hygiene_context, self); } -} -impl<'a, 'tcx> Encodable> for ExpnId { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { - s.hygiene_context.schedule_expn_data_for_encoding(*self); - self.expn_hash().encode(s); + fn encode_expn_id(&mut self, expn_id: ExpnId) { + self.hygiene_context.schedule_expn_data_for_encoding(expn_id); + expn_id.expn_hash().encode(self); } -} -impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> { fn encode_span(&mut self, span: Span) { let span_data = span.data_untracked(); span_data.ctxt.encode(self); @@ -915,32 +897,42 @@ impl<'a, 'tcx> SpanEncoder for CacheEncoder<'a, 'tcx> { col_lo.encode(self); len.encode(self); } -} -// copy&paste impl from rustc_metadata -impl<'a, 'tcx> Encodable> for Symbol { - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { + // copy&paste impl from rustc_metadata + fn encode_symbol(&mut self, symbol: Symbol) { // if symbol preinterned, emit tag and symbol index - if self.is_preinterned() { - s.encoder.emit_u8(SYMBOL_PREINTERNED); - s.encoder.emit_u32(self.as_u32()); + if symbol.is_preinterned() { + self.encoder.emit_u8(SYMBOL_PREINTERNED); + self.encoder.emit_u32(symbol.as_u32()); } else { // otherwise write it as string or as offset to it - match s.symbol_table.entry(*self) { + match self.symbol_table.entry(symbol) { Entry::Vacant(o) => { - s.encoder.emit_u8(SYMBOL_STR); - let pos = s.encoder.position(); + self.encoder.emit_u8(SYMBOL_STR); + let pos = self.encoder.position(); o.insert(pos); - s.emit_str(self.as_str()); + self.emit_str(symbol.as_str()); } Entry::Occupied(o) => { let x = *o.get(); - s.emit_u8(SYMBOL_OFFSET); - s.emit_usize(x); + self.emit_u8(SYMBOL_OFFSET); + self.emit_usize(x); } } } } + + fn encode_crate_num(&mut self, crate_num: CrateNum) { + self.tcx.stable_crate_id(crate_num).encode(self); + } + + fn encode_def_id(&mut self, def_id: DefId) { + self.tcx.def_path_hash(def_id).encode(self); + } + + fn encode_def_index(&mut self, _def_index: DefIndex) { + bug!("encoding `DefIndex` without context"); + } } impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> { @@ -967,26 +959,6 @@ impl<'a, 'tcx> TyEncoder for CacheEncoder<'a, 'tcx> { } } -impl<'a, 'tcx> Encodable> for CrateNum { - #[inline] - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { - s.tcx.stable_crate_id(*self).encode(s); - } -} - -impl<'a, 'tcx> Encodable> for DefId { - #[inline] - fn encode(&self, s: &mut CacheEncoder<'a, 'tcx>) { - s.tcx.def_path_hash(*self).encode(s); - } -} - -impl<'a, 'tcx> Encodable> for DefIndex { - fn encode(&self, _: &mut CacheEncoder<'a, 'tcx>) { - bug!("encoding `DefIndex` without context"); - } -} - macro_rules! encoder_methods { ($($name:ident($ty:ty);)*) => { #[inline] diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index e397fab54593b..b498202dc9210 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -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 Encodable for CrateNum { - default fn encode(&self, s: &mut E) { - s.emit_u32(self.as_u32()); - } -} - -impl Decodable 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 Encodable for DefIndex { - default fn encode(&self, _: &mut E) { - panic!("cannot encode `DefIndex` with `{}`", std::any::type_name::()); - } -} - -impl Decodable for DefIndex { - default fn decode(_: &mut D) -> DefIndex { - panic!("cannot decode `DefIndex` with `{}`", std::any::type_name::()); - } -} - /// A `DefId` identifies a particular *definition*, by combining a crate /// index and a def index. /// @@ -347,19 +321,6 @@ impl From for DefId { } } -impl Encodable for DefId { - default fn encode(&self, s: &mut E) { - self.krate.encode(s); - self.index.encode(s); - } -} - -impl Decodable 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 Encodable for LocalDefId { +impl Encodable for LocalDefId { fn encode(&self, s: &mut E) { self.to_def_id().encode(s); } } -impl Decodable for LocalDefId { +impl Decodable for LocalDefId { fn decode(d: &mut D) -> LocalDefId { DefId::decode(d).expect_local() } diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index b717229b68dff..c4557017c87ec 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -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 Encodable for LocalExpnId { +impl Encodable for LocalExpnId { fn encode(&self, e: &mut E) { self.to_expn_id().encode(e); } } -impl Encodable for ExpnId { - default fn encode(&self, _: &mut E) { - panic!("cannot encode `ExpnId` with `{}`", std::any::type_name::()); - } -} - -impl Decodable for LocalExpnId { +impl Decodable for LocalExpnId { fn decode(d: &mut D) -> Self { ExpnId::expect_local(ExpnId::decode(d)) } } -impl Decodable for ExpnId { - default fn decode(_: &mut D) -> Self { - panic!("cannot decode `ExpnId` with `{}`", std::any::type_name::()); - } -} - pub fn raw_encode_syntax_context( ctxt: SyntaxContext, context: &HygieneEncodeContext, @@ -1466,18 +1454,6 @@ pub fn raw_encode_syntax_context( ctxt.0.encode(e); } -impl Encodable for SyntaxContext { - default fn encode(&self, _: &mut E) { - panic!("cannot encode `SyntaxContext` with `{}`", std::any::type_name::()); - } -} - -impl Decodable for SyntaxContext { - default fn decode(_: &mut D) -> Self { - panic!("cannot decode `SyntaxContext` with `{}`", std::any::type_name::()); - } -} - /// Updates the `disambiguator` field of the corresponding `ExpnData` /// such that the `Fingerprint` of the `ExpnData` does not collide with /// any other `ExpnIds`. diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 0eafbae6e82bf..18d1f14cf44dd 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -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 Encodable for Span { @@ -1037,8 +1070,50 @@ impl Encodable for Span { } } +impl Encodable for Symbol { + fn encode(&self, s: &mut E) { + s.encode_symbol(*self); + } +} + +impl Encodable for ExpnId { + fn encode(&self, s: &mut E) { + s.encode_expn_id(*self) + } +} + +impl Encodable for SyntaxContext { + fn encode(&self, s: &mut E) { + s.encode_syntax_context(*self) + } +} + +impl Encodable for CrateNum { + fn encode(&self, s: &mut E) { + s.encode_crate_num(*self) + } +} + +impl Encodable for DefIndex { + fn encode(&self, s: &mut E) { + s.encode_def_index(*self) + } +} + +impl Encodable 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 Decodable for Span { @@ -1056,6 +1155,42 @@ impl Decodable for Span { } } +impl Decodable for Symbol { + fn decode(s: &mut D) -> Symbol { + s.decode_symbol() + } +} + +impl Decodable for ExpnId { + fn decode(s: &mut D) -> ExpnId { + s.decode_expn_id() + } +} + +impl Decodable for SyntaxContext { + fn decode(s: &mut D) -> SyntaxContext { + s.decode_syntax_context() + } +} + +impl Decodable for CrateNum { + fn decode(s: &mut D) -> CrateNum { + s.decode_crate_num() + } +} + +impl Decodable for DefIndex { + fn decode(s: &mut D) -> DefIndex { + s.decode_def_index() + } +} + +impl Decodable 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>(source_map: Lrc, f: F) -> T { diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 0b44071496ea8..6e59806cf076e 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -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 Encodable for Symbol { - default fn encode(&self, s: &mut S) { - s.emit_str(self.as_str()); - } -} - -impl Decodable for Symbol { - #[inline] - default fn decode(d: &mut D) -> Symbol { - Symbol::intern(d.read_str()) - } -} - impl HashStable for Symbol { #[inline] fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { From 47936b4813398435189501f2390f27296187c32c Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 31 Dec 2023 20:40:09 +0000 Subject: [PATCH 3/6] Avoid specialization for AttrId deserialization --- compiler/rustc_ast/src/ast.rs | 18 +-------------- compiler/rustc_metadata/src/rmeta/decoder.rs | 9 +++----- .../rustc_middle/src/query/on_disk_cache.rs | 4 ++++ compiler/rustc_span/src/lib.rs | 22 +++++++++++++++++++ 4 files changed, 30 insertions(+), 23 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 1812cc335a4f6..e1e4e5fc56750 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -20,6 +20,7 @@ pub use crate::format::*; pub use crate::util::parser::ExprPrecedence; +pub use rustc_span::AttrId; pub use GenericArgs::*; pub use UnsafeSource::*; @@ -30,7 +31,6 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_data_structures::sync::Lrc; use rustc_macros::HashStable_Generic; -use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_span::source_map::{respan, Spanned}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP}; @@ -2682,22 +2682,6 @@ pub enum AttrStyle { Inner, } -rustc_index::newtype_index! { - #[orderable] - #[debug_format = "AttrId({})"] - pub struct AttrId {} -} - -impl Encodable for AttrId { - fn encode(&self, _s: &mut S) {} -} - -impl Decodable for AttrId { - default fn decode(_: &mut D) -> AttrId { - panic!("cannot decode `AttrId` with `{}`", std::any::type_name::()); - } -} - /// A list of attributes. pub type AttrVec = ThinVec; diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index d20f4bc7a9417..ebba8ee683d49 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -416,15 +416,12 @@ impl<'a, 'tcx> Decodable> for ExpnIndex { } } -impl<'a, 'tcx> Decodable> for ast::AttrId { - #[inline] - fn decode(d: &mut DecodeContext<'a, 'tcx>) -> ast::AttrId { - let sess = d.sess.expect("can't decode AttrId without Session"); +impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> { + fn decode_attr_id(&mut self) -> rustc_span::AttrId { + let sess = self.sess.expect("can't decode AttrId without Session"); sess.parse_sess.attr_id_generator.mk_attr_id() } -} -impl<'a, 'tcx> SpanDecoder for DecodeContext<'a, 'tcx> { fn decode_crate_num(&mut self) -> CrateNum { let cnum = CrateNum::from_u32(self.read_u32()); self.map_encoded_cnum_to_current(cnum) diff --git a/compiler/rustc_middle/src/query/on_disk_cache.rs b/compiler/rustc_middle/src/query/on_disk_cache.rs index 8d084f5f13118..9ca830af9cdfc 100644 --- a/compiler/rustc_middle/src/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/query/on_disk_cache.rs @@ -741,6 +741,10 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> { panic!("Failed to convert DefPathHash {def_path_hash:?}") }) } + + fn decode_attr_id(&mut self) -> rustc_span::AttrId { + panic!("cannot decode `AttrId` with `CacheDecoder`"); + } } impl<'a, 'tcx> Decodable> for &'tcx UnordSet { diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 18d1f14cf44dd..9bd3cabcc854d 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1019,6 +1019,12 @@ impl Default for Span { } } +rustc_index::newtype_index! { + #[orderable] + #[debug_format = "AttrId({})"] + pub struct AttrId {} +} + pub trait SpanEncoder: Encoder { fn encode_span(&mut self, span: Span); fn encode_symbol(&mut self, symbol: Symbol); @@ -1106,6 +1112,11 @@ impl Encodable for DefId { } } +impl Encodable for AttrId { + fn encode(&self, _s: &mut E) { + // A fresh id will be generated when decoding + } +} pub trait SpanDecoder: Decoder { fn decode_span(&mut self) -> Span; fn decode_symbol(&mut self) -> Symbol; @@ -1114,6 +1125,7 @@ pub trait SpanDecoder: Decoder { fn decode_crate_num(&mut self) -> CrateNum; fn decode_def_index(&mut self) -> DefIndex; fn decode_def_id(&mut self) -> DefId; + fn decode_attr_id(&mut self) -> AttrId; } impl SpanDecoder for MemDecoder<'_> { @@ -1147,6 +1159,10 @@ impl SpanDecoder for MemDecoder<'_> { fn decode_def_id(&mut self) -> DefId { DefId { krate: Decodable::decode(self), index: Decodable::decode(self) } } + + fn decode_attr_id(&mut self) -> AttrId { + panic!("cannot decode `AttrId` with `MemDecoder`"); + } } impl Decodable for Span { @@ -1191,6 +1207,12 @@ impl Decodable for DefId { } } +impl Decodable for AttrId { + fn decode(s: &mut D) -> AttrId { + s.decode_attr_id() + } +} + /// Insert `source_map` into the session globals for the duration of the /// closure's execution. pub fn set_source_map T>(source_map: Lrc, f: F) -> T { From 3e69335c447a3ca93e4957ee2f4e66d13149e38c Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 31 Dec 2023 20:58:36 +0000 Subject: [PATCH 4/6] Fix tidy error --- compiler/rustc_type_ir/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_type_ir/Cargo.toml b/compiler/rustc_type_ir/Cargo.toml index 3a94256f402f9..59966423f7fb7 100644 --- a/compiler/rustc_type_ir/Cargo.toml +++ b/compiler/rustc_type_ir/Cargo.toml @@ -10,8 +10,8 @@ derivative = "2.2.0" rustc_data_structures = { path = "../rustc_data_structures", optional = true } rustc_index = { path = "../rustc_index", default-features = false } rustc_macros = { path = "../rustc_macros", optional = true } -rustc_span = { path = "../rustc_span", optional = true } rustc_serialize = { path = "../rustc_serialize", optional = true } +rustc_span = { path = "../rustc_span", optional = true } smallvec = { version = "1.8.1" } # tidy-alphabetical-end @@ -21,8 +21,8 @@ nightly = [ "smallvec/may_dangle", "smallvec/union", "rustc_index/nightly", - "rustc_span", "rustc_serialize", + "rustc_span", "rustc_data_structures", "rustc_macros", ] From 3d9eebc21f4ec00e75fc5dd23f66835634b2e77b Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Mon, 1 Jan 2024 15:13:39 +0000 Subject: [PATCH 5/6] Fix tests --- tests/ui-fulldeps/deriving-global.rs | 1 + tests/ui-fulldeps/deriving-hygiene.rs | 1 + tests/ui-fulldeps/empty-struct-braces-derive.rs | 1 + tests/ui-fulldeps/rustc_encodable_hygiene.rs | 2 +- 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/ui-fulldeps/deriving-global.rs b/tests/ui-fulldeps/deriving-global.rs index 214bb4368ffdd..9c0fc13a5e2fa 100644 --- a/tests/ui-fulldeps/deriving-global.rs +++ b/tests/ui-fulldeps/deriving-global.rs @@ -4,6 +4,7 @@ extern crate rustc_macros; extern crate rustc_serialize; +extern crate rustc_span; // Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta // files. diff --git a/tests/ui-fulldeps/deriving-hygiene.rs b/tests/ui-fulldeps/deriving-hygiene.rs index e1084a08fec93..48d3355b9d541 100644 --- a/tests/ui-fulldeps/deriving-hygiene.rs +++ b/tests/ui-fulldeps/deriving-hygiene.rs @@ -4,6 +4,7 @@ #![feature(rustc_private)] extern crate rustc_macros; extern crate rustc_serialize; +extern crate rustc_span; use rustc_macros::{Decodable, Encodable}; diff --git a/tests/ui-fulldeps/empty-struct-braces-derive.rs b/tests/ui-fulldeps/empty-struct-braces-derive.rs index 10e8beaa7b119..3637610af0d9a 100644 --- a/tests/ui-fulldeps/empty-struct-braces-derive.rs +++ b/tests/ui-fulldeps/empty-struct-braces-derive.rs @@ -5,6 +5,7 @@ extern crate rustc_macros; extern crate rustc_serialize; +extern crate rustc_span; // Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta // files. diff --git a/tests/ui-fulldeps/rustc_encodable_hygiene.rs b/tests/ui-fulldeps/rustc_encodable_hygiene.rs index 509a6b1d22ca6..bec7930d4622f 100644 --- a/tests/ui-fulldeps/rustc_encodable_hygiene.rs +++ b/tests/ui-fulldeps/rustc_encodable_hygiene.rs @@ -3,8 +3,8 @@ #![feature(rustc_private)] extern crate rustc_macros; -#[allow(dead_code)] extern crate rustc_serialize; +extern crate rustc_span; // Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta // files. From 8fb8e6eefb27313523be937ff9de557dd3bcf4fe Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Mon, 1 Jan 2024 19:10:42 +0000 Subject: [PATCH 6/6] Add comments sugested by reviewer --- compiler/rustc_span/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 9bd3cabcc854d..7f7f05b797897 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -1025,6 +1025,8 @@ rustc_index::newtype_index! { pub struct AttrId {} } +/// This trait is used to allow encoder specific encodings of certain types. +/// It is similar to rustc_type_ir's TyEncoder. pub trait SpanEncoder: Encoder { fn encode_span(&mut self, span: Span); fn encode_symbol(&mut self, symbol: Symbol); @@ -1117,6 +1119,9 @@ impl Encodable for AttrId { // A fresh id will be generated when decoding } } + +/// This trait is used to allow decoder specific encodings of certain types. +/// It is similar to rustc_type_ir's TyDecoder. pub trait SpanDecoder: Decoder { fn decode_span(&mut self) -> Span; fn decode_symbol(&mut self) -> Symbol;