From 3ef9d4d0ed608b0493d66ffe8af2755529ce474c Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 21 Nov 2023 16:35:26 +1100 Subject: [PATCH 1/3] 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. --- Cargo.lock | 8 -------- compiler/rustc_ast/src/ast.rs | 1 - compiler/rustc_ast/src/node_id.rs | 1 + compiler/rustc_borrowck/Cargo.toml | 1 - compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs | 2 -- compiler/rustc_hir/src/hir_id.rs | 1 + .../src/coherence/inherent_impls_overlap.rs | 1 - compiler/rustc_hir_typeck/Cargo.toml | 1 - compiler/rustc_index_macros/src/newtype.rs | 6 +++--- compiler/rustc_infer/Cargo.toml | 1 - compiler/rustc_lint/src/levels.rs | 1 - compiler/rustc_middle/src/middle/region.rs | 1 + compiler/rustc_middle/src/mir/coverage.rs | 2 ++ compiler/rustc_middle/src/mir/mod.rs | 4 ++++ compiler/rustc_middle/src/mir/query.rs | 1 + compiler/rustc_middle/src/ty/sty.rs | 1 + compiler/rustc_middle/src/ty/typeck_results.rs | 1 + compiler/rustc_mir_build/Cargo.toml | 1 - compiler/rustc_mir_dataflow/Cargo.toml | 1 - compiler/rustc_mir_transform/Cargo.toml | 1 - compiler/rustc_passes/Cargo.toml | 1 - compiler/rustc_query_system/src/dep_graph/serialized.rs | 1 + compiler/rustc_span/src/def_id.rs | 3 +-- compiler/rustc_span/src/hygiene.rs | 2 -- compiler/rustc_target/src/abi/mod.rs | 2 ++ compiler/rustc_trait_selection/Cargo.toml | 1 - compiler/rustc_type_ir/src/const_kind.rs | 2 ++ compiler/rustc_type_ir/src/lib.rs | 3 +++ compiler/rustc_type_ir/src/ty_kind.rs | 3 +++ 29 files changed, 27 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8d54fe7ef7630..8c9b12028f090 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3524,7 +3524,6 @@ dependencies = [ "rustc_macros", "rustc_middle", "rustc_mir_dataflow", - "rustc_serialize", "rustc_session", "rustc_span", "rustc_target", @@ -3935,7 +3934,6 @@ dependencies = [ "rustc_lint", "rustc_macros", "rustc_middle", - "rustc_serialize", "rustc_session", "rustc_span", "rustc_target", @@ -3998,7 +3996,6 @@ dependencies = [ "rustc_index", "rustc_macros", "rustc_middle", - "rustc_serialize", "rustc_span", "rustc_target", "smallvec", @@ -4216,7 +4213,6 @@ dependencies = [ "rustc_infer", "rustc_macros", "rustc_middle", - "rustc_serialize", "rustc_session", "rustc_span", "rustc_target", @@ -4240,7 +4236,6 @@ dependencies = [ "rustc_index", "rustc_macros", "rustc_middle", - "rustc_serialize", "rustc_span", "rustc_target", "smallvec", @@ -4267,7 +4262,6 @@ dependencies = [ "rustc_middle", "rustc_mir_build", "rustc_mir_dataflow", - "rustc_serialize", "rustc_session", "rustc_span", "rustc_target", @@ -4341,7 +4335,6 @@ dependencies = [ "rustc_lexer", "rustc_macros", "rustc_middle", - "rustc_serialize", "rustc_session", "rustc_span", "rustc_target", @@ -4565,7 +4558,6 @@ dependencies = [ "rustc_middle", "rustc_parse_format", "rustc_query_system", - "rustc_serialize", "rustc_session", "rustc_span", "rustc_target", diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index c85ff6f5c4451..e311352c93dec 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2574,7 +2574,6 @@ pub enum AttrStyle { } rustc_index::newtype_index! { - #[custom_encodable] #[debug_format = "AttrId({})"] pub struct AttrId {} } diff --git a/compiler/rustc_ast/src/node_id.rs b/compiler/rustc_ast/src/node_id.rs index d16741757d1f1..48081582fc320 100644 --- a/compiler/rustc_ast/src/node_id.rs +++ b/compiler/rustc_ast/src/node_id.rs @@ -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. diff --git a/compiler/rustc_borrowck/Cargo.toml b/compiler/rustc_borrowck/Cargo.toml index 79ad242732502..714f46270f92c 100644 --- a/compiler/rustc_borrowck/Cargo.toml +++ b/compiler/rustc_borrowck/Cargo.toml @@ -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" } diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs index 4f540de7ae012..f2be6f27ff6ff 100644 --- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs +++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs @@ -189,8 +189,6 @@ impl GlobalFileTable { } rustc_index::newtype_index! { - // Tell the newtype macro to not generate `Encode`/`Decode` impls. - #[custom_encodable] struct LocalFileId {} } diff --git a/compiler/rustc_hir/src/hir_id.rs b/compiler/rustc_hir/src/hir_id.rs index 7b741e8882dea..c40a317947c03 100644 --- a/compiler/rustc_hir/src/hir_id.rs +++ b/compiler/rustc_hir/src/hir_id.rs @@ -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 {} } diff --git a/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs b/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs index 7205b7a21a823..cbce8c4b8b83d 100644 --- a/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs +++ b/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs @@ -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 {} } diff --git a/compiler/rustc_hir_typeck/Cargo.toml b/compiler/rustc_hir_typeck/Cargo.toml index 0062889d2445f..b0c60304424ad 100644 --- a/compiler/rustc_hir_typeck/Cargo.toml +++ b/compiler/rustc_hir_typeck/Cargo.toml @@ -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" } diff --git a/compiler/rustc_index_macros/src/newtype.rs b/compiler/rustc_index_macros/src/newtype.rs index 2a974fd26281c..62de5c7c6c163 100644 --- a/compiler/rustc_index_macros/src/newtype.rs +++ b/compiler/rustc_index_macros/src/newtype.rs @@ -22,7 +22,7 @@ impl Parse for Newtype { let mut debug_format: Option = 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() }; @@ -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" => { diff --git a/compiler/rustc_infer/Cargo.toml b/compiler/rustc_infer/Cargo.toml index 00251a192264a..73a02a431dfc0 100644 --- a/compiler/rustc_infer/Cargo.toml +++ b/compiler/rustc_infer/Cargo.toml @@ -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"] } diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs index 1281cc9f92030..3fc4f09244301 100644 --- a/compiler/rustc_lint/src/levels.rs +++ b/compiler/rustc_lint/src/levels.rs @@ -56,7 +56,6 @@ struct LintLevelSets { } rustc_index::newtype_index! { - #[custom_encodable] // we don't need encoding struct LintStackIndex { const COMMAND_LINE = 0; } diff --git a/compiler/rustc_middle/src/middle/region.rs b/compiler/rustc_middle/src/middle/region.rs index 9d55295fb66ba..a0765770a8f67 100644 --- a/compiler/rustc_middle/src/middle/region.rs +++ b/compiler/rustc_middle/src/middle/region.rs @@ -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 {} } diff --git a/compiler/rustc_middle/src/mir/coverage.rs b/compiler/rustc_middle/src/mir/coverage.rs index 08d377a8695f9..ccd2cd76ad879 100644 --- a/compiler/rustc_middle/src/mir/coverage.rs +++ b/compiler/rustc_middle/src/mir/coverage.rs @@ -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 {} @@ -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 {} diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index d4778cdccf37b..4e6f5c3fb4e07 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -736,6 +736,7 @@ impl SourceInfo { rustc_index::newtype_index! { #[derive(HashStable)] + #[encodable] #[debug_format = "_{}"] pub struct Local { const RETURN_PLACE = 0; @@ -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; @@ -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; @@ -1533,6 +1536,7 @@ impl UserTypeProjection { rustc_index::newtype_index! { #[derive(HashStable)] + #[encodable] #[debug_format = "promoted[{}]"] pub struct Promoted {} } diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index b5dd3010d3a8b..4f98c302298e4 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -132,6 +132,7 @@ pub struct UnsafetyCheckResult { rustc_index::newtype_index! { #[derive(HashStable)] + #[encodable] #[debug_format = "_{}"] pub struct CoroutineSavedLocal {} } diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 6609da492981a..f3915f6b41035 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1612,6 +1612,7 @@ impl fmt::Debug for EarlyParamRegion { rustc_index::newtype_index! { /// A **region** (lifetime) **v**ariable **ID**. #[derive(HashStable)] + #[encodable] #[debug_format = "'?{}"] pub struct RegionVid {} } diff --git a/compiler/rustc_middle/src/ty/typeck_results.rs b/compiler/rustc_middle/src/ty/typeck_results.rs index 0331546cdd905..4a5c89411daab 100644 --- a/compiler/rustc_middle/src/ty/typeck_results.rs +++ b/compiler/rustc_middle/src/ty/typeck_results.rs @@ -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; diff --git a/compiler/rustc_mir_build/Cargo.toml b/compiler/rustc_mir_build/Cargo.toml index 6dceacd75a543..db54223405230 100644 --- a/compiler/rustc_mir_build/Cargo.toml +++ b/compiler/rustc_mir_build/Cargo.toml @@ -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" } diff --git a/compiler/rustc_mir_dataflow/Cargo.toml b/compiler/rustc_mir_dataflow/Cargo.toml index 61664eb2a6fb0..7199db677c464 100644 --- a/compiler/rustc_mir_dataflow/Cargo.toml +++ b/compiler/rustc_mir_dataflow/Cargo.toml @@ -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"] } diff --git a/compiler/rustc_mir_transform/Cargo.toml b/compiler/rustc_mir_transform/Cargo.toml index ea7f2a413fb7d..c2ca0a6bcb84b 100644 --- a/compiler/rustc_mir_transform/Cargo.toml +++ b/compiler/rustc_mir_transform/Cargo.toml @@ -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" } diff --git a/compiler/rustc_passes/Cargo.toml b/compiler/rustc_passes/Cargo.toml index d4cd452e7e2a2..80e6c104bd46d 100644 --- a/compiler/rustc_passes/Cargo.toml +++ b/compiler/rustc_passes/Cargo.toml @@ -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" } diff --git a/compiler/rustc_query_system/src/dep_graph/serialized.rs b/compiler/rustc_query_system/src/dep_graph/serialized.rs index e97ef8072045d..a70f4138cfb53 100644 --- a/compiler/rustc_query_system/src/dep_graph/serialized.rs +++ b/compiler/rustc_query_system/src/dep_graph/serialized.rs @@ -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 {} } diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index 595babc26ae68..595456e6d6a17 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -13,7 +13,6 @@ pub type StableCrateIdMap = indexmap::IndexMap>; rustc_index::newtype_index! { - #[custom_encodable] #[debug_format = "crate{}"] pub struct CrateNum {} } @@ -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, @@ -222,6 +220,7 @@ rustc_index::newtype_index! { } } +// njn: I don't understand these impl Encodable for DefIndex { default fn encode(&self, _: &mut E) { panic!("cannot encode `DefIndex` with `{}`", std::any::type_name::()); diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 988ff57254c53..8c6600c4e531f 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -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 {} } @@ -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 {} diff --git a/compiler/rustc_target/src/abi/mod.rs b/compiler/rustc_target/src/abi/mod.rs index b00567e87c656..c1fbc2980a97e 100644 --- a/compiler/rustc_target/src/abi/mod.rs +++ b/compiler/rustc_target/src/abi/mod.rs @@ -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 {} } @@ -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; diff --git a/compiler/rustc_trait_selection/Cargo.toml b/compiler/rustc_trait_selection/Cargo.toml index 667ee3d4e1c69..7d098180b9314 100644 --- a/compiler/rustc_trait_selection/Cargo.toml +++ b/compiler/rustc_trait_selection/Cargo.toml @@ -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" } diff --git a/compiler/rustc_type_ir/src/const_kind.rs b/compiler/rustc_type_ir/src/const_kind.rs index a5f90421de5f7..765cbceb7d1c9 100644 --- a/compiler/rustc_type_ir/src/const_kind.rs +++ b/compiler/rustc_type_ir/src/const_kind.rs @@ -95,6 +95,7 @@ impl DebugWithInfcx for ConstKind { rustc_index::newtype_index! { /// A **`const`** **v**ariable **ID**. + #[encodable] #[debug_format = "?{}c"] #[gate_rustc_only] pub struct ConstVid {} @@ -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 {} diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index d293a9341fe29..ba7774863ef47 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -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 { @@ -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 {} @@ -335,6 +337,7 @@ impl UniverseIndex { rustc_index::newtype_index! { #[cfg_attr(feature = "nightly", derive(HashStable_NoContext))] + #[encodable] #[debug_format = "{}"] #[gate_rustc_only] pub struct BoundVar {} diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index 494eeaf3dc881..a3e94d5c13787 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -622,6 +622,7 @@ pub struct FloatVarValue(pub FloatTy); rustc_index::newtype_index! { /// A **ty**pe **v**ariable **ID**. + #[encodable] #[debug_format = "?{}t"] #[gate_rustc_only] pub struct TyVid {} @@ -629,6 +630,7 @@ rustc_index::newtype_index! { rustc_index::newtype_index! { /// An **int**egral (`u32`, `i32`, `usize`, etc.) type **v**ariable **ID**. + #[encodable] #[debug_format = "?{}i"] #[gate_rustc_only] pub struct IntVid {} @@ -636,6 +638,7 @@ rustc_index::newtype_index! { rustc_index::newtype_index! { /// A **float**ing-point (`f32` or `f64`) type **v**ariable **ID**. + #[encodable] #[debug_format = "?{}f"] #[gate_rustc_only] pub struct FloatVid {} From 7060fc8327f457882823fcc1737a964407eed0d6 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 21 Nov 2023 17:35:46 +1100 Subject: [PATCH 2/3] Replace `no_ord_impl` with `orderable`. Similar to the previous commit, this replaces `newtype_index`'s opt-out `no_ord_impl` attribute with the opt-in `orderable` attribute. --- compiler/rustc_ast/src/ast.rs | 1 + compiler/rustc_ast/src/node_id.rs | 1 + compiler/rustc_borrowck/src/constraints/mod.rs | 1 + compiler/rustc_borrowck/src/dataflow.rs | 1 + compiler/rustc_borrowck/src/location.rs | 1 + compiler/rustc_borrowck/src/region_infer/values.rs | 1 + compiler/rustc_data_structures/src/graph/dominators/mod.rs | 1 + compiler/rustc_hir/src/hir_id.rs | 1 + .../src/coherence/inherent_impls_overlap.rs | 1 + compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs | 2 ++ compiler/rustc_index/src/vec/tests.rs | 1 + compiler/rustc_index_macros/src/newtype.rs | 6 +++--- .../rustc_infer/src/infer/region_constraints/leak_check.rs | 2 ++ compiler/rustc_middle/src/middle/region.rs | 1 + compiler/rustc_middle/src/mir/coverage.rs | 2 ++ compiler/rustc_middle/src/mir/mod.rs | 3 +++ compiler/rustc_middle/src/ty/sty.rs | 1 + compiler/rustc_mir_build/src/build/scope.rs | 1 + compiler/rustc_mir_dataflow/src/move_paths/mod.rs | 2 ++ compiler/rustc_mir_transform/src/coverage/graph.rs | 1 + compiler/rustc_span/src/def_id.rs | 2 ++ compiler/rustc_span/src/hygiene.rs | 2 +- compiler/rustc_span/src/symbol.rs | 1 + compiler/rustc_target/src/abi/mod.rs | 2 ++ compiler/rustc_trait_selection/src/solve/search_graph.rs | 1 + compiler/rustc_type_ir/src/const_kind.rs | 2 ++ compiler/rustc_type_ir/src/lib.rs | 3 +++ compiler/rustc_type_ir/src/ty_kind.rs | 3 +++ 28 files changed, 43 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index e311352c93dec..83fe95f16f9ae 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -2574,6 +2574,7 @@ pub enum AttrStyle { } rustc_index::newtype_index! { + #[orderable] #[debug_format = "AttrId({})"] pub struct AttrId {} } diff --git a/compiler/rustc_ast/src/node_id.rs b/compiler/rustc_ast/src/node_id.rs index 48081582fc320..1cd2449530901 100644 --- a/compiler/rustc_ast/src/node_id.rs +++ b/compiler/rustc_ast/src/node_id.rs @@ -9,6 +9,7 @@ rustc_index::newtype_index! { /// /// [`DefId`]: rustc_span::def_id::DefId #[encodable] + #[orderable] #[debug_format = "NodeId({})"] pub struct NodeId { /// The [`NodeId`] used to represent the root of the crate. diff --git a/compiler/rustc_borrowck/src/constraints/mod.rs b/compiler/rustc_borrowck/src/constraints/mod.rs index 315886bbe29ba..041ac75ec016e 100644 --- a/compiler/rustc_borrowck/src/constraints/mod.rs +++ b/compiler/rustc_borrowck/src/constraints/mod.rs @@ -122,6 +122,7 @@ rustc_index::newtype_index! { } rustc_index::newtype_index! { + #[orderable] #[debug_format = "ConstraintSccIndex({})"] pub struct ConstraintSccIndex {} } diff --git a/compiler/rustc_borrowck/src/dataflow.rs b/compiler/rustc_borrowck/src/dataflow.rs index 8676d2ba7c4c0..27b558be3b77f 100644 --- a/compiler/rustc_borrowck/src/dataflow.rs +++ b/compiler/rustc_borrowck/src/dataflow.rs @@ -109,6 +109,7 @@ impl_visitable! { } rustc_index::newtype_index! { + #[orderable] #[debug_format = "bw{}"] pub struct BorrowIndex {} } diff --git a/compiler/rustc_borrowck/src/location.rs b/compiler/rustc_borrowck/src/location.rs index 0e669abfd14aa..6f09393169f3d 100644 --- a/compiler/rustc_borrowck/src/location.rs +++ b/compiler/rustc_borrowck/src/location.rs @@ -20,6 +20,7 @@ pub struct LocationTable { } rustc_index::newtype_index! { + #[orderable] #[debug_format = "LocationIndex({})"] pub struct LocationIndex {} } diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index 38452df32e9ed..96b3a4e6d18fd 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -90,6 +90,7 @@ impl RegionValueElements { rustc_index::newtype_index! { /// A single integer representing a `Location` in the MIR control-flow /// graph. Constructed efficiently from `RegionValueElements`. + #[orderable] #[debug_format = "PointIndex({})"] pub struct PointIndex {} } diff --git a/compiler/rustc_data_structures/src/graph/dominators/mod.rs b/compiler/rustc_data_structures/src/graph/dominators/mod.rs index 5dd414cfd41fa..4b819e1cbd615 100644 --- a/compiler/rustc_data_structures/src/graph/dominators/mod.rs +++ b/compiler/rustc_data_structures/src/graph/dominators/mod.rs @@ -23,6 +23,7 @@ struct PreOrderFrame { } rustc_index::newtype_index! { + #[orderable] struct PreorderIndex {} } diff --git a/compiler/rustc_hir/src/hir_id.rs b/compiler/rustc_hir/src/hir_id.rs index c40a317947c03..d339075c171d1 100644 --- a/compiler/rustc_hir/src/hir_id.rs +++ b/compiler/rustc_hir/src/hir_id.rs @@ -155,6 +155,7 @@ rustc_index::newtype_index! { /// tree or hash map. #[derive(HashStable_Generic)] #[encodable] + #[orderable] pub struct ItemLocalId {} } diff --git a/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs b/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs index cbce8c4b8b83d..3f8c0db8752d3 100644 --- a/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs +++ b/compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs @@ -198,6 +198,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> { // entire graph when there are many connected regions. rustc_index::newtype_index! { + #[orderable] pub struct RegionId {} } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs index 7aadb95d9393e..566d407d23c12 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs @@ -4,11 +4,13 @@ use rustc_middle::ty::error::TypeError; use std::cmp; rustc_index::newtype_index! { + #[orderable] #[debug_format = "ExpectedIdx({})"] pub(crate) struct ExpectedIdx {} } rustc_index::newtype_index! { + #[orderable] #[debug_format = "ProvidedIdx({})"] pub(crate) struct ProvidedIdx {} } diff --git a/compiler/rustc_index/src/vec/tests.rs b/compiler/rustc_index/src/vec/tests.rs index 1959f4e07b7c6..381d79c24fcba 100644 --- a/compiler/rustc_index/src/vec/tests.rs +++ b/compiler/rustc_index/src/vec/tests.rs @@ -2,6 +2,7 @@ use crate as rustc_index; crate::newtype_index! { + #[orderable] #[max = 0xFFFF_FFFA] struct MyIdx {} } diff --git a/compiler/rustc_index_macros/src/newtype.rs b/compiler/rustc_index_macros/src/newtype.rs index 62de5c7c6c163..df1318c835e2b 100644 --- a/compiler/rustc_index_macros/src/newtype.rs +++ b/compiler/rustc_index_macros/src/newtype.rs @@ -23,7 +23,7 @@ impl Parse for Newtype { let mut max = None; let mut consts = Vec::new(); let mut encodable = false; - let mut ord = true; + let mut ord = false; let mut gate_rustc_only = quote! {}; let mut gate_rustc_only_cfg = quote! { all() }; @@ -38,8 +38,8 @@ impl Parse for Newtype { encodable = true; false } - "no_ord_impl" => { - ord = false; + "orderable" => { + ord = true; false } "max" => { diff --git a/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs b/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs index 479343c3ec284..e06596df7b94a 100644 --- a/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs +++ b/compiler/rustc_infer/src/infer/region_constraints/leak_check.rs @@ -341,11 +341,13 @@ impl<'tcx> SccUniverse<'tcx> { } rustc_index::newtype_index! { + #[orderable] #[debug_format = "LeakCheckNode({})"] struct LeakCheckNode {} } rustc_index::newtype_index! { + #[orderable] #[debug_format = "LeakCheckScc({})"] struct LeakCheckScc {} } diff --git a/compiler/rustc_middle/src/middle/region.rs b/compiler/rustc_middle/src/middle/region.rs index a0765770a8f67..b8f04ed03dcaa 100644 --- a/compiler/rustc_middle/src/middle/region.rs +++ b/compiler/rustc_middle/src/middle/region.rs @@ -149,6 +149,7 @@ rustc_index::newtype_index! { /// and thus does not include EXPR_2, but covers the `...`. #[derive(HashStable)] #[encodable] + #[orderable] pub struct FirstStatementIndex {} } diff --git a/compiler/rustc_middle/src/mir/coverage.rs b/compiler/rustc_middle/src/mir/coverage.rs index ccd2cd76ad879..f15ee0082cede 100644 --- a/compiler/rustc_middle/src/mir/coverage.rs +++ b/compiler/rustc_middle/src/mir/coverage.rs @@ -18,6 +18,7 @@ rustc_index::newtype_index! { /// to use a larger representation on the Rust side. #[derive(HashStable)] #[encodable] + #[orderable] #[max = 0xFFFF_FFFF] #[debug_format = "CounterId({})"] pub struct CounterId {} @@ -39,6 +40,7 @@ rustc_index::newtype_index! { /// to use a larger representation on the Rust side. #[derive(HashStable)] #[encodable] + #[orderable] #[max = 0xFFFF_FFFF] #[debug_format = "ExpressionId({})"] pub struct ExpressionId {} diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 4e6f5c3fb4e07..1e5a7401c6f94 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -737,6 +737,7 @@ impl SourceInfo { rustc_index::newtype_index! { #[derive(HashStable)] #[encodable] + #[orderable] #[debug_format = "_{}"] pub struct Local { const RETURN_PLACE = 0; @@ -1173,6 +1174,7 @@ rustc_index::newtype_index! { /// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/ #[derive(HashStable)] #[encodable] + #[orderable] #[debug_format = "bb{}"] pub struct BasicBlock { const START_BLOCK = 0; @@ -1537,6 +1539,7 @@ impl UserTypeProjection { rustc_index::newtype_index! { #[derive(HashStable)] #[encodable] + #[orderable] #[debug_format = "promoted[{}]"] pub struct Promoted {} } diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index f3915f6b41035..f12a512da3137 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1613,6 +1613,7 @@ rustc_index::newtype_index! { /// A **region** (lifetime) **v**ariable **ID**. #[derive(HashStable)] #[encodable] + #[orderable] #[debug_format = "'?{}"] pub struct RegionVid {} } diff --git a/compiler/rustc_mir_build/src/build/scope.rs b/compiler/rustc_mir_build/src/build/scope.rs index d6ca873e9922c..993fee95895ce 100644 --- a/compiler/rustc_mir_build/src/build/scope.rs +++ b/compiler/rustc_mir_build/src/build/scope.rs @@ -186,6 +186,7 @@ pub(crate) enum BreakableTarget { } rustc_index::newtype_index! { + #[orderable] struct DropIdx {} } diff --git a/compiler/rustc_mir_dataflow/src/move_paths/mod.rs b/compiler/rustc_mir_dataflow/src/move_paths/mod.rs index 7ab1a9ed069ff..22cf3999239c1 100644 --- a/compiler/rustc_mir_dataflow/src/move_paths/mod.rs +++ b/compiler/rustc_mir_dataflow/src/move_paths/mod.rs @@ -14,6 +14,7 @@ use self::abs_domain::{AbstractElem, Lift}; mod abs_domain; rustc_index::newtype_index! { + #[orderable] #[debug_format = "mp{}"] pub struct MovePathIndex {} } @@ -25,6 +26,7 @@ impl polonius_engine::Atom for MovePathIndex { } rustc_index::newtype_index! { + #[orderable] #[debug_format = "mo{}"] pub struct MoveOutIndex {} } diff --git a/compiler/rustc_mir_transform/src/coverage/graph.rs b/compiler/rustc_mir_transform/src/coverage/graph.rs index 7defc9ec148a7..0d807db404c8c 100644 --- a/compiler/rustc_mir_transform/src/coverage/graph.rs +++ b/compiler/rustc_mir_transform/src/coverage/graph.rs @@ -264,6 +264,7 @@ impl graph::WithPredecessors for CoverageGraph { rustc_index::newtype_index! { /// A node in the control-flow graph of CoverageGraph. + #[orderable] #[debug_format = "bcb{}"] pub(super) struct BasicCoverageBlock { const START_BCB = 0; diff --git a/compiler/rustc_span/src/def_id.rs b/compiler/rustc_span/src/def_id.rs index 595456e6d6a17..4b5bd75d5b10e 100644 --- a/compiler/rustc_span/src/def_id.rs +++ b/compiler/rustc_span/src/def_id.rs @@ -13,6 +13,7 @@ pub type StableCrateIdMap = indexmap::IndexMap>; rustc_index::newtype_index! { + #[orderable] #[debug_format = "crate{}"] pub struct CrateNum {} } @@ -212,6 +213,7 @@ 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. + #[orderable] #[debug_format = "DefIndex({})"] pub struct DefIndex { /// The crate root is always assigned index 0 by the AST Map code, diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 8c6600c4e531f..b717229b68dff 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -60,6 +60,7 @@ pub struct SyntaxContextData { rustc_index::newtype_index! { /// A unique ID associated with a macro invocation and expansion. + #[orderable] pub struct ExpnIndex {} } @@ -79,7 +80,6 @@ impl fmt::Debug for ExpnId { rustc_index::newtype_index! { /// A unique ID associated with a macro invocation and expansion. - #[no_ord_impl] #[debug_format = "expn{}"] pub struct LocalExpnId {} } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index 2e3b54464050f..ea80bc82bd1f0 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -2018,6 +2018,7 @@ impl fmt::Display for MacroRulesNormalizedIdent { pub struct Symbol(SymbolIndex); rustc_index::newtype_index! { + #[orderable] struct SymbolIndex {} } diff --git a/compiler/rustc_target/src/abi/mod.rs b/compiler/rustc_target/src/abi/mod.rs index c1fbc2980a97e..a274790bffcfd 100644 --- a/compiler/rustc_target/src/abi/mod.rs +++ b/compiler/rustc_target/src/abi/mod.rs @@ -43,6 +43,7 @@ rustc_index::newtype_index! { /// `f` is `FieldIdx(1)` in `VariantIdx(0)`. #[derive(HashStable_Generic)] #[encodable] + #[orderable] pub struct FieldIdx {} } @@ -59,6 +60,7 @@ rustc_index::newtype_index! { /// with variant index zero, aka [`FIRST_VARIANT`]. #[derive(HashStable_Generic)] #[encodable] + #[orderable] pub struct VariantIdx { /// Equivalent to `VariantIdx(0)`. const FIRST_VARIANT = 0; diff --git a/compiler/rustc_trait_selection/src/solve/search_graph.rs b/compiler/rustc_trait_selection/src/solve/search_graph.rs index 68f81a0553617..71adebffc1513 100644 --- a/compiler/rustc_trait_selection/src/solve/search_graph.rs +++ b/compiler/rustc_trait_selection/src/solve/search_graph.rs @@ -13,6 +13,7 @@ use rustc_session::Limit; use std::collections::hash_map::Entry; rustc_index::newtype_index! { + #[orderable] pub struct StackDepth {} } diff --git a/compiler/rustc_type_ir/src/const_kind.rs b/compiler/rustc_type_ir/src/const_kind.rs index 765cbceb7d1c9..409033a2d8d0e 100644 --- a/compiler/rustc_type_ir/src/const_kind.rs +++ b/compiler/rustc_type_ir/src/const_kind.rs @@ -96,6 +96,7 @@ impl DebugWithInfcx for ConstKind { rustc_index::newtype_index! { /// A **`const`** **v**ariable **ID**. #[encodable] + #[orderable] #[debug_format = "?{}c"] #[gate_rustc_only] pub struct ConstVid {} @@ -110,6 +111,7 @@ rustc_index::newtype_index! { /// 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] + #[orderable] #[debug_format = "?{}e"] #[gate_rustc_only] pub struct EffectVid {} diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index ba7774863ef47..2aeb4230bb83c 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -93,6 +93,7 @@ rustc_index::newtype_index! { /// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index #[cfg_attr(feature = "nightly", derive(HashStable_NoContext))] #[encodable] + #[orderable] #[debug_format = "DebruijnIndex({})"] #[gate_rustc_only] pub struct DebruijnIndex { @@ -295,6 +296,7 @@ rustc_index::newtype_index! { /// use for checking generic functions. #[cfg_attr(feature = "nightly", derive(HashStable_NoContext))] #[encodable] + #[orderable] #[debug_format = "U{}"] #[gate_rustc_only] pub struct UniverseIndex {} @@ -338,6 +340,7 @@ impl UniverseIndex { rustc_index::newtype_index! { #[cfg_attr(feature = "nightly", derive(HashStable_NoContext))] #[encodable] + #[orderable] #[debug_format = "{}"] #[gate_rustc_only] pub struct BoundVar {} diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index a3e94d5c13787..3d4e7f77a4f2c 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -623,6 +623,7 @@ pub struct FloatVarValue(pub FloatTy); rustc_index::newtype_index! { /// A **ty**pe **v**ariable **ID**. #[encodable] + #[orderable] #[debug_format = "?{}t"] #[gate_rustc_only] pub struct TyVid {} @@ -631,6 +632,7 @@ rustc_index::newtype_index! { rustc_index::newtype_index! { /// An **int**egral (`u32`, `i32`, `usize`, etc.) type **v**ariable **ID**. #[encodable] + #[orderable] #[debug_format = "?{}i"] #[gate_rustc_only] pub struct IntVid {} @@ -639,6 +641,7 @@ rustc_index::newtype_index! { rustc_index::newtype_index! { /// A **float**ing-point (`f32` or `f64`) type **v**ariable **ID**. #[encodable] + #[orderable] #[debug_format = "?{}f"] #[gate_rustc_only] pub struct FloatVid {} From 0991374bd1c99e1e64f47a884b10932b39ad61c2 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 21 Nov 2023 18:42:34 +1100 Subject: [PATCH 3/3] Document `newtype_index` attributes. --- compiler/rustc_index_macros/src/lib.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_index_macros/src/lib.rs b/compiler/rustc_index_macros/src/lib.rs index f6a6175374acb..13500949a2219 100644 --- a/compiler/rustc_index_macros/src/lib.rs +++ b/compiler/rustc_index_macros/src/lib.rs @@ -18,8 +18,19 @@ mod newtype; /// to create/return a value. /// /// Internally, the index uses a u32, so the index must not exceed -/// `u32::MAX`. You can also customize things like the `Debug` impl, -/// what traits are derived, and so forth via the macro. +/// `u32::MAX`. +/// +/// The impls provided by default are Clone, Copy, PartialEq, Eq, and Hash. +/// +/// Accepted attributes for customization: +/// - #[derive(HashStable_Generic)]/#[derive(HashStable)]: derives +/// `HashStable`, as normal. +/// - #[encodable]: derives `Encodable`/`Decodable`. +/// - #[orderable]: derives `PartialOrd`/`Ord`, plus step-related methods. +/// - #[debug_format = "Foo({})"]: derives `Debug` with particular output. +/// - #[max = 0xFFFF_FFFD]: specifies the max value, which allows niche +/// optimizations. The default max value is 0xFFFF_FF00. +/// - #[gate_rustc_only]: makes parts of the generated code nightly-only. #[proc_macro] #[cfg_attr( feature = "nightly",