Skip to content

Commit f2c6d5c

Browse files
committed
Replace custom_encodable with encodable.
By default, `newtype_index!` types get a default `Encodable`/`Decodable` impl. You can opt out of this with `custom_encodable`. Opting out is the opposite to how Rust normally works with autogenerated (derived) impls. This commit inverts the behaviour, replacing `custom_encodable` with `encodable` which opts into the default `Encodable`/`Decodable` impl. Only 23 of the 59 `newtype_index!` occurrences need `encodable`. Even better, there were eight crates with a dependency on `rustc_serialize` just from unused default `Encodable`/`Decodable` impls. This commit removes that dependency from those eight crates.
1 parent 290fc68 commit f2c6d5c

File tree

28 files changed

+27
-28
lines changed

28 files changed

+27
-28
lines changed

Cargo.lock

-8
Original file line numberDiff line numberDiff line change
@@ -3523,7 +3523,6 @@ dependencies = [
35233523
"rustc_macros",
35243524
"rustc_middle",
35253525
"rustc_mir_dataflow",
3526-
"rustc_serialize",
35273526
"rustc_session",
35283527
"rustc_span",
35293528
"rustc_target",
@@ -3934,7 +3933,6 @@ dependencies = [
39343933
"rustc_lint",
39353934
"rustc_macros",
39363935
"rustc_middle",
3937-
"rustc_serialize",
39383936
"rustc_session",
39393937
"rustc_span",
39403938
"rustc_target",
@@ -3986,7 +3984,6 @@ dependencies = [
39863984
"rustc_index",
39873985
"rustc_macros",
39883986
"rustc_middle",
3989-
"rustc_serialize",
39903987
"rustc_span",
39913988
"rustc_target",
39923989
"smallvec",
@@ -4204,7 +4201,6 @@ dependencies = [
42044201
"rustc_infer",
42054202
"rustc_macros",
42064203
"rustc_middle",
4207-
"rustc_serialize",
42084204
"rustc_session",
42094205
"rustc_span",
42104206
"rustc_target",
@@ -4228,7 +4224,6 @@ dependencies = [
42284224
"rustc_index",
42294225
"rustc_macros",
42304226
"rustc_middle",
4231-
"rustc_serialize",
42324227
"rustc_span",
42334228
"rustc_target",
42344229
"smallvec",
@@ -4255,7 +4250,6 @@ dependencies = [
42554250
"rustc_middle",
42564251
"rustc_mir_build",
42574252
"rustc_mir_dataflow",
4258-
"rustc_serialize",
42594253
"rustc_session",
42604254
"rustc_span",
42614255
"rustc_target",
@@ -4329,7 +4323,6 @@ dependencies = [
43294323
"rustc_lexer",
43304324
"rustc_macros",
43314325
"rustc_middle",
4332-
"rustc_serialize",
43334326
"rustc_session",
43344327
"rustc_span",
43354328
"rustc_target",
@@ -4553,7 +4546,6 @@ dependencies = [
45534546
"rustc_middle",
45544547
"rustc_parse_format",
45554548
"rustc_query_system",
4556-
"rustc_serialize",
45574549
"rustc_session",
45584550
"rustc_span",
45594551
"rustc_target",

compiler/rustc_ast/src/ast.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2574,7 +2574,6 @@ pub enum AttrStyle {
25742574
}
25752575

25762576
rustc_index::newtype_index! {
2577-
#[custom_encodable]
25782577
#[debug_format = "AttrId({})"]
25792578
pub struct AttrId {}
25802579
}

compiler/rustc_ast/src/node_id.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ rustc_index::newtype_index! {
88
/// This is later turned into [`DefId`] and `HirId` for the HIR.
99
///
1010
/// [`DefId`]: rustc_span::def_id::DefId
11+
#[encodable]
1112
#[debug_format = "NodeId({})"]
1213
pub struct NodeId {
1314
/// The [`NodeId`] used to represent the root of the crate.

compiler/rustc_borrowck/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ rustc_lexer = { path = "../rustc_lexer" }
1919
rustc_macros = { path = "../rustc_macros" }
2020
rustc_middle = { path = "../rustc_middle" }
2121
rustc_mir_dataflow = { path = "../rustc_mir_dataflow" }
22-
rustc_serialize = { path = "../rustc_serialize" }
2322
rustc_session = { path = "../rustc_session" }
2423
rustc_span = { path = "../rustc_span" }
2524
rustc_target = { path = "../rustc_target" }

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,6 @@ impl GlobalFileTable {
189189
}
190190

191191
rustc_index::newtype_index! {
192-
// Tell the newtype macro to not generate `Encode`/`Decode` impls.
193-
#[custom_encodable]
194192
struct LocalFileId {}
195193
}
196194

compiler/rustc_hir/src/hir_id.rs

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ rustc_index::newtype_index! {
156156
/// an "item-like" to something else can be implemented by a `Vec` instead of a
157157
/// tree or hash map.
158158
#[derive(HashStable_Generic)]
159+
#[encodable]
159160
pub struct ItemLocalId {}
160161
}
161162

compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs

-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
198198
// entire graph when there are many connected regions.
199199

200200
rustc_index::newtype_index! {
201-
#[custom_encodable]
202201
pub struct RegionId {}
203202
}
204203

compiler/rustc_hir_typeck/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ rustc_infer = { path = "../rustc_infer" }
1919
rustc_lint = { path = "../rustc_lint" }
2020
rustc_macros = { path = "../rustc_macros" }
2121
rustc_middle = { path = "../rustc_middle" }
22-
rustc_serialize = { path = "../rustc_serialize" }
2322
rustc_session = { path = "../rustc_session" }
2423
rustc_span = { path = "../rustc_span" }
2524
rustc_target = { path = "../rustc_target" }

compiler/rustc_infer/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ rustc_hir = { path = "../rustc_hir" }
1515
rustc_index = { path = "../rustc_index" }
1616
rustc_macros = { path = "../rustc_macros" }
1717
rustc_middle = { path = "../rustc_middle" }
18-
rustc_serialize = { path = "../rustc_serialize" }
1918
rustc_span = { path = "../rustc_span" }
2019
rustc_target = { path = "../rustc_target" }
2120
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }

compiler/rustc_lint/src/levels.rs

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ struct LintLevelSets {
5656
}
5757

5858
rustc_index::newtype_index! {
59-
#[custom_encodable] // we don't need encoding
6059
struct LintStackIndex {
6160
const COMMAND_LINE = 0;
6261
}

compiler/rustc_macros/src/newtype.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ impl Parse for Newtype {
2222
let mut debug_format: Option<Lit> = None;
2323
let mut max = None;
2424
let mut consts = Vec::new();
25-
let mut encodable = true;
25+
let mut encodable = false;
2626
let mut ord = true;
2727

2828
attrs.retain(|attr| match attr.path().get_ident() {
2929
Some(ident) => match &*ident.to_string() {
30-
"custom_encodable" => {
31-
encodable = false;
30+
"encodable" => {
31+
encodable = true;
3232
false
3333
}
3434
"no_ord_impl" => {

compiler/rustc_middle/src/middle/region.rs

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ rustc_index::newtype_index! {
148148
/// * The subscope with `first_statement_index == 1` is scope of `c`,
149149
/// and thus does not include EXPR_2, but covers the `...`.
150150
#[derive(HashStable)]
151+
#[encodable]
151152
pub struct FirstStatementIndex {}
152153
}
153154

compiler/rustc_middle/src/mir/coverage.rs

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ rustc_index::newtype_index! {
1717
/// Note that LLVM handles counter IDs as `uint32_t`, so there is no need
1818
/// to use a larger representation on the Rust side.
1919
#[derive(HashStable)]
20+
#[encodable]
2021
#[max = 0xFFFF_FFFF]
2122
#[debug_format = "CounterId({})"]
2223
pub struct CounterId {}
@@ -37,6 +38,7 @@ rustc_index::newtype_index! {
3738
/// Note that LLVM handles expression IDs as `uint32_t`, so there is no need
3839
/// to use a larger representation on the Rust side.
3940
#[derive(HashStable)]
41+
#[encodable]
4042
#[max = 0xFFFF_FFFF]
4143
#[debug_format = "ExpressionId({})"]
4244
pub struct ExpressionId {}

compiler/rustc_middle/src/mir/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ impl SourceInfo {
736736

737737
rustc_index::newtype_index! {
738738
#[derive(HashStable)]
739+
#[encodable]
739740
#[debug_format = "_{}"]
740741
pub struct Local {
741742
const RETURN_PLACE = 0;
@@ -1171,6 +1172,7 @@ rustc_index::newtype_index! {
11711172
/// [`CriticalCallEdges`]: ../../rustc_const_eval/transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges
11721173
/// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/
11731174
#[derive(HashStable)]
1175+
#[encodable]
11741176
#[debug_format = "bb{}"]
11751177
pub struct BasicBlock {
11761178
const START_BLOCK = 0;
@@ -1305,6 +1307,7 @@ impl<'tcx> BasicBlockData<'tcx> {
13051307

13061308
rustc_index::newtype_index! {
13071309
#[derive(HashStable)]
1310+
#[encodable]
13081311
#[debug_format = "scope[{}]"]
13091312
pub struct SourceScope {
13101313
const OUTERMOST_SOURCE_SCOPE = 0;
@@ -1533,6 +1536,7 @@ impl UserTypeProjection {
15331536

15341537
rustc_index::newtype_index! {
15351538
#[derive(HashStable)]
1539+
#[encodable]
15361540
#[debug_format = "promoted[{}]"]
15371541
pub struct Promoted {}
15381542
}

compiler/rustc_middle/src/mir/query.rs

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ pub struct UnsafetyCheckResult {
132132

133133
rustc_index::newtype_index! {
134134
#[derive(HashStable)]
135+
#[encodable]
135136
#[debug_format = "_{}"]
136137
pub struct CoroutineSavedLocal {}
137138
}

compiler/rustc_middle/src/ty/sty.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,7 @@ impl fmt::Debug for EarlyParamRegion {
16101610

16111611
rustc_index::newtype_index! {
16121612
/// A **`const`** **v**ariable **ID**.
1613+
#[encodable]
16131614
#[debug_format = "?{}c"]
16141615
pub struct ConstVid {}
16151616
}
@@ -1622,13 +1623,15 @@ rustc_index::newtype_index! {
16221623
/// relate an effect variable with a normal one, we would ICE, which can catch bugs
16231624
/// where we are not correctly using the effect var for an effect param. Fallback
16241625
/// is also implemented on top of having separate effect and normal const variables.
1626+
#[encodable]
16251627
#[debug_format = "?{}e"]
16261628
pub struct EffectVid {}
16271629
}
16281630

16291631
rustc_index::newtype_index! {
16301632
/// A **region** (lifetime) **v**ariable **ID**.
16311633
#[derive(HashStable)]
1634+
#[encodable]
16321635
#[debug_format = "'?{}"]
16331636
pub struct RegionVid {}
16341637
}
@@ -1641,6 +1644,7 @@ impl Atom for RegionVid {
16411644

16421645
rustc_index::newtype_index! {
16431646
#[derive(HashStable)]
1647+
#[encodable]
16441648
#[debug_format = "{}"]
16451649
pub struct BoundVar {}
16461650
}

compiler/rustc_middle/src/ty/typeck_results.rs

+1
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ impl<'a, V> LocalTableInContextMut<'a, V> {
578578

579579
rustc_index::newtype_index! {
580580
#[derive(HashStable)]
581+
#[encodable]
581582
#[debug_format = "UserType({})"]
582583
pub struct UserTypeAnnotationIndex {
583584
const START_INDEX = 0;

compiler/rustc_mir_build/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ rustc_index = { path = "../rustc_index" }
1717
rustc_infer = { path = "../rustc_infer" }
1818
rustc_macros = { path = "../rustc_macros" }
1919
rustc_middle = { path = "../rustc_middle" }
20-
rustc_serialize = { path = "../rustc_serialize" }
2120
rustc_session = { path = "../rustc_session" }
2221
rustc_span = { path = "../rustc_span" }
2322
rustc_target = { path = "../rustc_target" }

compiler/rustc_mir_dataflow/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ rustc_hir = { path = "../rustc_hir" }
1616
rustc_index = { path = "../rustc_index" }
1717
rustc_macros = { path = "../rustc_macros" }
1818
rustc_middle = { path = "../rustc_middle" }
19-
rustc_serialize = { path = "../rustc_serialize" }
2019
rustc_span = { path = "../rustc_span" }
2120
rustc_target = { path = "../rustc_target" }
2221
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }

compiler/rustc_mir_transform/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ rustc_macros = { path = "../rustc_macros" }
2020
rustc_middle = { path = "../rustc_middle" }
2121
rustc_mir_build = { path = "../rustc_mir_build" }
2222
rustc_mir_dataflow = { path = "../rustc_mir_dataflow" }
23-
rustc_serialize = { path = "../rustc_serialize" }
2423
rustc_session = { path = "../rustc_session" }
2524
rustc_span = { path = "../rustc_span" }
2625
rustc_target = { path = "../rustc_target" }

compiler/rustc_passes/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ rustc_index = { path = "../rustc_index" }
1919
rustc_lexer = { path = "../rustc_lexer" }
2020
rustc_macros = { path = "../rustc_macros" }
2121
rustc_middle = { path = "../rustc_middle" }
22-
rustc_serialize = { path = "../rustc_serialize" }
2322
rustc_session = { path = "../rustc_session" }
2423
rustc_span = { path = "../rustc_span" }
2524
rustc_target = { path = "../rustc_target" }

compiler/rustc_query_system/src/dep_graph/serialized.rs

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use std::marker::PhantomData;
5454
// unused so that we can store multiple index types in `CompressedHybridIndex`,
5555
// and use those bits to encode which index type it contains.
5656
rustc_index::newtype_index! {
57+
#[encodable]
5758
#[max = 0x7FFF_FFFF]
5859
pub struct SerializedDepNodeIndex {}
5960
}

compiler/rustc_span/src/def_id.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub type StableCrateIdMap =
1313
indexmap::IndexMap<StableCrateId, CrateNum, BuildHasherDefault<Unhasher>>;
1414

1515
rustc_index::newtype_index! {
16-
#[custom_encodable]
1716
#[debug_format = "crate{}"]
1817
pub struct CrateNum {}
1918
}
@@ -213,7 +212,6 @@ rustc_index::newtype_index! {
213212
/// A DefIndex is an index into the hir-map for a crate, identifying a
214213
/// particular definition. It should really be considered an interned
215214
/// shorthand for a particular DefPath.
216-
#[custom_encodable] // (only encodable in metadata)
217215
#[debug_format = "DefIndex({})"]
218216
pub struct DefIndex {
219217
/// The crate root is always assigned index 0 by the AST Map code,
@@ -222,6 +220,7 @@ rustc_index::newtype_index! {
222220
}
223221
}
224222

223+
// njn: I don't understand these
225224
impl<E: Encoder> Encodable<E> for DefIndex {
226225
default fn encode(&self, _: &mut E) {
227226
panic!("cannot encode `DefIndex` with `{}`", std::any::type_name::<E>());

compiler/rustc_span/src/hygiene.rs

-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ pub struct SyntaxContextData {
6060

6161
rustc_index::newtype_index! {
6262
/// A unique ID associated with a macro invocation and expansion.
63-
#[custom_encodable]
6463
pub struct ExpnIndex {}
6564
}
6665

@@ -80,7 +79,6 @@ impl fmt::Debug for ExpnId {
8079

8180
rustc_index::newtype_index! {
8281
/// A unique ID associated with a macro invocation and expansion.
83-
#[custom_encodable]
8482
#[no_ord_impl]
8583
#[debug_format = "expn{}"]
8684
pub struct LocalExpnId {}

compiler/rustc_target/src/abi/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ rustc_index::newtype_index! {
4242
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
4343
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
4444
#[derive(HashStable_Generic)]
45+
#[encodable]
4546
pub struct FieldIdx {}
4647
}
4748

@@ -57,6 +58,7 @@ rustc_index::newtype_index! {
5758
/// `struct`s, `tuples`, and `unions`s are considered to have a single variant
5859
/// with variant index zero, aka [`FIRST_VARIANT`].
5960
#[derive(HashStable_Generic)]
61+
#[encodable]
6062
pub struct VariantIdx {
6163
/// Equivalent to `VariantIdx(0)`.
6264
const FIRST_VARIANT = 0;

compiler/rustc_trait_selection/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ rustc_macros = { path = "../rustc_macros" }
1717
rustc_middle = { path = "../rustc_middle" }
1818
rustc_parse_format = { path = "../rustc_parse_format" }
1919
rustc_query_system = { path = "../rustc_query_system" }
20-
rustc_serialize = { path = "../rustc_serialize" }
2120
rustc_session = { path = "../rustc_session" }
2221
rustc_span = { path = "../rustc_span" }
2322
rustc_target = { path = "../rustc_target" }

compiler/rustc_type_ir/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ rustc_index::newtype_index! {
9191
///
9292
/// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
9393
#[derive(HashStable_Generic)]
94+
#[encodable]
9495
#[debug_format = "DebruijnIndex({})"]
9596
pub struct DebruijnIndex {
9697
const INNERMOST = 0;
@@ -290,6 +291,7 @@ rustc_index::newtype_index! {
290291
/// type -- an idealized representative of "types in general" that we
291292
/// use for checking generic functions.
292293
#[derive(HashStable_Generic)]
294+
#[encodable]
293295
#[debug_format = "U{}"]
294296
pub struct UniverseIndex {}
295297
}

compiler/rustc_type_ir/src/ty_kind.rs

+3
Original file line numberDiff line numberDiff line change
@@ -732,18 +732,21 @@ pub struct FloatVarValue(pub FloatTy);
732732

733733
rustc_index::newtype_index! {
734734
/// A **ty**pe **v**ariable **ID**.
735+
#[encodable]
735736
#[debug_format = "?{}t"]
736737
pub struct TyVid {}
737738
}
738739

739740
rustc_index::newtype_index! {
740741
/// An **int**egral (`u32`, `i32`, `usize`, etc.) type **v**ariable **ID**.
742+
#[encodable]
741743
#[debug_format = "?{}i"]
742744
pub struct IntVid {}
743745
}
744746

745747
rustc_index::newtype_index! {
746748
/// A **float**ing-point (`f32` or `f64`) type **v**ariable **ID**.
749+
#[encodable]
747750
#[debug_format = "?{}f"]
748751
pub struct FloatVid {}
749752
}

0 commit comments

Comments
 (0)