Skip to content

Commit e62bc63

Browse files
committed
Stop using rustc_layout_scalar_valid_range_* in rustc
1 parent 8387095 commit e62bc63

File tree

15 files changed

+107
-34
lines changed

15 files changed

+107
-34
lines changed

‎compiler/rustc_abi/src/layout/ty.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ rustc_index::newtype_index! {
3333
/// `b` is `FieldIdx(1)` in `VariantIdx(0)`,
3434
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
3535
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
36-
#[derive(HashStable_Generic)]
36+
#[stable_hash_generic]
3737
#[encodable]
3838
#[orderable]
3939
pub struct FieldIdx {}
@@ -57,7 +57,7 @@ rustc_index::newtype_index! {
5757
///
5858
/// `struct`s, `tuples`, and `unions`s are considered to have a single variant
5959
/// with variant index zero, aka [`FIRST_VARIANT`].
60-
#[derive(HashStable_Generic)]
60+
#[stable_hash_generic]
6161
#[encodable]
6262
#[orderable]
6363
pub struct VariantIdx {

‎compiler/rustc_data_structures/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#![feature(min_specialization)]
2929
#![feature(negative_impls)]
3030
#![feature(never_type)]
31+
#![feature(pattern_type_macro)]
32+
#![feature(pattern_types)]
3133
#![feature(ptr_alignment_type)]
3234
#![feature(rustc_attrs)]
3335
#![feature(sized_hierarchy)]

‎compiler/rustc_hir_id/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ rustc_index::newtype_index! {
151151
/// integers starting at zero, so a mapping that maps all or most nodes within
152152
/// an "item-like" to something else can be implemented by a `Vec` instead of a
153153
/// tree or hash map.
154-
#[derive(HashStable_Generic)]
154+
#[stable_hash_generic]
155155
#[encodable]
156156
#[orderable]
157157
pub struct ItemLocalId {}

‎compiler/rustc_index_macros/src/lib.rs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@ mod newtype;
3434
/// optimizations. The default max value is 0xFFFF_FF00.
3535
/// - `#[gate_rustc_only]`: makes parts of the generated code nightly-only.
3636
#[proc_macro]
37-
#[cfg_attr(feature = "nightly", allow_internal_unstable(step_trait, rustc_attrs, trusted_step))]
37+
#[cfg_attr(
38+
feature = "nightly",
39+
allow_internal_unstable(
40+
step_trait,
41+
rustc_attrs,
42+
trusted_step,
43+
pattern_types,
44+
pattern_type_macro,
45+
structural_match,
46+
)
47+
)]
3848
pub fn newtype_index(input: TokenStream) -> TokenStream {
3949
newtype::newtype(input)
4050
}

‎compiler/rustc_index_macros/src/newtype.rs‎

Lines changed: 72 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ impl Parse for Newtype {
1818
braced!(body in input);
1919

2020
// Any additional `#[derive]` macro paths to apply
21-
let mut derive_paths: Vec<Path> = Vec::new();
2221
let mut debug_format: Option<Lit> = None;
2322
let mut max = None;
2423
let mut consts = Vec::new();
2524
let mut encodable = false;
2625
let mut ord = false;
26+
let mut stable_hash = false;
27+
let mut stable_hash_generic = false;
28+
let mut stable_hash_no_context = false;
2729
let mut gate_rustc_only = quote! {};
2830
let mut gate_rustc_only_cfg = quote! { all() };
2931

@@ -42,6 +44,18 @@ impl Parse for Newtype {
4244
ord = true;
4345
false
4446
}
47+
"stable_hash" => {
48+
stable_hash = true;
49+
false
50+
}
51+
"stable_hash_generic" => {
52+
stable_hash_generic = true;
53+
false
54+
}
55+
"stable_hash_no_context" => {
56+
stable_hash_no_context = true;
57+
false
58+
}
4559
"max" => {
4660
let Meta::NameValue(MetaNameValue { value: Expr::Lit(lit), .. }) = &attr.meta
4761
else {
@@ -111,12 +125,6 @@ impl Parse for Newtype {
111125
} else {
112126
quote! {}
113127
};
114-
115-
if ord {
116-
derive_paths.push(parse_quote!(Ord));
117-
derive_paths.push(parse_quote!(PartialOrd));
118-
}
119-
120128
let step = if ord {
121129
quote! {
122130
#gate_rustc_only
@@ -139,6 +147,38 @@ impl Parse for Newtype {
139147
Self::index(start).checked_sub(u).map(Self::from_usize)
140148
}
141149
}
150+
impl ::std::cmp::Ord for #name {
151+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
152+
self.as_u32().cmp(&other.as_u32())
153+
}
154+
}
155+
impl ::std::cmp::PartialOrd for #name {
156+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
157+
self.as_u32().partial_cmp(&other.as_u32())
158+
}
159+
}
160+
}
161+
} else {
162+
quote! {}
163+
};
164+
165+
let hash_stable = if stable_hash {
166+
quote! {
167+
#gate_rustc_only
168+
impl<'__ctx> ::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>> for #name {
169+
fn hash_stable(&self, hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>, hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
170+
self.as_u32().hash_stable(hcx, hasher)
171+
}
172+
}
173+
}
174+
} else if stable_hash_generic || stable_hash_no_context {
175+
quote! {
176+
#gate_rustc_only
177+
impl<CTX> ::rustc_data_structures::stable_hasher::HashStable<CTX> for #name {
178+
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
179+
self.as_u32().hash_stable(hcx, hasher)
180+
}
181+
}
142182
}
143183
} else {
144184
quote! {}
@@ -154,11 +194,13 @@ impl Parse for Newtype {
154194

155195
Ok(Self(quote! {
156196
#(#attrs)*
157-
#[derive(Clone, Copy, PartialEq, Eq, Hash, #(#derive_paths),*)]
158-
#[cfg_attr(#gate_rustc_only_cfg, rustc_layout_scalar_valid_range_end(#max))]
197+
#[derive(Clone, Copy)]
159198
#[cfg_attr(#gate_rustc_only_cfg, rustc_pass_by_value)]
160199
#vis struct #name {
200+
#[cfg(not(#gate_rustc_only_cfg))]
161201
private_use_as_methods_instead: u32,
202+
#[cfg(#gate_rustc_only_cfg)]
203+
private_use_as_methods_instead: pattern_type!(u32 is 0..=#max),
162204
}
163205

164206
#(#consts)*
@@ -226,7 +268,7 @@ impl Parse for Newtype {
226268
/// Prefer using `from_u32`.
227269
#[inline]
228270
#vis const unsafe fn from_u32_unchecked(value: u32) -> Self {
229-
Self { private_use_as_methods_instead: value }
271+
Self { private_use_as_methods_instead: unsafe { std::mem::transmute(value) } }
230272
}
231273

232274
/// Extracts the value of this index as a `usize`.
@@ -238,7 +280,7 @@ impl Parse for Newtype {
238280
/// Extracts the value of this index as a `u32`.
239281
#[inline]
240282
#vis const fn as_u32(self) -> u32 {
241-
self.private_use_as_methods_instead
283+
unsafe { std::mem::transmute(self.private_use_as_methods_instead) }
242284
}
243285

244286
/// Extracts the value of this index as a `usize`.
@@ -278,6 +320,8 @@ impl Parse for Newtype {
278320

279321
#step
280322

323+
#hash_stable
324+
281325
impl From<#name> for u32 {
282326
#[inline]
283327
fn from(v: #name) -> u32 {
@@ -306,6 +350,23 @@ impl Parse for Newtype {
306350
}
307351
}
308352

353+
impl ::std::cmp::Eq for #name {}
354+
355+
impl ::std::cmp::PartialEq for #name {
356+
fn eq(&self, other: &Self) -> bool {
357+
self.as_u32().eq(&other.as_u32())
358+
}
359+
}
360+
361+
#gate_rustc_only
362+
impl ::std::marker::StructuralPartialEq for #name {}
363+
364+
impl ::std::hash::Hash for #name {
365+
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
366+
self.as_u32().hash(state)
367+
}
368+
}
369+
309370
#encodable_impls
310371
#debug_impl
311372
}))

‎compiler/rustc_middle/src/middle/region.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ rustc_index::newtype_index! {
159159
///
160160
/// * The subscope with `first_statement_index == 1` is scope of `c`,
161161
/// and thus does not include EXPR_2, but covers the `...`.
162-
#[derive(HashStable)]
162+
#[stable_hash]
163163
#[encodable]
164164
#[orderable]
165165
pub struct FirstStatementIndex {}

‎compiler/rustc_middle/src/mir/coverage.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_span::Span;
1010
rustc_index::newtype_index! {
1111
/// Used by [`CoverageKind::BlockMarker`] to mark blocks during THIR-to-MIR
1212
/// lowering, so that those blocks can be identified later.
13-
#[derive(HashStable)]
13+
#[stable_hash]
1414
#[encodable]
1515
#[debug_format = "BlockMarkerId({})"]
1616
pub struct BlockMarkerId {}
@@ -26,7 +26,7 @@ rustc_index::newtype_index! {
2626
///
2727
/// Note that LLVM handles counter IDs as `uint32_t`, so there is no need
2828
/// to use a larger representation on the Rust side.
29-
#[derive(HashStable)]
29+
#[stable_hash]
3030
#[encodable]
3131
#[orderable]
3232
#[debug_format = "CounterId({})"]
@@ -43,7 +43,7 @@ rustc_index::newtype_index! {
4343
///
4444
/// Note that LLVM handles expression IDs as `uint32_t`, so there is no need
4545
/// to use a larger representation on the Rust side.
46-
#[derive(HashStable)]
46+
#[stable_hash]
4747
#[encodable]
4848
#[orderable]
4949
#[debug_format = "ExpressionId({})"]
@@ -203,7 +203,7 @@ rustc_index::newtype_index! {
203203
///
204204
/// After that pass is complete, the coverage graph no longer exists, so a
205205
/// BCB is effectively an opaque ID.
206-
#[derive(HashStable)]
206+
#[stable_hash]
207207
#[encodable]
208208
#[orderable]
209209
#[debug_format = "bcb{}"]

‎compiler/rustc_middle/src/mir/mod.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ impl SourceInfo {
840840
// Variables and temps
841841

842842
rustc_index::newtype_index! {
843-
#[derive(HashStable)]
843+
#[stable_hash]
844844
#[encodable]
845845
#[orderable]
846846
#[debug_format = "_{}"]
@@ -1283,7 +1283,7 @@ rustc_index::newtype_index! {
12831283
/// https://rustc-dev-guide.rust-lang.org/appendix/background.html#what-is-a-dataflow-analysis
12841284
/// [`CriticalCallEdges`]: ../../rustc_mir_transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges
12851285
/// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/
1286-
#[derive(HashStable)]
1286+
#[stable_hash]
12871287
#[encodable]
12881288
#[orderable]
12891289
#[debug_format = "bb{}"]
@@ -1417,7 +1417,7 @@ impl<'tcx> BasicBlockData<'tcx> {
14171417
// Scopes
14181418

14191419
rustc_index::newtype_index! {
1420-
#[derive(HashStable)]
1420+
#[stable_hash]
14211421
#[encodable]
14221422
#[debug_format = "scope[{}]"]
14231423
pub struct SourceScope {
@@ -1556,7 +1556,7 @@ pub struct UserTypeProjection {
15561556
}
15571557

15581558
rustc_index::newtype_index! {
1559-
#[derive(HashStable)]
1559+
#[stable_hash]
15601560
#[encodable]
15611561
#[orderable]
15621562
#[debug_format = "promoted[{}]"]

‎compiler/rustc_middle/src/mir/query.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::{ConstValue, SourceInfo};
1313
use crate::ty::{self, CoroutineArgsExt, Ty};
1414

1515
rustc_index::newtype_index! {
16-
#[derive(HashStable)]
16+
#[stable_hash]
1717
#[encodable]
1818
#[debug_format = "_s{}"]
1919
pub struct CoroutineSavedLocal {}

‎compiler/rustc_middle/src/thir.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ macro_rules! thir_with_elements {
4444
) => {
4545
$(
4646
newtype_index! {
47-
#[derive(HashStable)]
47+
#[stable_hash]
4848
#[debug_format = $format]
4949
pub struct $id {}
5050
}

0 commit comments

Comments
 (0)