From 91ad3f9c4aa5aad5a26274220350dc90e0fca439 Mon Sep 17 00:00:00 2001 From: aerooneqq Date: Wed, 21 Oct 2026 18:30:26 +0300 Subject: [PATCH] Add `#[default]` for `newtype_index!` --- compiler/rustc_index_macros/src/lib.rs | 1 + compiler/rustc_index_macros/src/newtype.rs | 20 ++++++++++++++++++++ compiler/rustc_span/src/hygiene.rs | 7 +------ compiler/rustc_type_ir/src/lib.rs | 7 +------ 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_index_macros/src/lib.rs b/compiler/rustc_index_macros/src/lib.rs index 27fc4680ea289..b0bc40a9d8b1a 100644 --- a/compiler/rustc_index_macros/src/lib.rs +++ b/compiler/rustc_index_macros/src/lib.rs @@ -26,6 +26,7 @@ mod newtype; /// /// Accepted attributes for customization: /// - `#[stable_hash]`: derives `StableHash`. +/// - `#[default]`: derives `std::default::Default` returning `Self::ZERO`. /// - `#[encodable]`: derives `Encodable`/`Decodable`. /// - `#[orderable]`: derives `PartialOrd`/`Ord`, plus step-related methods. /// - `#[debug_format = "Foo({})"]`: derives `Debug` with particular output. diff --git a/compiler/rustc_index_macros/src/newtype.rs b/compiler/rustc_index_macros/src/newtype.rs index aaa025bd7cbdb..52f1d070718e8 100644 --- a/compiler/rustc_index_macros/src/newtype.rs +++ b/compiler/rustc_index_macros/src/newtype.rs @@ -24,6 +24,7 @@ impl Parse for Newtype { let mut encodable = false; let mut ord = false; let mut stable_hash = false; + let mut default = false; let mut gate_rustc_only = quote! {}; let mut gate_rustc_only_cfg = quote! { all() }; @@ -46,6 +47,10 @@ impl Parse for Newtype { stable_hash = true; false } + "default" => { + default = true; + false + } "max" => { let Meta::NameValue(MetaNameValue { value: Expr::Lit(lit), .. }) = &attr.meta else { @@ -185,6 +190,19 @@ impl Parse for Newtype { quote! {} }; + let default_impl = if default { + quote! { + impl std::default::Default for #name { + #[inline] + fn default() -> Self { + Self::ZERO + } + } + } + } else { + quote! {} + }; + let debug_impl = quote! { impl ::std::fmt::Debug for #name { fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { @@ -323,6 +341,8 @@ impl Parse for Newtype { #stable_hash_impl + #default_impl + impl From<#name> for u32 { #[inline] fn from(v: #name) -> u32 { diff --git a/compiler/rustc_span/src/hygiene.rs b/compiler/rustc_span/src/hygiene.rs index 4745a26ade953..c107f6ce4f64c 100644 --- a/compiler/rustc_span/src/hygiene.rs +++ b/compiler/rustc_span/src/hygiene.rs @@ -134,15 +134,10 @@ impl fmt::Debug for ExpnId { rustc_index::newtype_index! { /// A unique ID associated with a macro invocation and expansion. #[debug_format = "expn{}"] + #[default] pub struct LocalExpnId {} } -impl Default for LocalExpnId { - fn default() -> LocalExpnId { - LocalExpnId::ROOT - } -} - // To ensure correctness of incremental compilation, // `LocalExpnId` must not implement `Ord` or `PartialOrd`. // See https://github.com/rust-lang/rust/issues/90317. diff --git a/compiler/rustc_type_ir/src/lib.rs b/compiler/rustc_type_ir/src/lib.rs index eab69fa224d92..a1c5bcacc4b5f 100644 --- a/compiler/rustc_type_ir/src/lib.rs +++ b/compiler/rustc_type_ir/src/lib.rs @@ -375,6 +375,7 @@ rustc_index::newtype_index! { /// type -- an idealized representative of "types in general" that we /// use for checking generic functions. #[stable_hash] + #[default] #[encodable] #[orderable] #[debug_format = "U{}"] @@ -422,12 +423,6 @@ impl UniverseIndex { } } -impl Default for UniverseIndex { - fn default() -> Self { - Self::ROOT - } -} - rustc_index::newtype_index! { #[stable_hash] #[encodable]