Skip to content

Commit 0e758be

Browse files
Rollup merge of #164696 - aerooneqq:default-for-newtype-index, r=petrochenkov
Add `#[default]` for `newtype_index!` Add `#[default]` for `newtype_index!`. We can merge it before or after #164425. r? @petrochenkov
2 parents 935e942 + 91ad3f9 commit 0e758be

4 files changed

Lines changed: 23 additions & 12 deletions

File tree

‎compiler/rustc_index_macros/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod newtype;
2626
///
2727
/// Accepted attributes for customization:
2828
/// - `#[stable_hash]`: derives `StableHash`.
29+
/// - `#[default]`: derives `std::default::Default` returning `Self::ZERO`.
2930
/// - `#[encodable]`: derives `Encodable`/`Decodable`.
3031
/// - `#[orderable]`: derives `PartialOrd`/`Ord`, plus step-related methods.
3132
/// - `#[debug_format = "Foo({})"]`: derives `Debug` with particular output.

‎compiler/rustc_index_macros/src/newtype.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ impl Parse for Newtype {
2424
let mut encodable = false;
2525
let mut ord = false;
2626
let mut stable_hash = false;
27+
let mut default = false;
2728
let mut gate_rustc_only = quote! {};
2829
let mut gate_rustc_only_cfg = quote! { all() };
2930

@@ -46,6 +47,10 @@ impl Parse for Newtype {
4647
stable_hash = true;
4748
false
4849
}
50+
"default" => {
51+
default = true;
52+
false
53+
}
4954
"max" => {
5055
let Meta::NameValue(MetaNameValue { value: Expr::Lit(lit), .. }) = &attr.meta
5156
else {
@@ -185,6 +190,19 @@ impl Parse for Newtype {
185190
quote! {}
186191
};
187192

193+
let default_impl = if default {
194+
quote! {
195+
impl std::default::Default for #name {
196+
#[inline]
197+
fn default() -> Self {
198+
Self::ZERO
199+
}
200+
}
201+
}
202+
} else {
203+
quote! {}
204+
};
205+
188206
let debug_impl = quote! {
189207
impl ::std::fmt::Debug for #name {
190208
fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
@@ -323,6 +341,8 @@ impl Parse for Newtype {
323341

324342
#stable_hash_impl
325343

344+
#default_impl
345+
326346
impl From<#name> for u32 {
327347
#[inline]
328348
fn from(v: #name) -> u32 {

‎compiler/rustc_span/src/hygiene.rs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,10 @@ impl fmt::Debug for ExpnId {
134134
rustc_index::newtype_index! {
135135
/// A unique ID associated with a macro invocation and expansion.
136136
#[debug_format = "expn{}"]
137+
#[default]
137138
pub struct LocalExpnId {}
138139
}
139140

140-
impl Default for LocalExpnId {
141-
fn default() -> LocalExpnId {
142-
LocalExpnId::ROOT
143-
}
144-
}
145-
146141
// To ensure correctness of incremental compilation,
147142
// `LocalExpnId` must not implement `Ord` or `PartialOrd`.
148143
// See https://github.com/rust-lang/rust/issues/90317.

‎compiler/rustc_type_ir/src/lib.rs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ rustc_index::newtype_index! {
375375
/// type -- an idealized representative of "types in general" that we
376376
/// use for checking generic functions.
377377
#[stable_hash]
378+
#[default]
378379
#[encodable]
379380
#[orderable]
380381
#[debug_format = "U{}"]
@@ -422,12 +423,6 @@ impl UniverseIndex {
422423
}
423424
}
424425

425-
impl Default for UniverseIndex {
426-
fn default() -> Self {
427-
Self::ROOT
428-
}
429-
}
430-
431426
rustc_index::newtype_index! {
432427
#[stable_hash]
433428
#[encodable]

0 commit comments

Comments
 (0)