Skip to content

Commit

Permalink
Replace custom_encodable with encodable.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nnethercote committed Nov 21, 2023
1 parent 2f8d81f commit f608a80
Show file tree
Hide file tree
Showing 29 changed files with 27 additions and 28 deletions.
8 changes: 0 additions & 8 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3523,7 +3523,6 @@ dependencies = [
"rustc_macros",
"rustc_middle",
"rustc_mir_dataflow",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand Down Expand Up @@ -3934,7 +3933,6 @@ dependencies = [
"rustc_lint",
"rustc_macros",
"rustc_middle",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand Down Expand Up @@ -3997,7 +3995,6 @@ dependencies = [
"rustc_index",
"rustc_macros",
"rustc_middle",
"rustc_serialize",
"rustc_span",
"rustc_target",
"smallvec",
Expand Down Expand Up @@ -4215,7 +4212,6 @@ dependencies = [
"rustc_infer",
"rustc_macros",
"rustc_middle",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand All @@ -4239,7 +4235,6 @@ dependencies = [
"rustc_index",
"rustc_macros",
"rustc_middle",
"rustc_serialize",
"rustc_span",
"rustc_target",
"smallvec",
Expand All @@ -4266,7 +4261,6 @@ dependencies = [
"rustc_middle",
"rustc_mir_build",
"rustc_mir_dataflow",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand Down Expand Up @@ -4340,7 +4334,6 @@ dependencies = [
"rustc_lexer",
"rustc_macros",
"rustc_middle",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand Down Expand Up @@ -4564,7 +4557,6 @@ dependencies = [
"rustc_middle",
"rustc_parse_format",
"rustc_query_system",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2574,7 +2574,6 @@ pub enum AttrStyle {
}

rustc_index::newtype_index! {
#[custom_encodable]
#[debug_format = "AttrId({})"]
pub struct AttrId {}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast/src/node_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rustc_index::newtype_index! {
/// This is later turned into [`DefId`] and `HirId` for the HIR.
///
/// [`DefId`]: rustc_span::def_id::DefId
#[encodable]
#[debug_format = "NodeId({})"]
pub struct NodeId {
/// The [`NodeId`] used to represent the root of the crate.
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_borrowck/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ rustc_lexer = { path = "../rustc_lexer" }
rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }
rustc_mir_dataflow = { path = "../rustc_mir_dataflow" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ impl GlobalFileTable {
}

rustc_index::newtype_index! {
// Tell the newtype macro to not generate `Encode`/`Decode` impls.
#[custom_encodable]
struct LocalFileId {}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/hir_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ rustc_index::newtype_index! {
/// an "item-like" to something else can be implemented by a `Vec` instead of a
/// tree or hash map.
#[derive(HashStable_Generic)]
#[encodable]
pub struct ItemLocalId {}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
// entire graph when there are many connected regions.

rustc_index::newtype_index! {
#[custom_encodable]
pub struct RegionId {}
}

Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_hir_typeck/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ rustc_infer = { path = "../rustc_infer" }
rustc_lint = { path = "../rustc_lint" }
rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_index_macros/src/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Parse for Newtype {
let mut debug_format: Option<Lit> = None;
let mut max = None;
let mut consts = Vec::new();
let mut encodable = true;
let mut encodable = false;
let mut ord = true;
let mut gate_rustc_only = quote! {};
let mut gate_rustc_only_cfg = quote! { all() };
Expand All @@ -34,8 +34,8 @@ impl Parse for Newtype {
gate_rustc_only_cfg = quote! { feature = "nightly" };
false
}
"custom_encodable" => {
encodable = false;
"encodable" => {
encodable = true;
false
}
"no_ord_impl" => {
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_infer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ rustc_hir = { path = "../rustc_hir" }
rustc_index = { path = "../rustc_index" }
rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_lint/src/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ struct LintLevelSets {
}

rustc_index::newtype_index! {
#[custom_encodable] // we don't need encoding
struct LintStackIndex {
const COMMAND_LINE = 0;
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ rustc_index::newtype_index! {
/// * The subscope with `first_statement_index == 1` is scope of `c`,
/// and thus does not include EXPR_2, but covers the `...`.
#[derive(HashStable)]
#[encodable]
pub struct FirstStatementIndex {}
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/mir/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rustc_index::newtype_index! {
/// Note that LLVM handles counter IDs as `uint32_t`, so there is no need
/// to use a larger representation on the Rust side.
#[derive(HashStable)]
#[encodable]
#[max = 0xFFFF_FFFF]
#[debug_format = "CounterId({})"]
pub struct CounterId {}
Expand All @@ -37,6 +38,7 @@ rustc_index::newtype_index! {
/// Note that LLVM handles expression IDs as `uint32_t`, so there is no need
/// to use a larger representation on the Rust side.
#[derive(HashStable)]
#[encodable]
#[max = 0xFFFF_FFFF]
#[debug_format = "ExpressionId({})"]
pub struct ExpressionId {}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ impl SourceInfo {

rustc_index::newtype_index! {
#[derive(HashStable)]
#[encodable]
#[debug_format = "_{}"]
pub struct Local {
const RETURN_PLACE = 0;
Expand Down Expand Up @@ -1171,6 +1172,7 @@ rustc_index::newtype_index! {
/// [`CriticalCallEdges`]: ../../rustc_const_eval/transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges
/// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/
#[derive(HashStable)]
#[encodable]
#[debug_format = "bb{}"]
pub struct BasicBlock {
const START_BLOCK = 0;
Expand Down Expand Up @@ -1305,6 +1307,7 @@ impl<'tcx> BasicBlockData<'tcx> {

rustc_index::newtype_index! {
#[derive(HashStable)]
#[encodable]
#[debug_format = "scope[{}]"]
pub struct SourceScope {
const OUTERMOST_SOURCE_SCOPE = 0;
Expand Down Expand Up @@ -1533,6 +1536,7 @@ impl UserTypeProjection {

rustc_index::newtype_index! {
#[derive(HashStable)]
#[encodable]
#[debug_format = "promoted[{}]"]
pub struct Promoted {}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/mir/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ pub struct UnsafetyCheckResult {

rustc_index::newtype_index! {
#[derive(HashStable)]
#[encodable]
#[debug_format = "_{}"]
pub struct CoroutineSavedLocal {}
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,7 @@ impl fmt::Debug for EarlyParamRegion {
rustc_index::newtype_index! {
/// A **region** (lifetime) **v**ariable **ID**.
#[derive(HashStable)]
#[encodable]
#[debug_format = "'?{}"]
pub struct RegionVid {}
}
Expand All @@ -1623,6 +1624,7 @@ impl Atom for RegionVid {

rustc_index::newtype_index! {
#[derive(HashStable)]
#[encodable]
#[debug_format = "{}"]
pub struct BoundVar {}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/typeck_results.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ impl<'a, V> LocalTableInContextMut<'a, V> {

rustc_index::newtype_index! {
#[derive(HashStable)]
#[encodable]
#[debug_format = "UserType({})"]
pub struct UserTypeAnnotationIndex {
const START_INDEX = 0;
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_mir_build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ rustc_index = { path = "../rustc_index" }
rustc_infer = { path = "../rustc_infer" }
rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_mir_dataflow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ rustc_hir = { path = "../rustc_hir" }
rustc_index = { path = "../rustc_index" }
rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_mir_transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }
rustc_mir_build = { path = "../rustc_mir_build" }
rustc_mir_dataflow = { path = "../rustc_mir_dataflow" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_passes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ rustc_index = { path = "../rustc_index" }
rustc_lexer = { path = "../rustc_lexer" }
rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_query_system/src/dep_graph/serialized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use std::marker::PhantomData;
// unused so that we can store multiple index types in `CompressedHybridIndex`,
// and use those bits to encode which index type it contains.
rustc_index::newtype_index! {
#[encodable]
#[max = 0x7FFF_FFFF]
pub struct SerializedDepNodeIndex {}
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_span/src/def_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub type StableCrateIdMap =
indexmap::IndexMap<StableCrateId, CrateNum, BuildHasherDefault<Unhasher>>;

rustc_index::newtype_index! {
#[custom_encodable]
#[debug_format = "crate{}"]
pub struct CrateNum {}
}
Expand Down Expand Up @@ -213,7 +212,6 @@ rustc_index::newtype_index! {
/// A DefIndex is an index into the hir-map for a crate, identifying a
/// particular definition. It should really be considered an interned
/// shorthand for a particular DefPath.
#[custom_encodable] // (only encodable in metadata)
#[debug_format = "DefIndex({})"]
pub struct DefIndex {
/// The crate root is always assigned index 0 by the AST Map code,
Expand All @@ -222,6 +220,7 @@ rustc_index::newtype_index! {
}
}

// njn: I don't understand these
impl<E: Encoder> Encodable<E> for DefIndex {
default fn encode(&self, _: &mut E) {
panic!("cannot encode `DefIndex` with `{}`", std::any::type_name::<E>());
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ pub struct SyntaxContextData {

rustc_index::newtype_index! {
/// A unique ID associated with a macro invocation and expansion.
#[custom_encodable]
pub struct ExpnIndex {}
}

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

rustc_index::newtype_index! {
/// A unique ID associated with a macro invocation and expansion.
#[custom_encodable]
#[no_ord_impl]
#[debug_format = "expn{}"]
pub struct LocalExpnId {}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ rustc_index::newtype_index! {
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
#[derive(HashStable_Generic)]
#[encodable]
pub struct FieldIdx {}
}

Expand All @@ -57,6 +58,7 @@ rustc_index::newtype_index! {
/// `struct`s, `tuples`, and `unions`s are considered to have a single variant
/// with variant index zero, aka [`FIRST_VARIANT`].
#[derive(HashStable_Generic)]
#[encodable]
pub struct VariantIdx {
/// Equivalent to `VariantIdx(0)`.
const FIRST_VARIANT = 0;
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_trait_selection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ rustc_macros = { path = "../rustc_macros" }
rustc_middle = { path = "../rustc_middle" }
rustc_parse_format = { path = "../rustc_parse_format" }
rustc_query_system = { path = "../rustc_query_system" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_session = { path = "../rustc_session" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_type_ir/src/const_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ impl<I: Interner> DebugWithInfcx<I> for ConstKind<I> {

rustc_index::newtype_index! {
/// A **`const`** **v**ariable **ID**.
#[encodable]
#[debug_format = "?{}c"]
#[gate_rustc_only]
pub struct ConstVid {}
Expand All @@ -108,6 +109,7 @@ rustc_index::newtype_index! {
/// relate an effect variable with a normal one, we would ICE, which can catch bugs
/// where we are not correctly using the effect var for an effect param. Fallback
/// is also implemented on top of having separate effect and normal const variables.
#[encodable]
#[debug_format = "?{}e"]
#[gate_rustc_only]
pub struct EffectVid {}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_type_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ rustc_index::newtype_index! {
///
/// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
#[encodable]
#[debug_format = "DebruijnIndex({})"]
#[gate_rustc_only]
pub struct DebruijnIndex {
Expand Down Expand Up @@ -293,6 +294,7 @@ rustc_index::newtype_index! {
/// type -- an idealized representative of "types in general" that we
/// use for checking generic functions.
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
#[encodable]
#[debug_format = "U{}"]
#[gate_rustc_only]
pub struct UniverseIndex {}
Expand Down
Loading

0 comments on commit f608a80

Please sign in to comment.