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

Stop using rustc_layout_scalar_valid_range_* in rustc

*[View all comments](https://triagebot.infra.rust-lang.org/gh-comments/rust-lang/rust/pull/152569)*

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-03-11 22:05:41 +01:00
committed by GitHub
15 changed files with 107 additions and 34 deletions
+2 -2
View File
@@ -45,7 +45,7 @@
/// `b` is `FieldIdx(1)` in `VariantIdx(0)`,
/// `d` is `FieldIdx(1)` in `VariantIdx(1)`, and
/// `f` is `FieldIdx(1)` in `VariantIdx(0)`.
#[cfg_attr(feature = "nightly", derive(rustc_macros::HashStable_Generic))]
#[stable_hash_generic]
#[encodable]
#[orderable]
#[gate_rustc_only]
@@ -70,7 +70,7 @@ impl FieldIdx {
///
/// `struct`s, `tuples`, and `unions`s are considered to have a single variant
/// with variant index zero, aka [`FIRST_VARIANT`].
#[cfg_attr(feature = "nightly", derive(rustc_macros::HashStable_Generic))]
#[stable_hash_generic]
#[encodable]
#[orderable]
#[gate_rustc_only]
@@ -25,6 +25,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)]
+1 -1
View File
@@ -151,7 +151,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// 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 {}
+11 -1
View File
@@ -34,7 +34,17 @@
/// 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)
}
+72 -11
View File
@@ -18,12 +18,14 @@ fn parse(input: ParseStream<'_>) -> Result<Self> {
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 @@ fn parse(input: ParseStream<'_>) -> Result<Self> {
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 @@ fn encode(&self, e: &mut E) {
} 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 @@ fn backward_checked(start: Self, u: usize) -> Option<Self> {
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 @@ fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
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 #name {
/// 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 #name {
/// 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 @@ fn index(self) -> usize {
#step
#hash_stable
impl From<#name> for u32 {
#[inline]
fn from(v: #name) -> u32 {
@@ -306,6 +350,23 @@ fn from(value: u32) -> Self {
}
}
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
}))
+1 -1
View File
@@ -159,7 +159,7 @@ pub enum ScopeData {
///
/// * 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 {}
+4 -4
View File
@@ -10,7 +10,7 @@
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 @@ pub struct BlockMarkerId {}
///
/// 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 @@ pub struct CounterId {}
///
/// 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 @@ pub struct CoverageIdsInfo {
///
/// 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{}"]
+4 -4
View File
@@ -839,7 +839,7 @@ pub fn outermost(span: Span) -> Self {
// Variables and temps
rustc_index::newtype_index! {
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[orderable]
#[debug_format = "_{}"]
@@ -1263,7 +1263,7 @@ pub struct VarDebugInfo<'tcx> {
/// 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{}"]
@@ -1397,7 +1397,7 @@ pub fn drop_debuginfo(&mut self) {
// Scopes
rustc_index::newtype_index! {
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[debug_format = "scope[{}]"]
pub struct SourceScope {
@@ -1536,7 +1536,7 @@ pub struct UserTypeProjection {
}
rustc_index::newtype_index! {
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[orderable]
#[debug_format = "promoted[{}]"]
+1 -1
View File
@@ -13,7 +13,7 @@
use crate::ty::{self, CoroutineArgsExt, Ty};
rustc_index::newtype_index! {
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[debug_format = "_s{}"]
pub struct CoroutineSavedLocal {}
+1 -1
View File
@@ -44,7 +44,7 @@ macro_rules! thir_with_elements {
) => {
$(
newtype_index! {
#[derive(HashStable)]
#[stable_hash]
#[debug_format = $format]
pub struct $id {}
}
@@ -716,7 +716,7 @@ pub fn remove(&mut self, id: hir::HirId) -> bool {
}
rustc_index::newtype_index! {
#[derive(HashStable)]
#[stable_hash]
#[encodable]
#[debug_format = "UserType({})"]
pub struct UserTypeAnnotationIndex {
@@ -161,7 +161,7 @@ struct TOFinder<'a, 'tcx> {
}
rustc_index::newtype_index! {
#[derive(Ord, PartialOrd)]
#[orderable]
#[debug_format = "_c{}"]
struct ConditionIndex {}
}
@@ -30,7 +30,7 @@ fn from(value: usize) -> Self {
}
newtype_index! {
#[derive(Ord, PartialOrd)]
#[orderable]
struct StaticSccIdx {}
}
+3 -3
View File
@@ -123,7 +123,7 @@
/// 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({})"]
@@ -333,7 +333,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// 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{}"]
@@ -388,7 +388,7 @@ fn default() -> Self {
}
rustc_index::newtype_index! {
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
#[stable_hash_generic]
#[encodable]
#[orderable]
#[debug_format = "{}"]
+2 -2
View File
@@ -4,7 +4,7 @@
#[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 @@
#[orderable]
#[debug_format = "'?{}"]
#[gate_rustc_only]
#[cfg_attr(feature = "nightly", derive(HashStable_NoContext))]
#[stable_hash_no_context]
pub struct RegionVid {}
}