Skip to content

Commit d679764

Browse files
committed
Make #[debug_format] an attribute in newtype_index
This removes the `custom` format functionality as its only user was trivially migrated to using a normal format. If a new use case for a custom formatting impl pops up, you can add it back.
1 parent 91c3c20 commit d679764

File tree

24 files changed

+64
-92
lines changed

24 files changed

+64
-92
lines changed

compiler/rustc_ast/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2555,8 +2555,8 @@ pub enum AttrStyle {
25552555

25562556
rustc_index::newtype_index! {
25572557
#[custom_encodable]
2558+
#[debug_format = "AttrId({})]"]
25582559
pub struct AttrId {
2559-
DEBUG_FORMAT = "AttrId({})"
25602560
}
25612561
}
25622562

compiler/rustc_ast/src/node_id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +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+
#[debug_format = "NodeId({})"]
1112
pub struct NodeId {
12-
DEBUG_FORMAT = "NodeId({})"
1313
}
1414
}
1515

compiler/rustc_borrowck/src/constraints/mod.rs

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

117117
rustc_index::newtype_index! {
118+
#[debug_format = "OutlivesConstraintIndex({})"]
118119
pub struct OutlivesConstraintIndex {
119-
DEBUG_FORMAT = "OutlivesConstraintIndex({})"
120120
}
121121
}
122122

123123
rustc_index::newtype_index! {
124+
#[debug_format = "ConstraintSccIndex({})"]
124125
pub struct ConstraintSccIndex {
125-
DEBUG_FORMAT = "ConstraintSccIndex({})"
126126
}
127127
}

compiler/rustc_borrowck/src/dataflow.rs

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

110110
rustc_index::newtype_index! {
111+
#[debug_format = "bw{}"]
111112
pub struct BorrowIndex {
112-
DEBUG_FORMAT = "bw{}"
113113
}
114114
}
115115

compiler/rustc_borrowck/src/location.rs

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

2222
rustc_index::newtype_index! {
23+
#[debug_format = "LocationIndex({})"]
2324
pub struct LocationIndex {
24-
DEBUG_FORMAT = "LocationIndex({})"
2525
}
2626
}
2727

compiler/rustc_borrowck/src/member_constraints.rs

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

5757
rustc_index::newtype_index! {
58+
#[debug_format = "MemberConstraintIndex({})"]
5859
pub(crate) struct NllMemberConstraintIndex {
59-
DEBUG_FORMAT = "MemberConstraintIndex({})"
6060
}
6161
}
6262

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

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

66
rustc_index::newtype_index! {
7+
#[debug_format = "ExpectedIdx({})"]
78
pub(crate) struct ExpectedIdx {
8-
DEBUG_FORMAT = "ExpectedIdx({})",
99
}
1010
}
1111

1212
rustc_index::newtype_index! {
13+
#[debug_format = "ProvidedIdx({})"]
1314
pub(crate) struct ProvidedIdx {
14-
DEBUG_FORMAT = "ProvidedIdx({})",
1515
}
1616
}
1717

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

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

9898
rustc_index::newtype_index! {
99+
#[debug_format = "id({})"]
99100
pub struct PostOrderId {
100-
DEBUG_FORMAT = "id({})",
101101
}
102102
}
103103

104104
rustc_index::newtype_index! {
105+
#[debug_format = "hidx({})"]
105106
pub struct TrackedValueIndex {
106-
DEBUG_FORMAT = "hidx({})",
107107
}
108108
}
109109

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,14 @@ impl<'tcx> SccUniverse<'tcx> {
357357
}
358358

359359
rustc_index::newtype_index! {
360+
#[debug_format = "LeakCheckNode({})"]
360361
struct LeakCheckNode {
361-
DEBUG_FORMAT = "LeakCheckNode({})"
362362
}
363363
}
364364

365365
rustc_index::newtype_index! {
366+
#[debug_format = "LeakCheckScc({})"]
366367
struct LeakCheckScc {
367-
DEBUG_FORMAT = "LeakCheckScc({})"
368368
}
369369
}
370370

compiler/rustc_macros/src/newtype.rs

+21-45
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,6 @@ use quote::quote;
33
use syn::parse::*;
44
use syn::*;
55

