Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_index_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions compiler/rustc_index_macros/src/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() };

Expand 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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -323,6 +341,8 @@ impl Parse for Newtype {

#stable_hash_impl

#default_impl

impl From<#name> for u32 {
#[inline]
fn from(v: #name) -> u32 {
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_type_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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{}"]
Expand Down Expand Up @@ -422,12 +423,6 @@ impl UniverseIndex {
}
}

impl Default for UniverseIndex {
fn default() -> Self {
Self::ROOT
}
}

rustc_index::newtype_index! {
#[stable_hash]
#[encodable]
Expand Down
Loading