Skip to content

Commit e21f4cd

Browse files
committed
Auto merge of #119478 - bjorn3:no_serialize_specialization, r=wesleywiser
Avoid specialization in the metadata serialization code With the exception of a perf-only specialization for byte slices and byte vectors. This uses the same trick of introducing a new trait and having the Encodable and Decodable derives add a bound to it as used for TyEncoder/TyDecoder. The new code is clearer about which encoder/decoder uses which impl and it reduces the dependency of rustc on specialization, making it easier to remove support for specialization entirely or turn it into a construct that is only allowed for perf optimizations if we decide to do this.
2 parents 5cb2e7d + 8fb8e6e commit e21f4cd

File tree

24 files changed

+475
-397
lines changed

24 files changed

+475
-397
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4644,6 +4644,7 @@ dependencies = [
46444644
"rustc_index",
46454645
"rustc_macros",
46464646
"rustc_serialize",
4647+
"rustc_span",
46474648
"smallvec",
46484649
]
46494650

compiler/rustc_abi/src/lib.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_data_structures::stable_hasher::StableOrd;
1616
#[cfg(feature = "nightly")]
1717
use rustc_macros::HashStable_Generic;
1818
#[cfg(feature = "nightly")]
19-
use rustc_macros::{Decodable, Encodable};
19+
use rustc_macros::{Decodable_Generic, Encodable_Generic};
2020
#[cfg(feature = "nightly")]
2121
use std::iter::Step;
2222

@@ -30,7 +30,7 @@ pub use layout::LayoutCalculator;
3030
pub trait HashStableContext {}
3131

3232
#[derive(Clone, Copy, PartialEq, Eq, Default)]
33-
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
33+
#[cfg_attr(feature = "nightly", derive(Encodable_Generic, Decodable_Generic, HashStable_Generic))]
3434
pub struct ReprFlags(u8);
3535

