Skip to content

Commit 5a9e0e8

Browse files
committed
Auto merge of rust-lang#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`
2 parents 855c683 + 0991374 commit 5a9e0e8

File tree

43 files changed

+83
-34
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+83
-34
lines changed

Cargo.lock

-8
Original file line numberDiff line numberDiff line change
@@ -3524,7 +3524,6 @@ dependencies = [
35243524
"rustc_macros",
35253525
"rustc_middle",
35263526
"rustc_mir_dataflow",
3527-
"rustc_serialize",
35283527
"rustc_session",
35293528
"rustc_span",
35303529
"rustc_target",
@@ -3935,7 +3934,6 @@ dependencies = [
39353934
"rustc_lint",
39363935
"rustc_macros",
39373936
"rustc_middle",
3938-
"rustc_serialize",
39393937
"rustc_session",
39403938
"rustc_span",
39413939
"rustc_target",
@@ -3998,7 +3996,6 @@ dependencies = [
39983996
"rustc_index",
39993997
"rustc_macros",
40003998
"rustc_middle",
4001-
"rustc_serialize",
40023999
"rustc_span",
40034000
"rustc_target",
40044001
"smallvec",
@@ -4216,7 +4213,6 @@ dependencies = [
42164213
"rustc_infer",
42174214
"rustc_macros",
42184215
"rustc_middle",
4219-
"rustc_serialize",
42204216
"rustc_session",
42214217
"rustc_span",
42224218
"rustc_target",
@@ -4240,7 +4236,6 @@ dependencies = [
42404236
"rustc_index",
42414237
"rustc_macros",
42424238
"rustc_middle",
4243-
"rustc_serialize",
42444239
"rustc_span",
42454240
"rustc_target",
42464241
"smallvec",
@@ -4267,7 +4262,6 @@ dependencies = [
42674262
"rustc_middle",
42684263
"rustc_mir_build",
42694264
"rustc_mir_dataflow",
4270-
"rustc_serialize",
42714265
"rustc_session",
42724266
"rustc_span",
42734267
"rustc_target",
@@ -4341,7 +4335,6 @@ dependencies = [
43414335
"rustc_lexer",
43424336
"rustc_macros",
43434337
"rustc_middle",
4344-
"rustc_serialize",
43454338
"rustc_session",
43464339
"rustc_span",
43474340
"rustc_target",
@@ -4565,7 +4558,6 @@ dependencies = [
45654558
"rustc_middle",
45664559
"rustc_parse_format",
45674560
"rustc_query_system",
4568-
"rustc_serialize",
45694561
"rustc_session",
45704562
"rustc_span",
45714563
"rustc_target",

compiler/rustc_ast/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2574,7 +2574,7 @@ pub enum AttrStyle {
25742574
}
25752575

25762576
rustc_index::newtype_index! {
2577-
#[custom_encodable]
2577+
#[orderable]
25782578
#[debug_format = "AttrId({})"]
25792579
pub struct AttrId {}
25802580
}

compiler/rustc_ast/src/node_id.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ 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]
12+
#[orderable]
1113
#[debug_format = "NodeId({})"]
1214
pub struct NodeId {
1315
/// 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_borrowck/src/constraints/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ rustc_index::newtype_index! {
122122
}
123123

124124
rustc_index::newtype_index! {
125+
#[orderable]
125126
#[debug_format = "ConstraintSccIndex({})"]
126127
pub struct ConstraintSccIndex {}
127128
}

compiler/rustc_borrowck/src/dataflow.rs

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl_visitable! {
109109
}
110110

111111
rustc_index::newtype_index! {
112+
#[orderable]
112113
#[debug_format = "bw{}"]
113114
pub struct BorrowIndex {}
114115
}

compiler/rustc_borrowck/src/location.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct LocationTable {
2020
}
2121

2222
rustc_index::newtype_index! {
23+
#[orderable]
2324
#[debug_format = "LocationIndex({})"]
2425
pub struct LocationIndex {}
2526
}

compiler/rustc_borrowck/src/region_infer/values.rs

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ impl RegionValueElements {
9090
rustc_index::newtype_index! {
9191
/// A single integer representing a `Location` in the MIR control-flow
9292
/// graph. Constructed efficiently from `RegionValueElements`.
93+
#[orderable]
9394
#[debug_format = "PointIndex({})"]
9495
pub struct PointIndex {}
9596
}

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_data_structures/src/graph/dominators/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct PreOrderFrame<Iter> {
2323
}
2424

2525
rustc_index::newtype_index! {
26+
#[orderable]
2627
struct PreorderIndex {}
2728
}
2829

compiler/rustc_hir/src/hir_id.rs

+2
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ 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]
158+
#[orderable]
157159
pub struct ItemLocalId {}
158160
}
159161

compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
198198
// entire graph when there are many connected regions.
199199

200200
rustc_index::newtype_index! {
201-
#[custom_encodable]
201+
#[orderable]
202202
pub struct RegionId {}
203203
}
204204

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_hir_typeck/src/fn_ctxt/arg_matrix.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ use rustc_middle::ty::error::TypeError;
44
use std::cmp;
55

66
rustc_index::newtype_index! {
7+
#[orderable]
78
#[debug_format = "ExpectedIdx({})"]
89
pub(crate) struct ExpectedIdx {}
910
}
1011

1112
rustc_index::newtype_index! {
13+
#[orderable]
1214
#[debug_format = "ProvidedIdx({})"]
1315
pub(crate) struct ProvidedIdx {}
1416
}

compiler/rustc_index/src/vec/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use crate as rustc_index;
33

44
crate::newtype_index! {
5+
#[orderable]
56
#[max = 0xFFFF_FFFA]
67
struct MyIdx {}
78
}

compiler/rustc_index_macros/src/lib.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@ mod newtype;
1818
/// to create/return a value.
1919
///
2020
/// Internally, the index uses a u32, so the index must not exceed
21-
/// `u32::MAX`. You can also customize things like the `Debug` impl,
22-
/// what traits are derived, and so forth via the macro.
21+
/// `u32::MAX`.
22+
///
23+
/// The impls provided by default are Clone, Copy, PartialEq, Eq, and Hash.
24+
///
25+
/// Accepted attributes for customization:
26+
/// - #[derive(HashStable_Generic)]/#[derive(HashStable)]: derives
27+
/// `HashStable`, as normal.
28+
/// - #[encodable]: derives `Encodable`/`Decodable`.
29+
/// - #[orderable]: derives `PartialOrd`/`Ord`, plus step-related methods.
30+
/// - #[debug_format = "Foo({})"]: derives `Debug` with particular output.
31+
/// - #[max = 0xFFFF_FFFD]: specifies the max value, which allows niche
32+
/// optimizations. The default max value is 0xFFFF_FF00.
33+
/// - #[gate_rustc_only]: makes parts of the generated code nightly-only.
2334
#[proc_macro]
2435
#[cfg_attr(
2536
feature = "nightly",

compiler/rustc_index_macros/src/newtype.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ 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;
26-
let mut ord = true;
25+
let mut encodable = false;
26+
let mut ord = false;
2727
let mut gate_rustc_only = quote! {};
2828
let mut gate_rustc_only_cfg = quote! { all() };
2929

@@ -34,12 +34,12 @@ 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
}
41-
"no_ord_impl" => {
42-
ord = false;
41+
"orderable" => {
42+
ord = true;
4343
false
4444
}
4545
"max" => {

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_infer/src/infer/region_constraints/leak_check.rs

+2
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,13 @@ impl<'tcx> SccUniverse<'tcx> {
341341
}
342342

343343
rustc_index::newtype_index! {
344+
#[orderable]
344345
#[debug_format = "LeakCheckNode({})"]
345346
struct LeakCheckNode {}
346347
}
347348

348349
rustc_index::newtype_index! {
350+
#[orderable]
349351
#[debug_format = "LeakCheckScc({})"]
350352
struct LeakCheckScc {}
351353
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ 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]
152+
#[orderable]
151153
pub struct FirstStatementIndex {}
152154
}
153155

compiler/rustc_middle/src/mir/coverage.rs

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ 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]
21+
#[orderable]
2022
#[max = 0xFFFF_FFFF]
2123
#[debug_format = "CounterId({})"]
2224
pub struct CounterId {}
@@ -37,6 +39,8 @@ rustc_index::newtype_index! {
3739
/// Note that LLVM handles expression IDs as `uint32_t`, so there is no need
3840
/// to use a larger representation on the Rust side.
3941
#[derive(HashStable)]
42+
#[encodable]
43+
#[orderable]
4044
#[max = 0xFFFF_FFFF]
4145
#[debug_format = "ExpressionId({})"]
4246
pub struct ExpressionId {}

compiler/rustc_middle/src/mir/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,8 @@ impl SourceInfo {
736736

737737
rustc_index::newtype_index! {
738738
#[derive(HashStable)]
739+
#[encodable]
740+
#[orderable]
739741
#[debug_format = "_{}"]
740742
pub struct Local {
741743
const RETURN_PLACE = 0;
@@ -1171,6 +1173,8 @@ rustc_index::newtype_index! {
11711173
/// [`CriticalCallEdges`]: ../../rustc_const_eval/transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges
11721174
/// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/
11731175
#[derive(HashStable)]
1176+
#[encodable]
1177+
#[orderable]
11741178
#[debug_format = "bb{}"]
11751179
pub struct BasicBlock {
11761180
const START_BLOCK = 0;
@@ -1305,6 +1309,7 @@ impl<'tcx> BasicBlockData<'tcx> {
13051309

13061310
rustc_index::newtype_index! {
13071311
#[derive(HashStable)]
1312+
#[encodable]
13081313
#[debug_format = "scope[{}]"]
13091314
pub struct SourceScope {
13101315
const OUTERMOST_SOURCE_SCOPE = 0;
@@ -1533,6 +1538,8 @@ impl UserTypeProjection {
15331538

15341539
rustc_index::newtype_index! {
15351540
#[derive(HashStable)]
1541+
#[encodable]
1542+
#[orderable]
15361543
#[debug_format = "promoted[{}]"]
15371544
pub struct Promoted {}
15381545
}

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
@@ -1612,6 +1612,8 @@ impl fmt::Debug for EarlyParamRegion {
16121612
rustc_index::newtype_index! {
16131613
/// A **region** (lifetime) **v**ariable **ID**.
16141614
#[derive(HashStable)]
1615+
#[encodable]
1616+
#[orderable]
16151617
#[debug_format = "'?{}"]
16161618
pub struct RegionVid {}
16171619
}

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_build/src/build/scope.rs

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ pub(crate) enum BreakableTarget {
186186
}
187187

188188
rustc_index::newtype_index! {
189+
#[orderable]
189190
struct DropIdx {}
190191
}
191192

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_dataflow/src/move_paths/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use self::abs_domain::{AbstractElem, Lift};
1414
mod abs_domain;
1515

1616
rustc_index::newtype_index! {
17+
#[orderable]
1718
#[debug_format = "mp{}"]
1819
pub struct MovePathIndex {}
1920
}
@@ -25,6 +26,7 @@ impl polonius_engine::Atom for MovePathIndex {
2526
}
2627

2728
rustc_index::newtype_index! {
29+
#[orderable]
2830
#[debug_format = "mo{}"]
2931
pub struct MoveOutIndex {}
3032
}

0 commit comments

Comments
 (0)