Skip to content

Commit 91c3c20

Browse files
committed
Make #[max] an attribute in newtype_index
1 parent 9342994 commit 91c3c20

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

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_macros/src/newtype.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ impl Parse for Newtype {
5959
ord = false;
6060
false
6161
}
62+
"max" => {
63+
let Ok(Meta::NameValue(literal) )= attr.parse_meta() else {
64+
panic!("#[max = NUMBER] attribute requires max value");
65+
};
66+
67+
if let Some(old) = max.replace(literal.lit) {
68+
panic!("Specified multiple MAX: {:?}", old);
69+
}
70+
71+
false
72+
}
6273
_ => true,
6374
},
6475
_ => true,
@@ -84,16 +95,6 @@ impl Parse for Newtype {
8495
}
8596
continue;
8697
}
87-
if body.lookahead1().peek(kw::MAX) {
88-
body.parse::<kw::MAX>()?;
89-
body.parse::<Token![=]>()?;
90-
let val: Lit = body.parse()?;
91-
try_comma()?;
92-
if let Some(old) = max.replace(val) {
93-
panic!("Specified multiple MAX: {:?}", old);
94-
}
95-
continue;
96-
}
9798

9899
// We've parsed everything that the user provided, so we're done
99100
if body.is_empty() {

compiler/rustc_middle/src/mir/coverage.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ rustc_index::newtype_index! {
1111
/// (which _*descend*_ from u32::MAX). Id value `0` (zero) represents a virtual counter with a
1212
/// constant value of `0`.
1313
#[derive(HashStable)]
14+
#[max = 0xFFFF_FFFF]
1415
pub struct ExpressionOperandId {
1516
DEBUG_FORMAT = "ExpressionOperandId({})",
16-
MAX = 0xFFFF_FFFF,
1717
}
1818
}
1919

@@ -33,9 +33,9 @@ impl ExpressionOperandId {
3333

3434
rustc_index::newtype_index! {
3535
#[derive(HashStable)]
36+
#[max = 0xFFFF_FFFF]
3637
pub struct CounterValueReference {
3738
DEBUG_FORMAT = "CounterValueReference({})",
38-
MAX = 0xFFFF_FFFF,
3939
}
4040
}
4141

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

@@ -68,9 +68,9 @@ rustc_index::newtype_index! {
6868
///
6969
/// Values ascend from 0.
7070
#[derive(HashStable)]
71+
#[max = 0xFFFF_FFFF]
7172
pub struct InjectedExpressionIndex {
7273
DEBUG_FORMAT = "InjectedExpressionIndex({})",
73-
MAX = 0xFFFF_FFFF,
7474
}
7575
}
7676

@@ -79,9 +79,9 @@ rustc_index::newtype_index! {
7979
/// array position in the LLVM coverage map "Expressions" array, which is assembled during the
8080
/// "mapgen" process. They cannot be computed algorithmically, from the other `newtype_index`s.
8181
#[derive(HashStable)]
82+
#[max = 0xFFFF_FFFF]
8283
pub struct MappedExpressionIndex {
8384
DEBUG_FORMAT = "MappedExpressionIndex({})",
84-
MAX = 0xFFFF_FFFF,
8585
}
8686
}
8787

compiler/rustc_query_system/src/dep_graph/serialized.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use smallvec::SmallVec;
2727
// unused so that we can store multiple index types in `CompressedHybridIndex`,
2828
// and use those bits to encode which index type it contains.
2929
rustc_index::newtype_index! {
30+
#[max = 0x7FFF_FFFF]
3031
pub struct SerializedDepNodeIndex {
31-
MAX = 0x7FFF_FFFF
3232
}
3333
}
3434

0 commit comments

Comments
 (0)