Skip to content

Commit 5ca7e81

Browse files
authored
Rollup merge of rust-lang#105880 - Nilstrieb:make-newtypes-less-not-rust, r=oli-obk
Improve syntax of `newtype_index` This makes it more like proper Rust and also makes the implementation a lot simpler. Mostly just turns weird flags in the body into proper attributes. It should probably also be converted to an attribute macro instead of function-like, but that can be done in a future PR.
2 parents 9cd5a4d + 8bfd645 commit 5ca7e81

File tree

37 files changed

+182
-268
lines changed

37 files changed

+182
-268
lines changed

compiler/rustc_ast/src/ast.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -2554,10 +2554,9 @@ pub enum AttrStyle {
25542554
}
25552555

25562556
rustc_index::newtype_index! {
2557-
pub struct AttrId {
2558-
ENCODABLE = custom
2559-
DEBUG_FORMAT = "AttrId({})"
2560-
}
2557+
#[custom_encodable]
2558+
#[debug_format = "AttrId({})]"]
2559+
pub struct AttrId {}
25612560
}
25622561

25632562
impl<S: Encoder> Encodable<S> for AttrId {

compiler/rustc_ast/src/node_id.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +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-
pub struct NodeId {
12-
DEBUG_FORMAT = "NodeId({})"
13-
}
11+
#[debug_format = "NodeId({})"]
12+
pub struct NodeId {}
1413
}
1514

1615
rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeMapEntry, NodeId);

compiler/rustc_borrowck/src/constraints/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,11 @@ impl<'tcx> fmt::Debug for OutlivesConstraint<'tcx> {
115115
}
116116

117117
rustc_index::newtype_index! {
118-
pub struct OutlivesConstraintIndex {
119-
DEBUG_FORMAT = "OutlivesConstraintIndex({})"
120-
}
118+
#[debug_format = "OutlivesConstraintIndex({})"]
119+
pub struct OutlivesConstraintIndex {}
121120
}
122121

123122
rustc_index::newtype_index! {
124-
pub struct ConstraintSccIndex {
125-
DEBUG_FORMAT = "ConstraintSccIndex({})"
126-
}
123+
#[debug_format = "ConstraintSccIndex({})"]
124+
pub struct ConstraintSccIndex {}
127125
}

compiler/rustc_borrowck/src/dataflow.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ impl_visitable! {
108108
}
109109

110110
rustc_index::newtype_index! {
111-
pub struct BorrowIndex {
112-
DEBUG_FORMAT = "bw{}"
113-
}
111+
#[debug_format = "bw{}"]
112+
pub struct BorrowIndex {}
114113
}
115114

116115
/// `Borrows` stores the data used in the analyses that track the flow

compiler/rustc_borrowck/src/location.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ pub struct LocationTable {
2020
}
2121

2222
rustc_index::newtype_index! {
23-
pub struct LocationIndex {
24-
DEBUG_FORMAT = "LocationIndex({})"
25-
}
23+
#[debug_format = "LocationIndex({})"]
24+
pub struct LocationIndex {}
2625
}
2726

2827
#[derive(Copy, Clone, Debug)]

compiler/rustc_borrowck/src/member_constraints.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ pub(crate) struct NllMemberConstraint<'tcx> {
5555
}
5656

5757
rustc_index::newtype_index! {
58-
pub(crate) struct NllMemberConstraintIndex {
59-
DEBUG_FORMAT = "MemberConstraintIndex({})"
60-
}
58+
#[debug_format = "MemberConstraintIndex({})"]
59+
pub(crate) struct NllMemberConstraintIndex {}
6160
}
6261

6362
impl Default for MemberConstraintSet<'_, ty::RegionVid> {

compiler/rustc_borrowck/src/region_infer/values.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,14 @@ 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-
pub struct PointIndex { DEBUG_FORMAT = "PointIndex({})" }
93+
#[debug_format = "PointIndex({})"]
94+
pub struct PointIndex {}
9495
}
9596

9697
rustc_index::newtype_index! {
9798
/// A single integer representing a `ty::Placeholder`.
98-
pub struct PlaceholderIndex { DEBUG_FORMAT = "PlaceholderIndex({})" }
99+
#[debug_format = "PlaceholderIndex({})"]
100+
pub struct PlaceholderIndex {}
99101
}
100102

101103
/// An individual element in a region value -- the value of a

compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct Appearance {
4646
}
4747

4848
rustc_index::newtype_index! {
49-
pub struct AppearanceIndex { .. }
49+
pub struct AppearanceIndex {}
5050
}
5151

