Auto merge of #118125 - nnethercote:custom_encodable, r=compiler-errors

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`
This commit is contained in:
bors 2023-11-22 09:29:49 +00:00
commit 5a9e0e8787
43 changed files with 83 additions and 34 deletions

View File

@ -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",

View File

@ -2574,7 +2574,7 @@ pub enum AttrStyle {
}
rustc_index::newtype_index! {
#[custom_encodable]
#[orderable]
#[debug_format = "AttrId({})"]
pub struct AttrId {}
}

View File

@ -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.

View File

@ -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" }

View File

@ -122,6 +122,7 @@ rustc_index::newtype_index! {
}
rustc_index::newtype_index! {
#[orderable]
#[debug_format = "ConstraintSccIndex({})"]
pub struct ConstraintSccIndex {}
}

View File

@ -109,6 +109,7 @@ impl_visitable! {
}
rustc_index::newtype_index! {
#[orderable]
#[debug_format = "bw{}"]
pub struct BorrowIndex {}
}

View File

@ -20,6 +20,7 @@ pub struct LocationTable {
}
rustc_index::newtype_index! {
#[orderable]
#[debug_format = "LocationIndex({})"]
pub struct LocationIndex {}
}

View File

@ -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 {}
}

View File

@ -189,8 +189,6 @@ impl GlobalFileTable {
}
rustc_index::newtype_index! {
// Tell the newtype macro to not generate `Encode`/`Decode` impls.
#[custom_encodable]
struct LocalFileId {}
}

View File

@ -23,6 +23,7 @@ struct PreOrderFrame<Iter> {
}
rustc_index::newtype_index! {
#[orderable]
struct PreorderIndex {}
}

View File

@ -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 {}
}

View File

@ -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 {}
}

View File

@ -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" }

View File

@ -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 {}
}

View File

@ -2,6 +2,7 @@
use crate as rustc_index;
crate::newtype_index! {
#[orderable]
#[max = 0xFFFF_FFFA]
struct MyIdx {}
}

View File

@ -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",

View File

@ -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() };
@ -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" => {

View File

@ -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"] }

View File

@ -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 {}
}

View File

@ -56,7 +56,6 @@ struct LintLevelSets {
}
rustc_index::newtype_index! {
#[custom_encodable] // we don't need encoding
struct LintStackIndex {
const COMMAND_LINE = 0;
}

View File

@ -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 {}
}

View File

@ -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 {}
@ -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 {}

View File

@ -736,6 +736,8 @@ impl SourceInfo {
rustc_index::newtype_index! {
#[derive(HashStable)]
#[encodable]
#[orderable]
#[debug_format = "_{}"]
pub struct Local {
const RETURN_PLACE = 0;
@ -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;
@ -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;
@ -1533,6 +1538,8 @@ impl UserTypeProjection {
rustc_index::newtype_index! {
#[derive(HashStable)]
#[encodable]
#[orderable]
#[debug_format = "promoted[{}]"]
pub struct Promoted {}
}

View File

@ -132,6 +132,7 @@ pub struct UnsafetyCheckResult {
rustc_index::newtype_index! {
#[derive(HashStable)]
#[encodable]
#[debug_format = "_{}"]
pub struct CoroutineSavedLocal {}
}

View File

@ -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 {}
}

View File

@ -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;

View File

@ -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" }

View File

@ -186,6 +186,7 @@ pub(crate) enum BreakableTarget {
}
rustc_index::newtype_index! {
#[orderable]
struct DropIdx {}
}

View File

@ -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"] }

View File

@ -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 {}
}

View File

@ -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" }

View File

@ -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;

View File

@ -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" }

View File

@ -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 {}
}

View File

@ -13,7 +13,7 @@ pub type StableCrateIdMap =
indexmap::IndexMap<StableCrateId, CrateNum, BuildHasherDefault<Unhasher>>;
rustc_index::newtype_index! {
#[custom_encodable]
#[orderable]
#[debug_format = "crate{}"]
pub struct CrateNum {}
}
@ -213,7 +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.
#[custom_encodable] // (only encodable in metadata)
#[orderable]
#[debug_format = "DefIndex({})"]
pub struct DefIndex {
/// The crate root is always assigned index 0 by the AST Map code,
@ -222,6 +222,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>());

View File

@ -60,7 +60,7 @@ pub struct SyntaxContextData {
rustc_index::newtype_index! {
/// A unique ID associated with a macro invocation and expansion.
#[custom_encodable]
#[orderable]
pub struct ExpnIndex {}
}
@ -80,8 +80,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 {}
}

View File

@ -2018,6 +2018,7 @@ impl fmt::Display for MacroRulesNormalizedIdent {
pub struct Symbol(SymbolIndex);
rustc_index::newtype_index! {
#[orderable]
struct SymbolIndex {}
}

View File

@ -42,6 +42,8 @@ rustc_index::newtype_index! {
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
#[derive(HashStable_Generic)]
#[encodable]
#[orderable]
pub struct FieldIdx {}
}
@ -57,6 +59,8 @@ 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]
#[orderable]
pub struct VariantIdx {
/// Equivalent to `VariantIdx(0)`.
const FIRST_VARIANT = 0;

View File

@ -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" }

View File

@ -13,6 +13,7 @@ use rustc_session::Limit;
use std::collections::hash_map::Entry;
rustc_index::newtype_index! {
#[orderable]
pub struct StackDepth {}
}

View File

@ -95,6 +95,8 @@ impl<I: Interner> DebugWithInfcx<I> for ConstKind<I> {
rustc_index::newtype_index! {
/// A **`const`** **v**ariable **ID**.
#[encodable]
#[orderable]
#[debug_format = "?{}c"]
#[gate_rustc_only]
pub struct ConstVid {}
@ -108,6 +110,8 @@ 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]
#[orderable]
#[debug_format = "?{}e"]
#[gate_rustc_only]
pub struct EffectVid {}

View File

@ -92,6 +92,8 @@ 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 {
@ -293,6 +295,8 @@ 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]
#[orderable]
#[debug_format = "U{}"]
#[gate_rustc_only]
pub struct UniverseIndex {}
@ -335,6 +339,8 @@ impl UniverseIndex {
rustc_index::newtype_index! {
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
#[encodable]
#[orderable]
#[debug_format = "{}"]
#[gate_rustc_only]
pub struct BoundVar {}

View File

@ -622,6 +622,8 @@ 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 {}
@ -629,6 +631,8 @@ 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 {}
@ -636,6 +640,8 @@ 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 {}