Skip to content

Commit

Permalink
Auto merge of #118125 - nnethercote:custom_encodable, r=compiler-errors
Browse files Browse the repository at this point in the history
Make some `newtype_index!` derived impls opt-in instead of opt-out

Opt-in is the standard Rust way of doing things, and avoids some unnecessary dependencies on the `rustc_serialize` crate.

r? `@lcnr`
  • Loading branch information
bors committed Nov 22, 2023
2 parents 855c683 + 0991374 commit 5a9e0e8
Show file tree
Hide file tree
Showing 43 changed files with 83 additions and 34 deletions.
8 changes: 0 additions & 8 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3524,7 +3524,6 @@ dependencies = [
"rustc_macros",
"rustc_middle",
"rustc_mir_dataflow",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand Down Expand Up @@ -3935,7 +3934,6 @@ dependencies = [
"rustc_lint",
"rustc_macros",
"rustc_middle",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand Down Expand Up @@ -3998,7 +3996,6 @@ dependencies = [
"rustc_index",
"rustc_macros",
"rustc_middle",
"rustc_serialize",
"rustc_span",
"rustc_target",
"smallvec",
Expand Down Expand Up @@ -4216,7 +4213,6 @@ dependencies = [
"rustc_infer",
"rustc_macros",
"rustc_middle",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand All @@ -4240,7 +4236,6 @@ dependencies = [
"rustc_index",
"rustc_macros",
"rustc_middle",
"rustc_serialize",
"rustc_span",
"rustc_target",
"smallvec",
Expand All @@ -4267,7 +4262,6 @@ dependencies = [
"rustc_middle",
"rustc_mir_build",
"rustc_mir_dataflow",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand Down Expand Up @@ -4341,7 +4335,6 @@ dependencies = [
"rustc_lexer",
"rustc_macros",
"rustc_middle",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand Down Expand Up @@ -4565,7 +4558,6 @@ dependencies = [
"rustc_middle",
"rustc_parse_format",
"rustc_query_system",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2574,7 +2574,7 @@ pub enum AttrStyle {
}

rustc_index::newtype_index! {
#[custom_encodable]
#[orderable]
#[debug_format = "AttrId({})"]
pub struct AttrId {}
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_ast/src/node_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ rustc_index::newtype_index! {
/// This is later turned into [`DefId`] and `HirId` for the HIR.
///
/// [`DefId`]: rustc_span::def_id::DefId
#[encodable]
#[orderable]
#[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
1 change: 1 addition & 0 deletions compiler/rustc_borrowck/src/constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ rustc_index::newtype_index! {
}

rustc_index::newtype_index! {
#[orderable]
#[debug_format = "ConstraintSccIndex({})"]
pub struct ConstraintSccIndex {}
}
1 change: 1 addition & 0 deletions compiler/rustc_borrowck/src/dataflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ impl_visitable! {
}

rustc_index::newtype_index! {
#[orderable]
#[debug_format = "bw{}"]
pub struct BorrowIndex {}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_borrowck/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct LocationTable {
}

rustc_index::newtype_index! {
#[orderable]
#[debug_format = "LocationIndex({})"]
pub struct LocationIndex {}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_borrowck/src/region_infer/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
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_data_structures/src/graph/dominators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct PreOrderFrame<Iter> {
}

rustc_index::newtype_index! {
#[orderable]
struct PreorderIndex {}
}

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_hir/src/hir_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ 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]
#[orderable]
pub struct ItemLocalId {}
}

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

rustc_index::newtype_index! {
#[custom_encodable]
#[orderable]
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
2 changes: 2 additions & 0 deletions compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_index/src/vec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate as rustc_index;

crate::newtype_index! {
#[orderable]
#[max = 0xFFFF_FFFA]
struct MyIdx {}
}
Expand Down
15 changes: 13 additions & 2 deletions compiler/rustc_index_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_index_macros/src/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ 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 ord = true;
let mut encodable = false;
let mut ord = false;
let mut gate_rustc_only = quote! {};
let mut gate_rustc_only_cfg = quote! { all() };

Expand All @@ -34,12 +34,12 @@ impl Parse for Newtype {
gate_rustc_only_cfg = quote! { feature = "nightly" };
false
}
"custom_encodable" => {
encodable = false;
"encodable" => {
encodable = true;
false
}
"no_ord_impl" => {
ord = false;
"orderable" => {
ord = true;
false
}
"max" => {
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
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
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ 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]
#[orderable]
pub struct FirstStatementIndex {}
}

Expand Down
4 changes: 4 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,8 @@ 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]
#[orderable]
#[max = 0xFFFF_FFFF]
#[debug_format = "CounterId({})"]
pub struct CounterId {}
Expand All @@ -37,6 +39,8 @@ 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]
#[orderable]
#[max = 0xFFFF_FFFF]
#[debug_format = "ExpressionId({})"]
pub struct ExpressionId {}
Expand Down
7 changes: 7 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,8 @@ impl SourceInfo {

rustc_index::newtype_index! {
#[derive(HashStable)]
#[encodable]
#[orderable]
#[debug_format = "_{}"]
pub struct Local {
const RETURN_PLACE = 0;
Expand Down Expand Up @@ -1171,6 +1173,8 @@ 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]
#[orderable]
#[debug_format = "bb{}"]
pub struct BasicBlock {
const START_BLOCK = 0;
Expand Down Expand Up @@ -1305,6 +1309,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 +1538,8 @@ impl UserTypeProjection {

rustc_index::newtype_index! {
#[derive(HashStable)]
#[encodable]
#[orderable]
#[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 @@ -1612,6 +1612,8 @@ impl fmt::Debug for EarlyParamRegion {
rustc_index::newtype_index! {
/// A **region** (lifetime) **v**ariable **ID**.
#[derive(HashStable)]
#[encodable]
#[orderable]
#[debug_format = "'?{}"]
pub struct RegionVid {}
}
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: 1 addition & 0 deletions compiler/rustc_mir_build/src/build/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ pub(crate) enum BreakableTarget {
}

rustc_index::newtype_index! {
#[orderable]
struct DropIdx {}
}

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
2 changes: 2 additions & 0 deletions compiler/rustc_mir_dataflow/src/move_paths/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use self::abs_domain::{AbstractElem, Lift};
mod abs_domain;

rustc_index::newtype_index! {
#[orderable]
#[debug_format = "mp{}"]
pub struct MovePathIndex {}
}
Expand All @@ -25,6 +26,7 @@ impl polonius_engine::Atom for MovePathIndex {
}

rustc_index::newtype_index! {
#[orderable]
#[debug_format = "mo{}"]
pub struct MoveOutIndex {}
}
Expand Down
Loading

0 comments on commit 5a9e0e8

Please sign in to comment.