5252
impl vll::LinkElem for Appearance {

compiler/rustc_data_structures/src/graph/dominators/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct PreOrderFrame<Iter> {
2222
}
2323

2424
rustc_index::newtype_index! {
25-
struct PreorderIndex { .. }
25+
struct PreorderIndex {}
2626
}
2727

2828
pub fn dominators<G: ControlFlowGraph>(graph: G) -> Dominators<G::Node> {

compiler/rustc_hir/src/hir_id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ rustc_index::newtype_index! {
138138
/// an "item-like" to something else can be implemented by a `Vec` instead of a
139139
/// tree or hash map.
140140
#[derive(HashStable_Generic)]
141-
pub struct ItemLocalId { .. }
141+
pub struct ItemLocalId {}
142142
}
143143

144144
impl ItemLocalId {

compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs

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

200200
rustc_index::newtype_index! {
201-
pub struct RegionId {
202-
ENCODABLE = custom
203-
}
201+
#[custom_encodable]
202+
pub struct RegionId {}
204203
}
204+
205205
struct ConnectedRegion {
206206
idents: SmallVec<[Symbol; 8]>,
207207
impl_blocks: FxHashSet<usize>,

compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ use rustc_index::vec::IndexVec;
44
use rustc_middle::ty::error::TypeError;
55

66
rustc_index::newtype_index! {
7-
pub(crate) struct ExpectedIdx {
8-
DEBUG_FORMAT = "ExpectedIdx({})",
9-
}
7+
#[debug_format = "ExpectedIdx({})"]
8+
pub(crate) struct ExpectedIdx {}
109
}
1110

1211
rustc_index::newtype_index! {
13-
pub(crate) struct ProvidedIdx {
14-
DEBUG_FORMAT = "ProvidedIdx({})",
15-
}
12+
#[debug_format = "ProvidedIdx({})"]
13+
pub(crate) struct ProvidedIdx {}
1614
}
1715

1816
impl ExpectedIdx {

compiler/rustc_hir_typeck/src/generator_interior/drop_ranges/mod.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,13 @@ fn for_each_consumable<'tcx>(hir: Map<'tcx>, place: TrackedValue, mut f: impl Fn
9696
}
9797

9898
rustc_index::newtype_index! {
99-
pub struct PostOrderId {
100-
DEBUG_FORMAT = "id({})",
101-
}
99+
#[debug_format = "id({})"]
100+
pub struct PostOrderId {}
102101
}
103102

104103
rustc_index::newtype_index! {
105-
pub struct TrackedValueIndex {
106-
DEBUG_FORMAT = "hidx({})",
107-
}
104+
#[debug_format = "hidx({})"]
105+
pub struct TrackedValueIndex {}
108106
}
109107

110108
/// Identifies a value whose drop state we need to track.

compiler/rustc_index/src/vec/tests.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
// Allows the macro invocation below to work
44
use crate as rustc_index;
55

6-
rustc_macros::newtype_index!(struct MyIdx { MAX = 0xFFFF_FFFA });
6+
rustc_macros::newtype_index! {
7+
#[max = 0xFFFF_FFFA]
8+
struct MyIdx {}
9+
}
710

811
#[test]
912
fn index_size_is_optimized() {

compiler/rustc_infer/src/infer/region_constraints/leak_check.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -357,15 +357,13 @@ impl<'tcx> SccUniverse<'tcx> {
357357
}
358358

359359
rustc_index::newtype_index! {
360-
struct LeakCheckNode {
361-
DEBUG_FORMAT = "LeakCheckNode({})"
362-
}
360+
#[debug_format = "LeakCheckNode({})"]
361+
struct LeakCheckNode {}
363362
}
364363

365364
rustc_index::newtype_index! {
366-
struct LeakCheckScc {
367-
DEBUG_FORMAT = "LeakCheckScc({})"
368-
}
365+
#[debug_format = "LeakCheckScc({})"]
366+
struct LeakCheckScc {}
369367
}
370368

371369
/// Represents the graph of constraints. For each `R1: R2` constraint we create

compiler/rustc_lint/src/levels.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ struct LintLevelSets {
3939
}
4040

4141
rustc_index::newtype_index! {
42+
#[custom_encodable] // we don't need encoding
4243
struct LintStackIndex {
43-
ENCODABLE = custom, // we don't need encoding
44-
const COMMAND_LINE = 0,
44+
const COMMAND_LINE = 0;
4545
}
4646
}
4747

0 commit comments

Comments
 (0)