Skip to content

Commit f608a80

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 2f8d81f commit f608a80

File tree

29 files changed

+27
-28
lines changed

29 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",
@@ -3997,7 +3995,6 @@ dependencies = [
39973995
"rustc_index",
39983996
"rustc_macros",
39993997
"rustc_middle",
4000-
"rustc_serialize",
40013998
"rustc_span",
40023999
"rustc_target",
40034000
"smallvec",
@@ -4215,7 +4212,6 @@ dependencies = [
42154212
"rustc_infer",
42164213
"rustc_macros",
42174214
"rustc_middle",
4218-
"rustc_serialize",
42194215
"rustc_session",
42204216
"rustc_span",
42214217
"rustc_target",
@@ -4239,7 +4235,6 @@ dependencies = [
42394235
"rustc_index",
42404236
"rustc_macros",
42414237
"rustc_middle",
4242-
"rustc_serialize",
42434238
"rustc_span",
42444239
"rustc_target",
42454240
"smallvec",
@@ -4266,7 +4261,6 @@ dependencies = [
42664261
"rustc_middle",
42674262
"rustc_mir_build",
42684263
"rustc_mir_dataflow",
4269-
"rustc_serialize",
42704264
"rustc_session",
42714265
"rustc_span",
42724266
"rustc_target",
@@ -4340,7 +4334,6 @@ dependencies = [
43404334
"rustc_lexer",
43414335
"rustc_macros",
43424336
"rustc_middle",
4343-
"rustc_serialize",
43444337
"rustc_session",
43454338
"rustc_span",
43464339
"rustc_target",
@@ -4564,7 +4557,6 @@ dependencies = [
45644557
"rustc_middle",
45654558
"rustc_parse_format",
45664559
"rustc_query_system",
4567-
"rustc_serialize",
45684560
"rustc_session",
45694561
"rustc_span",
45704562
"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
@@ -154,6 +154,7 @@ rustc_index::newtype_index! {
154154
/// an "item-like" to something else can be implemented by a `Vec` instead of a
155155
/// tree or hash map.
156156
#[derive(HashStable_Generic)]
157+
#[encodable]
157158
pub struct ItemLocalId {}
158159
}
159160

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_index_macros/src/newtype.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ 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
let mut gate_rustc_only = quote! {};
2828
let mut gate_rustc_only_cfg = quote! { all() };
@@ -34,8 +34,8 @@ impl Parse for Newtype {
3434
gate_rustc_only_cfg = quote! { feature = "nightly" };
3535
false
3636
}
37-
"custom_encodable" => {
38-
encodable = false;
37+
"encodable" => {
38+
encodable = true;
3939
false
4040
}
4141
"no_ord_impl" => {

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_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

+2
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,7 @@ impl fmt::Debug for EarlyParamRegion {
16111611
rustc_index::newtype_index! {
16121612
/// A **region** (lifetime) **v**ariable **ID**.
16131613
#[derive(HashStable)]
1614+
#[encodable]
16141615
#[debug_format = "'?{}"]
16151616
pub struct RegionVid {}
16161617
}
@@ -1623,6 +1624,7 @@ impl Atom for RegionVid {
16231624

16241625
rustc_index::newtype_index! {
16251626
#[derive(HashStable)]
1627+
#[encodable]
16261628
#[debug_format = "{}"]
16271629
pub struct BoundVar {}
16281630
}

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/const_kind.rs

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl<I: Interner> DebugWithInfcx<I> for ConstKind<I> {
9595

9696
rustc_index::newtype_index! {
9797
/// A **`const`** **v**ariable **ID**.
98+
#[encodable]
9899
#[debug_format = "?{}c"]
99100
#[gate_rustc_only]
100101
pub struct ConstVid {}
@@ -108,6 +109,7 @@ rustc_index::newtype_index! {
108109
/// relate an effect variable with a normal one, we would ICE, which can catch bugs
109110
/// where we are not correctly using the effect var for an effect param. Fallback
110111
/// is also implemented on top of having separate effect and normal const variables.
112+
#[encodable]
111113
#[debug_format = "?{}e"]
112114
#[gate_rustc_only]
113115
pub struct EffectVid {}

compiler/rustc_type_ir/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ rustc_index::newtype_index! {
9292
///
9393
/// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
9494
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
95+
#[encodable]
9596
#[debug_format = "DebruijnIndex({})"]
9697
#[gate_rustc_only]
9798
pub struct DebruijnIndex {
@@ -293,6 +294,7 @@ rustc_index::newtype_index! {
293294
/// type -- an idealized representative of "types in general" that we
294295
/// use for checking generic functions.
295296
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
297+
#[encodable]
296298
#[debug_format = "U{}"]
297299
#[gate_rustc_only]
298300
pub struct UniverseIndex {}

0 commit comments

Comments
 (0)