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
8 changes: 8 additions & 0 deletions compiler/rustc_builtin_macros/src/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ fn default_struct_substructure(
variant_data: &VariantData,
type_ident: Ident,
) -> BlockOrExpr {
// FIXME: This is currently needed to avoid a regression with attribute macros after
// derives that incorrectly rely on the generated code using `Unit {}` instead of `Unit`.
// See https://github.com/rust-lang/rust/issues/164078.
if variant_data.fields().is_empty() {
return BlockOrExpr::new_expr(cx.expr_struct_ident(trait_span, type_ident, ThinVec::new()));
}

let expr = match variant_data {
// This is currently unreachable due to the above hack.
VariantData::Unit(_) => cx.expr_ident(trait_span, type_ident),
VariantData::Tuple(fields, _) => {
let exprs = fields
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/derives/attr-macro-changes-type-kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
extern crate struct_to_enum;
use struct_to_enum::*;

#[derive(Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]

@cyrgani cyrgani Oct 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also interesting: the Default derive produces an error on stable (expected struct, variant or union type, found enum Spooky), on beta (the Self constructor can only be used with tuple or unit structs), but #163863 caused it to compile. After this PR, it will fail again as on stable. Is this okay?

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't make any "this code now starts compiling" changes without going through some kind of process, so this is a bug in its own right.

Can you double check none such things slipped through in your other builtin macro refactorings? I remember you writing a series of prs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching stable sounds reasonable to me.

#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
#[struct_to_enum::phantom]
struct Spooky<T>;

Expand Down
47 changes: 47 additions & 0 deletions tests/ui/derives/default/attr-macros-fieldless-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! Regression test for https://github.com/rust-lang/rust/issues/164078,
//! where some attribute macros relied on the generated code always being
//! `Unit {}` and not `Unit()` or `Unit`.
//@ check-pass
//@ proc-macro: rewrite-fieldless-type-kind.rs

extern crate rewrite_fieldless_type_kind;
use rewrite_fieldless_type_kind::*;

#[derive(Default)]
#[to_unit]
struct UnitToUnit;

#[derive(Default)]
#[to_tuple]
struct UnitToTuple;

#[derive(Default)]
#[to_braced]
struct UnitToBraced;

#[derive(Default)]
#[to_unit]
struct TupleToUnit();

#[derive(Default)]
#[to_tuple]
struct TupleToTuple();

#[derive(Default)]
#[to_braced]
struct TupleToBraced();

#[derive(Default)]
#[to_unit]
struct BracedToUnit {}

#[derive(Default)]
#[to_tuple]
struct BracedToTuple {}

#[derive(Default)]
#[to_braced]
struct BracedToBraced {}

fn main() {}
20 changes: 20 additions & 0 deletions tests/ui/derives/default/auxiliary/rewrite-fieldless-type-kind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn to_unit(_: TokenStream, struct_item: TokenStream) -> TokenStream {
rewrite(struct_item, ";")
}
#[proc_macro_attribute]
pub fn to_tuple(_: TokenStream, struct_item: TokenStream) -> TokenStream {
rewrite(struct_item, "();")
}
#[proc_macro_attribute]
pub fn to_braced(_: TokenStream, struct_item: TokenStream) -> TokenStream {
rewrite(struct_item, "{}")
}

fn rewrite(struct_item: TokenStream, suffix: &str) -> TokenStream {
let ident = struct_item.into_iter().nth(1).unwrap();
format!("struct {ident}{suffix}").parse().unwrap()
}
2 changes: 1 addition & 1 deletion tests/ui/derives/deriving-all-codegen.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl ::core::fmt::Debug for Empty {
#[automatically_derived]
impl ::core::default::Default for Empty {
#[inline]
fn default() -> Self { Empty }
fn default() -> Self { Empty {} }
}
#[automatically_derived]
impl ::core::hash::Hash for Empty {
Expand Down
Loading