Sitelet https://github.com/starkware-libs/cairo/pull/10228
Skip to content

fix(plugins): reject #[default] on a struct member - #10228

Merged
orizi merged 1 commit into
mainfrom
orizi/07-21-fix_plugins_reject_default_on_a_struct_member
Jul 21, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/07-21-fix_plugins_reject_default_on_a_struct_member

Conversation

@orizi

@orizi orizi commented Jul 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds a diagnostic error when #[default] is used on a struct member during Default derivation. Previously, applying #[default] to a struct field was silently ignored — the derive macro would still generate a valid Default implementation without any indication that the attribute had no effect. Now, a clear error is emitted pointing to the offending attribute.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

#[default] is only meaningful on enum variants when deriving Default. When a user mistakenly places it on a struct member, the attribute was previously silently ignored, giving no feedback that the annotation was invalid or had no effect. This could lead to confusion about what the attribute does.


What was the behavior or documentation before?

Using #[default] on a struct member with #[derive(Default)] produced no diagnostic. The Default implementation was generated as if the attribute were not present.


What is the behavior or documentation after?

Using #[default] on a struct member with #[derive(Default)] now produces a compile error:

error: `#[default]` is only supported for enum variants.

The error points directly to the offending #[default] attribute on the struct member.


Related issue or discussion (if any)

N/A


Additional context

N/A

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jul 21, 2026

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@orizi
orizi marked this pull request as ready for review July 21, 2026 10:15
@cursor

cursor Bot commented Jul 21, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Compile-time diagnostic only in the derive plugin; valid struct Default derivation is unchanged.

Overview
The Default derive for structs now reports a compile error when a field is annotated with #[default], which only applies to enum variants. Before, that attribute was ignored while a normal per-field Default impl was still emitted.

A derive plugin test adds DefaultOnStructMember and expects #[default] is not supported on struct members. at the attribute site. Generated Default code for that case is unchanged aside from the new diagnostic.

Reviewed by Cursor Bugbot for commit 3682588. Bugbot is set up for automated code reviews on this repo. Configure here.

@eytan-starkware eytan-starkware left a comment

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.

@eytan-starkware reviewed 2 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on orizi and TomerStarkware).


crates/cairo-lang-plugins/src/test_data/derive line 867 at r1 (raw file):



error: `#[default]` is only supported for enum variants.

#[default] is not supported on struct members

`derive(Default)`'s enum arm diagnoses `#[default]` (requiring exactly one
default variant), but the struct arm never inspected member attributes. Since
`#[default]` is registered globally, placing it on a struct field neither
tripped the unknown-attribute check nor did anything — it parsed, emitted no
diagnostic, and was silently discarded, producing byte-identical Sierra with or
without the attribute. This contradicts the corelib docs, which scope
`#[default]` to enum variants.

Have the struct arm iterate the members and emit an error for any field
carrying `#[default]`. The best-effort `Default` impl is still generated (no
early return), matching the derive's existing error-recovery behavior.

Adds a regression case to the derive diagnostics golden.
@orizi
orizi force-pushed the orizi/07-21-fix_plugins_reject_default_on_a_struct_member branch from 28f63c1 to 3682588 Compare July 21, 2026 12:32

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@orizi made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on eytan-starkware and TomerStarkware).


crates/cairo-lang-plugins/src/test_data/derive line 867 at r1 (raw file):

Previously, eytan-starkware wrote…

#[default] is not supported on struct members

Done.

@orizi
orizi enabled auto-merge July 21, 2026 12:32

@eytan-starkware eytan-starkware left a comment

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.

:lgtm:

@eytan-starkware reviewed 2 files and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@orizi
orizi added this pull request to the merge queue Jul 21, 2026
Merged via the queue into main with commit ec3c9e4 Jul 21, 2026
55 checks passed
@orizi
orizi deleted the orizi/07-21-fix_plugins_reject_default_on_a_struct_member branch July 21, 2026 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants