Rollup merge of #152569 - oli-obk:rustc_layout_scalar_valid_range_end_end, r=davidtwco

Stop using rustc_layout_scalar_valid_range_* in rustc

Another step towards rust-lang/rust#135996

Required some manual impls, but we already do many manual impls for the newtype_index types, so it's not really a new maintenance burden.
This commit is contained in:
Jonathan Brouwer 2026-02-18 18:55:13 +01:00 committed by GitHub
commit 9dfffd6f37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 107 additions and 34 deletions

View file

@ -33,7 +33,7 @@ rustc_index::newtype_index! {
/// `b` is `FieldIdx(1)` in `VariantIdx(0)`,
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
#[derive(HashStable_Generic)]
#[stable_hash_generic]
#[encodable]
#[orderable]
pub struct FieldIdx {}
@ -57,7 +57,7 @@ rustc_index::newtype_index! {
///
/// `struct`s, `tuples`, and `unions`s are considered to have a single variant
/// with variant index zero, aka [`FIRST_VARIANT`].
#[derive(HashStable_Generic)]
#[stable_hash_generic]
#[encodable]
#[orderable]
pub struct VariantIdx {

View file

@ -28,6 +28,8 @@
#![feature(min_specialization)]
#![feature(negative_impls)]
#![feature(never_type)]
#![feature(pattern_type_macro)]
#![feature(pattern_types)]
#![feature(ptr_alignment_type)]
#![feature(rustc_attrs)]
#![feature(sized_hierarchy)]

View file

@ -151,7 +151,7 @@ rustc_index::newtype_index! {
/// integers starting at zero, so a mapping that maps all or most nodes within
/// an "item-like" to something else can be implemented by a `Vec` instead of a
/// tree or hash map.
#[derive(HashStable_Generic)]
#[stable_hash_generic]
#[encodable]
#[orderable]
pub struct ItemLocalId {}

View file

@ -34,7 +34,17 @@ mod newtype;
/// optimizations. The default max value is 0xFFFF_FF00.
/// - `#[gate_rustc_only]`: makes parts of the generated code nightly-only.
#[proc_macro]
#[cfg_attr(feature = "nightly", allow_internal_unstable(step_trait, rustc_attrs, trusted_step))]
#[cfg_attr(
feature = "nightly",
allow_internal_unstable(
step_trait,
rustc_attrs,
trusted_step,
pattern_types,
pattern_type_macro,
structural_match,
)
)]
pub fn newtype_index(input: TokenStream) -> TokenStream {
newtype::newtype(input)
}

View file

@ -18,12 +18,14 @@ impl Parse for Newtype {
braced!(body in input);
// Any additional `#[derive]` macro paths to apply
let mut derive_paths: Vec<Path> = Vec::new();
let mut debug_format: Option<Lit> = None;
let mut max = None;
let mut consts = Vec::new();
let mut encodable = false;
let mut ord = false;
let mut stable_hash = false;
let mut stable_hash_generic = false;
let mut stable_hash_no_context = false;
let mut gate_rustc_only = quote! {};
let mut gate_rustc_only_cfg = quote! { all() };
@ -42,6 +44,18 @@ impl Parse for Newtype {
ord = true;
false
}
"stable_hash" => {
stable_hash = true;
false
}
"stable_hash_generic" => {
stable_hash_generic = true;
false
}
"stable_hash_no_context" => {
stable_hash_no_context = true;
false
}
"max" => {
let Meta::NameValue(MetaNameValue { value: Expr::Lit(lit), .. }) = &attr.meta
else {
@ -111,12 +125,6 @@ impl Parse for Newtype {
} else {
quote! {}
};
if ord {
derive_paths.push(parse_quote!(Ord));
derive_paths.push(parse_quote!(PartialOrd));
}
let step = if ord {
quote! {
#gate_rustc_only
@ -139,6 +147,38 @@ impl Parse for Newtype {
Self::index(start).checked_sub(u).map(Self::from_usize)
}
}
impl ::std::cmp::Ord for #name {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.as_u32().cmp(&other.as_u32())
}
}
impl ::std::cmp::PartialOrd for #name {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.as_u32().partial_cmp(&other.as_u32())
}
}
}
} else {
quote! {}
};
let hash_stable = if stable_hash {
quote! {
#gate_rustc_only
impl<'__ctx> ::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>> for #name {
fn hash_stable(&self, hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>, hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
self.as_u32().hash_stable(hcx, hasher)
}
}
}
} else if stable_hash_generic || stable_hash_no_context {
quote! {
#gate_rustc_only
impl<CTX> ::rustc_data_structures::stable_hasher::HashStable<CTX> for #name {
fn hash_stable(&self, hcx: &mut CTX, hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
self.as_u32().hash_stable(hcx, hasher)
}
}
}
} else {
quote! {}
@ -154,11 +194,13 @@ impl Parse for Newtype {
Ok(Self(quote! {
#(#attrs)*
#[derive(Clone, Copy, PartialEq, Eq, Hash, #(#derive_paths),*)]
#[cfg_attr(#gate_rustc_only_cfg, rustc_layout_scalar_valid_range_end(#max))]
#[derive(Clone, Copy)]
#[cfg_attr(#gate_rustc_only_cfg, rustc_pass_by_value)]
#vis struct #name {
#[cfg(not(#gate_rustc_only_cfg))]
private_use_as_methods_instead: u32,
#[cfg(#gate_rustc_only_cfg)]
private_use_as_methods_instead: pattern_type!(u32 is 0..=#max),
}
#(#consts)*
@ -226,7 +268,7 @@ impl Parse for Newtype {
/// Prefer using `from_u32`.
#[inline]
#vis const unsafe fn from_u32_unchecked(value: u32) -> Self {
Self { private_use_as_methods_instead: value }
Self { private_use_as_methods_instead: unsafe { std::mem::transmute(value) } }
}
/// Extracts the value of this index as a `usize`.
@ -238,7 +280,7 @@ impl Parse for Newtype {
/// Extracts the value of this index as a `u32`.
#[inline]
#vis const fn as_u32(self) -> u32 {
self.private_use_as_methods_instead
unsafe { std::mem::transmute(self.private_use_as_methods_instead) }
}
/// Extracts the value of this index as a `usize`.
@ -278,6 +320,8 @@ impl Parse for Newtype {
#step
#hash_stable
impl From<#name> for u32 {
#[inline]
fn from(v: #name) -> u32 {
@ -306,6 +350,23 @@ impl Parse for Newtype {
}
}
impl ::std::cmp::Eq for #name {}
impl ::std::cmp::PartialEq for #name {
fn eq(&self, other: &Self) -> bool {
self.as_u32().eq(&other.as_u32())
}
}
#gate_rustc_only
impl ::std::marker::StructuralPartialEq for #name {}
impl ::std::hash::Hash for #name {
fn hash<H: ::std::hash::Hasher>(&self, state: &mut H) {
self.as_u32().hash(state)
}
}
#encodable_impls
#debug_impl
}))

View file

@ -159,7 +159,7 @@ rustc_index::newtype_index! {
///
/// * The subscope with `first_statement_index == 1` is scope of `c`,
/// and thus does not include EXPR_2, but covers the `...`.
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[orderable]
pub struct FirstStatementIndex {}

View file

@ -10,7 +10,7 @@ use rustc_span::Span;
rustc_index::newtype_index! {
/// Used by [`CoverageKind::BlockMarker`] to mark blocks during THIR-to-MIR
/// lowering, so that those blocks can be identified later.
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[debug_format = "BlockMarkerId({})"]
pub struct BlockMarkerId {}
@ -26,7 +26,7 @@ rustc_index::newtype_index! {
///
/// Note that LLVM handles counter IDs as `uint32_t`, so there is no need
/// to use a larger representation on the Rust side.
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[orderable]
#[debug_format = "CounterId({})"]
@ -43,7 +43,7 @@ rustc_index::newtype_index! {
///
/// Note that LLVM handles expression IDs as `uint32_t`, so there is no need
/// to use a larger representation on the Rust side.
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[orderable]
#[debug_format = "ExpressionId({})"]
@ -203,7 +203,7 @@ rustc_index::newtype_index! {
///
/// After that pass is complete, the coverage graph no longer exists, so a
/// BCB is effectively an opaque ID.
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[orderable]
#[debug_format = "bcb{}"]

View file

@ -840,7 +840,7 @@ impl SourceInfo {
// Variables and temps
rustc_index::newtype_index! {
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[orderable]
#[debug_format = "_{}"]
@ -1283,7 +1283,7 @@ rustc_index::newtype_index! {
/// https://rustc-dev-guide.rust-lang.org/appendix/background.html#what-is-a-dataflow-analysis
/// [`CriticalCallEdges`]: ../../rustc_mir_transform/add_call_guards/enum.AddCallGuards.html#variant.CriticalCallEdges
/// [guide-mir]: https://rustc-dev-guide.rust-lang.org/mir/
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[orderable]
#[debug_format = "bb{}"]
@ -1417,7 +1417,7 @@ impl<'tcx> BasicBlockData<'tcx> {
// Scopes
rustc_index::newtype_index! {
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[debug_format = "scope[{}]"]
pub struct SourceScope {
@ -1556,7 +1556,7 @@ pub struct UserTypeProjection {
}
rustc_index::newtype_index! {
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[orderable]
#[debug_format = "promoted[{}]"]

View file

@ -13,7 +13,7 @@ use super::{ConstValue, SourceInfo};
use crate::ty::{self, CoroutineArgsExt, Ty};
rustc_index::newtype_index! {
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[debug_format = "_s{}"]
pub struct CoroutineSavedLocal {}

View file

@ -44,7 +44,7 @@ macro_rules! thir_with_elements {
) => {
$(
newtype_index! {
#[derive(HashStable)]
#[stable_hash]
#[debug_format = $format]
pub struct $id {}
}

View file

@ -716,7 +716,7 @@ impl<'a> LocalSetInContextMut<'a> {
}
rustc_index::newtype_index! {
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[debug_format = "UserType({})"]
pub struct UserTypeAnnotationIndex {

View file

@ -161,7 +161,7 @@ struct TOFinder<'a, 'tcx> {
}
rustc_index::newtype_index! {
#[derive(Ord, PartialOrd)]
#[orderable]
#[debug_format = "_c{}"]
struct ConditionIndex {}
}

View file

@ -30,7 +30,7 @@ impl From<usize> for StaticNodeIdx {
}
newtype_index! {
#[derive(Ord, PartialOrd)]
#[orderable]
struct StaticSccIdx {}
}

View file

@ -122,7 +122,7 @@ rustc_index::newtype_index! {
/// is the outer fn.
///
/// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
#[stable_hash_no_context]
#[encodable]
#[orderable]
#[debug_format = "DebruijnIndex({})"]
@ -332,7 +332,7 @@ rustc_index::newtype_index! {
/// declared, but a type name in a non-zero universe is a placeholder
/// type -- an idealized representative of "types in general" that we
/// use for checking generic functions.
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
#[stable_hash_no_context]
#[encodable]
#[orderable]
#[debug_format = "U{}"]
@ -387,7 +387,7 @@ impl Default for UniverseIndex {
}
rustc_index::newtype_index! {
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
#[stable_hash_generic]
#[encodable]
#[orderable]
#[debug_format = "{}"]

View file

@ -4,7 +4,7 @@ use derive_where::derive_where;
#[cfg(feature = "nightly")]
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
#[cfg(feature = "nightly")]
use rustc_macros::{Decodable_NoContext, Encodable_NoContext, HashStable_NoContext};
use rustc_macros::{Decodable_NoContext, Encodable_NoContext};
use rustc_type_ir_macros::GenericTypeVisitable;
use self::RegionKind::*;
@ -16,7 +16,7 @@ rustc_index::newtype_index! {
#[orderable]
#[debug_format = "'?{}"]
#[gate_rustc_only]
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
#[stable_hash_no_context]
pub struct RegionVid {}
}