6-
mod kw {
7-
syn::custom_keyword!(DEBUG_FORMAT);
8-
syn::custom_keyword!(MAX);
9-
syn::custom_keyword!(custom);
10-
}
11-
12-
#[derive(Debug)]
13-
enum DebugFormat {
14-
// The user will provide a custom `Debug` impl, so we shouldn't generate
15-
// one
16-
Custom,
17-
// Use the specified format string in the generated `Debug` impl
18-
// By default, this is "{}"
19-
Format(String),
20-
}
21-
226
// We parse the input and emit the output in a single step.
237
// This field stores the final macro output
248
struct Newtype(TokenStream);
@@ -35,7 +19,7 @@ impl Parse for Newtype {
3519

3620
// Any additional `#[derive]` macro paths to apply
3721
let mut derive_paths: Vec<Path> = Vec::new();
38-
let mut debug_format: Option<DebugFormat> = None;
22+
let mut debug_format: Option<Lit> = None;
3923
let mut max = None;
4024
let mut consts = Vec::new();
4125
let mut encodable = true;
@@ -65,7 +49,18 @@ impl Parse for Newtype {
6549
};
6650

6751
if let Some(old) = max.replace(literal.lit) {
68-
panic!("Specified multiple MAX: {:?}", old);
52+
panic!("Specified multiple max: {:?}", old);
53+
}
54+
55+
false
56+
}
57+
"debug_format" => {
58+
let Ok(Meta::NameValue(literal) )= attr.parse_meta() else {
59+
panic!("#[debug_format = FMT] attribute requires a format");
60+
};
61+
62+
if let Some(old) = debug_format.replace(literal.lit) {
63+
panic!("Specified multiple debug format options: {:?}", old);
6964
}
7065

7166
false
@@ -79,23 +74,6 @@ impl Parse for Newtype {
7974
body.parse::<Token![..]>()?;
8075
} else {
8176
loop {
82-
if body.lookahead1().peek(kw::DEBUG_FORMAT) {
83-
body.parse::<kw::DEBUG_FORMAT>()?;
84-
body.parse::<Token![=]>()?;
85-
let new_debug_format = if body.lookahead1().peek(kw::custom) {
86-
body.parse::<kw::custom>()?;
87-
DebugFormat::Custom
88-
} else {
89-
let format_str: LitStr = body.parse()?;
90-
DebugFormat::Format(format_str.value())
91-
};
92-
try_comma()?;
93-
if let Some(old) = debug_format.replace(new_debug_format) {
94-
panic!("Specified multiple debug format options: {:?}", old);
95-
}
96-
continue;
97-
}
98-
9977
// We've parsed everything that the user provided, so we're done
10078
if body.is_empty() {
10179
break;
@@ -112,7 +90,9 @@ impl Parse for Newtype {
11290
}
11391
}
11492

115-
let debug_format = debug_format.unwrap_or(DebugFormat::Format("{}".to_string()));
93+
let debug_format =
94+
debug_format.unwrap_or_else(|| Lit::Str(LitStr::new("{}", Span::call_site())));
95+
11696
// shave off 256 indices at the end to allow space for packing these indices into enums
11797
let max = max.unwrap_or_else(|| Lit::Int(LitInt::new("0xFFFF_FF00", Span::call_site())));
11898

@@ -167,18 +147,14 @@ impl Parse for Newtype {
167147
quote! {}
168148
};
169149

170-
let debug_impl = match debug_format {
171-
DebugFormat::Custom => quote! {},
172-
DebugFormat::Format(format) => {
173-
quote! {
174-
impl ::std::fmt::Debug for #name {
175-
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
176-
write!(fmt, #format, self.as_u32())
177-
}
178-
}
150+
let debug_impl = quote! {
151+
impl ::std::fmt::Debug for #name {
152+
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
153+
write!(fmt, #debug_format, self.as_u32())
179154
}
180155
}
181156
};
157+
182158
let spec_partial_eq_impl = if let Lit::Int(max) = &max {
183159
if let Ok(max_val) = max.base10_parse::<u32>() {
184160
quote! {

compiler/rustc_middle/src/mir/coverage.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ rustc_index::newtype_index! {
1212
/// constant value of `0`.
1313
#[derive(HashStable)]
1414
#[max = 0xFFFF_FFFF]
15+
#[debug_format = "ExpressionOperandId({})"]
1516
pub struct ExpressionOperandId {
16-
DEBUG_FORMAT = "ExpressionOperandId({})",
1717
}
1818
}
1919

@@ -34,8 +34,8 @@ impl ExpressionOperandId {
3434
rustc_index::newtype_index! {
3535
#[derive(HashStable)]
3636
#[max = 0xFFFF_FFFF]
37+
#[debug_format = "CounterValueReference({})"]
3738
pub struct CounterValueReference {
38-
DEBUG_FORMAT = "CounterValueReference({})",
3939
}
4040
}
4141

@@ -58,8 +58,8 @@ rustc_index::newtype_index! {
5858
/// Values descend from u32::MAX.
5959
#[derive(HashStable)]
6060
#[max = 0xFFFF_FFFF]
61+
#[debug_format = "InjectedExpressionId({})"]
6162
pub struct InjectedExpressionId {
62-
DEBUG_FORMAT = "InjectedExpressionId({})",
6363
}
6464
}
6565

@@ -69,8 +69,8 @@ rustc_index::newtype_index! {
6969
/// Values ascend from 0.
7070
#[derive(HashStable)]
7171
#[max = 0xFFFF_FFFF]
72+
#[debug_format = "InjectedExpressionIndex({})"]
7273
pub struct InjectedExpressionIndex {
73-
DEBUG_FORMAT = "InjectedExpressionIndex({})",
7474
}
7575
}
7676

@@ -80,8 +80,8 @@ rustc_index::newtype_index! {
8080
/// "mapgen" process. They cannot be computed algorithmically, from the other `newtype_index`s.
8181
#[derive(HashStable)]
8282
#[max = 0xFFFF_FFFF]
83+
#[debug_format = "MappedExpressionIndex({})"]
8384
pub struct MappedExpressionIndex {
84-
DEBUG_FORMAT = "MappedExpressionIndex({})",
8585
}
8686
}
8787

compiler/rustc_middle/src/mir/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -655,8 +655,8 @@ impl SourceInfo {
655655

656656
rustc_index::newtype_index! {
657657
#[derive(HashStable)]
658+
#[debug_format = "_{}"]
658659
pub struct Local {
659-
DEBUG_FORMAT = "_{}",
660660
const RETURN_PLACE = 0,
661661
}
662662
}
@@ -1147,8 +1147,8 @@ rustc_index::newtype_index! {
11471147
/// [`CriticalCallEdges`]: ../../rustc_const_eval/transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges
11481148
/// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/
11491149
#[derive(HashStable)]
1150+
#[debug_format = "bb{}"]
11501151
pub struct BasicBlock {
1151-
DEBUG_FORMAT = "bb{}",
11521152
const START_BLOCK = 0,
11531153
}
11541154
}
@@ -1531,8 +1531,8 @@ rustc_index::newtype_index! {
15311531
/// [CFG]: https://rustc-dev-guide.rust-lang.org/appendix/background.html#cfg
15321532
/// [mir-datatypes]: https://rustc-dev-guide.rust-lang.org/mir/index.html#mir-data-types
15331533
#[derive(HashStable)]
1534+
#[debug_format = "field[{}]"]
15341535
pub struct Field {
1535-
DEBUG_FORMAT = "field[{}]"
15361536
}
15371537
}
15381538

@@ -1758,8 +1758,8 @@ impl Debug for Place<'_> {
17581758

17591759
rustc_index::newtype_index! {
17601760
#[derive(HashStable)]
1761+
#[debug_format = "scope[{}]"]
17611762
pub struct SourceScope {
1762-
DEBUG_FORMAT = "scope[{}]",
17631763
const OUTERMOST_SOURCE_SCOPE = 0,
17641764
}
17651765
}
@@ -2756,8 +2756,8 @@ impl<'tcx> TypeVisitable<'tcx> for UserTypeProjection {
27562756

27572757
rustc_index::newtype_index! {
27582758
#[derive(HashStable)]
2759+
#[debug_format = "promoted[{}]"]
27592760
pub struct Promoted {
2760-
DEBUG_FORMAT = "promoted[{}]"
27612761
}
27622762
}
27632763

compiler/rustc_middle/src/mir/query.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ pub struct UnsafetyCheckResult {
131131

132132
rustc_index::newtype_index! {
133133
#[derive(HashStable)]
134+
#[debug_format = "_{}"]
134135
pub struct GeneratorSavedLocal {
135-
DEBUG_FORMAT = "_{}",
136136
}
137137
}
138138

compiler/rustc_middle/src/thir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ macro_rules! thir_with_elements {
3535
$(
3636
newtype_index! {
3737
#[derive(HashStable)]
38+
#[debug_format = $format]
3839
pub struct $id {
39-
DEBUG_FORMAT = $format
4040
}
4141
}
4242
)*

compiler/rustc_middle/src/ty/structural_impls.rs

-6
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,6 @@ impl<'tcx> fmt::Debug for ty::ConstVid<'tcx> {
9999
}
100100
}
101101

102-
impl fmt::Debug for ty::RegionVid {
103-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
104-
write!(f, "'_#{}r", self.index())
105-
}
106-
}
107-
108102
impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> {
109103
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110104
with_no_trimmed_paths!(fmt::Display::fmt(self, f))

compiler/rustc_middle/src/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1378,8 +1378,8 @@ pub struct ConstVid<'tcx> {
13781378
rustc_index::newtype_index! {
13791379
/// A **region** (lifetime) **v**ariable **ID**.
13801380
#[derive(HashStable)]
1381+
#[debug_format = "'_#{}r"]
13811382
pub struct RegionVid {
1382-
DEBUG_FORMAT = custom,
13831383
}
13841384
}
13851385

compiler/rustc_middle/src/ty/typeck_results.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,8 @@ impl<'a, V> LocalTableInContextMut<'a, V> {
609609

610610
rustc_index::newtype_index! {
611611
#[derive(HashStable)]
612+
#[debug_format = "UserType({})"]
612613
pub struct UserTypeAnnotationIndex {
613-
DEBUG_FORMAT = "UserType({})",
614614
const START_INDEX = 0,
615615
}
616616
}

0 commit comments

Comments
 (0)