3636
bitflags! {
@@ -52,7 +52,7 @@ bitflags! {
5252
rustc_data_structures::external_bitflags_debug! { ReprFlags }
5353

5454
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
55-
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
55+
#[cfg_attr(feature = "nightly", derive(Encodable_Generic, Decodable_Generic, HashStable_Generic))]
5656
pub enum IntegerType {
5757
/// Pointer-sized integer type, i.e. `isize` and `usize`. The field shows signedness, e.g.
5858
/// `Pointer(true)` means `isize`.
@@ -73,7 +73,7 @@ impl IntegerType {
7373

7474
/// Represents the repr options provided by the user.
7575
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
76-
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
76+
#[cfg_attr(feature = "nightly", derive(Encodable_Generic, Decodable_Generic, HashStable_Generic))]
7777
pub struct ReprOptions {
7878
pub int: Option<IntegerType>,
7979
pub align: Option<Align>,
@@ -412,7 +412,7 @@ impl FromStr for Endian {
412412

413413
/// Size of a type in bytes.
414414
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
415-
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
415+
#[cfg_attr(feature = "nightly", derive(Encodable_Generic, Decodable_Generic, HashStable_Generic))]
416416
pub struct Size {
417417
raw: u64,
418418
}
@@ -636,7 +636,7 @@ impl Step for Size {
636636

637637
/// Alignment of a type in bytes (always a power of two).
638638
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
639-
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
639+
#[cfg_attr(feature = "nightly", derive(Encodable_Generic, Decodable_Generic, HashStable_Generic))]
640640
pub struct Align {
641641
pow2: u8,
642642
}
@@ -777,7 +777,7 @@ impl AbiAndPrefAlign {
777777

778778
/// Integers, also used for enum discriminants.
779779
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
780-
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_Generic))]
780+
#[cfg_attr(feature = "nightly", derive(Encodable_Generic, Decodable_Generic, HashStable_Generic))]
781781
pub enum Integer {
782782
I8,
783783
I16,

compiler/rustc_ast/src/ast.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
2121
pub use crate::format::*;
2222
pub use crate::util::parser::ExprPrecedence;
23+
pub use rustc_span::AttrId;
2324
pub use GenericArgs::*;
2425
pub use UnsafeSource::*;
2526

@@ -30,7 +31,6 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3031
use rustc_data_structures::stack::ensure_sufficient_stack;
3132
use rustc_data_structures::sync::Lrc;
3233
use rustc_macros::HashStable_Generic;
33-
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
3434
use rustc_span::source_map::{respan, Spanned};
3535
use rustc_span::symbol::{kw, sym, Ident, Symbol};
3636
use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP};
@@ -2682,22 +2682,6 @@ pub enum AttrStyle {
26822682
Inner,
26832683
}
26842684

2685-
rustc_index::newtype_index! {
2686-
#[orderable]
2687-
#[debug_format = "AttrId({})"]
2688-
pub struct AttrId {}
2689-
}
2690-
2691-
impl<S: Encoder> Encodable<S> for AttrId {
2692-
fn encode(&self, _s: &mut S) {}
2693-
}
2694-
2695-
impl<D: Decoder> Decodable<D> for AttrId {
2696-
default fn decode(_: &mut D) -> AttrId {
2697-
panic!("cannot decode `AttrId` with `{}`", std::any::type_name::<D>());
2698-
}
2699-
}
2700-
27012685
/// A list of attributes.
27022686
pub type AttrVec = ThinVec<Attribute>;
27032687

compiler/rustc_ast/src/tokenstream.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use crate::AttrVec;
2121
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
2222
use rustc_data_structures::sync::{self, Lrc};
2323
use rustc_macros::HashStable_Generic;
24-
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
25-
use rustc_span::{sym, Span, Symbol, DUMMY_SP};
24+
use rustc_serialize::{Decodable, Encodable};
25+
use rustc_span::{sym, Span, SpanDecoder, SpanEncoder, Symbol, DUMMY_SP};
2626
use smallvec::{smallvec, SmallVec};
2727

2828
use std::borrow::Cow;
@@ -150,14 +150,14 @@ impl fmt::Debug for LazyAttrTokenStream {
150150
}
151151
}
152152

153-
impl<S: Encoder> Encodable<S> for LazyAttrTokenStream {
153+
impl<S: SpanEncoder> Encodable<S> for LazyAttrTokenStream {
154154
fn encode(&self, s: &mut S) {
155155
// Used by AST json printing.
156156
Encodable::encode(&self.to_attr_token_stream(), s);
157157
}
158158
}
159159

160-
impl<D: Decoder> Decodable<D> for LazyAttrTokenStream {
160+
impl<D: SpanDecoder> Decodable<D> for LazyAttrTokenStream {
161161
fn decode(_d: &mut D) -> Self {
162162
panic!("Attempted to decode LazyAttrTokenStream");
163163
}

compiler/rustc_data_structures/src/sorted_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub use index_map::SortedIndexMultiMap;
1616
/// stores data in a more compact way. It also supports accessing contiguous
1717
/// ranges of elements as a slice, and slices of already sorted elements can be
1818
/// inserted efficiently.
19-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
19+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable_Generic, Decodable_Generic)]
2020
pub struct SortedMap<K, V> {
2121
data: Vec<(K, V)>,
2222
}

compiler/rustc_data_structures/src/svh.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::fmt;
1010

1111
use crate::stable_hasher;
1212

13-
#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable, Decodable, Hash)]
13+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable_Generic, Decodable_Generic, Hash)]
1414
pub struct Svh {
1515
hash: Fingerprint,
1616
}

compiler/rustc_data_structures/src/unord.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ trait UnordCollection {}
226226
///
227227
/// See [MCP 533](https://github.com/rust-lang/compiler-team/issues/533)
228228
/// for more information.
229-
#[derive(Debug, Eq, PartialEq, Clone, Encodable, Decodable)]
229+
#[derive(Debug, Eq, PartialEq, Clone, Encodable_Generic, Decodable_Generic)]
230230
pub struct UnordSet<V: Eq + Hash> {
231231
inner: FxHashSet<V>,
232232
}
@@ -417,7 +417,7 @@ impl<HCX, V: Hash + Eq + HashStable<HCX>> HashStable<HCX> for UnordSet<V> {
417417
///
418418
/// See [MCP 533](https://github.com/rust-lang/compiler-team/issues/533)
419419
/// for more information.
420-
#[derive(Debug, Eq, PartialEq, Clone, Encodable, Decodable)]
420+
#[derive(Debug, Eq, PartialEq, Clone, Encodable_Generic, Decodable_Generic)]
421421
pub struct UnordMap<K: Eq + Hash, V> {
422422
inner: FxHashMap<K, V>,
423423
}
@@ -626,7 +626,7 @@ impl<HCX, K: Hash + Eq + HashStable<HCX>, V: HashStable<HCX>> HashStable<HCX> fo
626626
///
627627
/// See [MCP 533](https://github.com/rust-lang/compiler-team/issues/533)
628628
/// for more information.
629-
#[derive(Default, Debug, Eq, PartialEq, Clone, Encodable, Decodable)]
629+
#[derive(Default, Debug, Eq, PartialEq, Clone, Encodable_Generic, Decodable_Generic)]
630630
pub struct UnordBag<V> {
631631
inner: Vec<V>,
632632
}

compiler/rustc_index/src/bit_set.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use arrayvec::ArrayVec;
1010
use smallvec::{smallvec, SmallVec};
1111

1212
#[cfg(feature = "nightly")]
13-
use rustc_macros::{Decodable, Encodable};
13+
use rustc_macros::{Decodable_Generic, Encodable_Generic};
1414

1515
use crate::{Idx, IndexVec};
1616

@@ -112,7 +112,7 @@ macro_rules! bit_relations_inherent_impls {
112112
/// to or greater than the domain size. All operations that involve two bitsets
113113
/// will panic if the bitsets have differing domain sizes.
114114
///
115-
#[cfg_attr(feature = "nightly", derive(Decodable, Encodable))]
115+
#[cfg_attr(feature = "nightly", derive(Decodable_Generic, Encodable_Generic))]
116116
#[derive(Eq, PartialEq, Hash)]
117117
pub struct BitSet<T> {
118118
domain_size: usize,
@@ -1590,7 +1590,7 @@ impl<T: Idx> From<BitSet<T>> for GrowableBitSet<T> {
15901590
///
15911591
/// All operations that involve a row and/or column index will panic if the
15921592
/// index exceeds the relevant bound.
1593-
#[cfg_attr(feature = "nightly", derive(Decodable, Encodable))]
1593+
#[cfg_attr(feature = "nightly", derive(Decodable_Generic, Encodable_Generic))]
15941594
#[derive(Clone, Eq, PartialEq, Hash)]
15951595
pub struct BitMatrix<R: Idx, C: Idx> {
15961596
num_rows: usize,
@@ -2020,7 +2020,7 @@ impl std::fmt::Debug for FiniteBitSet<u32> {
20202020

20212021
/// A fixed-sized bitset type represented by an integer type. Indices outwith than the range
20222022
/// representable by `T` are considered set.
2023-
#[cfg_attr(feature = "nightly", derive(Decodable, Encodable))]
2023+
#[cfg_attr(feature = "nightly", derive(Decodable_Generic, Encodable_Generic))]
20242024
#[derive(Copy, Clone, Eq, PartialEq)]
20252025
pub struct FiniteBitSet<T: FiniteBitSetTy>(pub T);
20262026

compiler/rustc_macros/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ decl_derive!(
5656
hash_stable::hash_stable_no_context_derive
5757
);
5858

59+
decl_derive!([Decodable_Generic] => serialize::decodable_generic_derive);
60+
decl_derive!([Encodable_Generic] => serialize::encodable_generic_derive);
5961
decl_derive!([Decodable] => serialize::decodable_derive);
6062
decl_derive!([Encodable] => serialize::encodable_derive);
6163
decl_derive!([TyDecodable] => serialize::type_decodable_derive);

compiler/rustc_macros/src/serialize.rs

+16
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ pub fn meta_decodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2:
3131
}
3232

3333
pub fn decodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
34+
let decoder_ty = quote! { __D };
35+
s.add_impl_generic(parse_quote! {#decoder_ty: ::rustc_span::SpanDecoder});
36+
s.add_bounds(synstructure::AddBounds::Generics);
37+
38+
decodable_body(s, decoder_ty)
39+
}
40+
41+
pub fn decodable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
3442
let decoder_ty = quote! { __D };
3543
s.add_impl_generic(parse_quote! {#decoder_ty: ::rustc_serialize::Decoder});
3644
s.add_bounds(synstructure::AddBounds::Generics);
@@ -129,6 +137,14 @@ pub fn meta_encodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2:
129137
}
130138

131139
pub fn encodable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
140+
let encoder_ty = quote! { __E };
141+
s.add_impl_generic(parse_quote! { #encoder_ty: ::rustc_span::SpanEncoder});
142+
s.add_bounds(synstructure::AddBounds::Generics);
143+
144+
encodable_body(s, encoder_ty, false)
145+
}
146+
147+
pub fn encodable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
132148
let encoder_ty = quote! { __E };
133149
s.add_impl_generic(parse_quote! { #encoder_ty: ::rustc_serialize::Encoder});
134150
s.add_bounds(synstructure::AddBounds::Generics);

0 commit comments

Comments
 (0)