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

bugfix(plugins): Sanitize hash and serde implementation. - #10117

Merged
orizi merged 1 commit into
mainfrom
orizi/06-17-bugfix_plugins_sanitize_hash_and_serde_implementation
Jun 17, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-17-bugfix_plugins_sanitize_hash_and_serde_implementation

Conversation

@orizi

@orizi orizi commented Jun 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fix variable name shadowing in #[derive(Hash)] and #[derive(Serde)] code generation when a struct field is named value (for Hash) or serialized (for Serde).

The generated update_state implementation for Hash used to bind each member into a local variable named after the field itself (e.g., let value = ...). If a field was named value, this shadowed the update_state function parameter value, causing incorrect code generation. Similarly, the Serde deserialize implementation bound members into locals named after the field, which could shadow the ref serialized parameter if a field was named serialized.

The fix prefixes all generated intermediate locals with __hash_derive_member_ (for Hash) and __serde_member_ (for Serde) to avoid any collision with parameter names or other identifiers.


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?

A struct with a field named value would cause the #[derive(Hash)] macro to emit code where the local variable let value = ... shadowed the value parameter of update_state, producing broken generated code. Likewise, a field named serialized in a #[derive(Serde)] struct would shadow the ref serialized parameter in the generated deserialize function.


What was the behavior or documentation before?

Generated Hash::update_state code used let {field_name} = ... and then referenced {field_name}.value, which broke when field_name was value. Generated Serde::deserialize code used let {field_name} = ..., which broke when field_name was serialized.


What is the behavior or documentation after?

Generated locals are now named __hash_derive_member_{field_name} and __serde_member_{field_name} respectively, preventing any shadowing of reserved parameter names regardless of what the struct fields are called. Tests covering StructWithValueFieldForHash and StructWithSerializedField are added to confirm correct round-trip behavior.


Related issue or discussion (if any)

#10115 #10116


Additional context

All existing snapshot test data files have been updated to reflect the new generated variable names.

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 17, 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 June 17, 2026 07:36
@cursor

cursor Bot commented Jun 17, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Narrow compiler-plugin codegen fix with behavior unchanged for normal field names; risk is limited to macro output correctness, covered by new tests and snapshot updates.

Overview
Fixes incorrect #[derive(Hash)] and #[derive(Serde)] codegen when struct fields collide with generated function parameters.

Hash used locals named after each field (e.g. let value = ...), which shadowed the update_state(..., value: T) parameter when a field was named value. Serde struct deserialize did the same for ref serialized when a field was named serialized. Generated bindings are now __hash_derive_member_{field} and __serde_member_{field} instead.

Regression tests cover a struct with a value field (hash) and a serialized field (serde round-trip). Snapshot/plugin test fixtures were refreshed for the new emitted names.

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

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

:lgtm:

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

@orizi
orizi added this pull request to the merge queue Jun 17, 2026
Merged via the queue into main with commit 28439dd Jun 17, 2026
55 checks passed
@orizi
orizi deleted the orizi/06-17-bugfix_plugins_sanitize_hash_and_serde_implementation branch June 17, 2026 15:13
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.

bug: Derive Hash field named value breaks codegen bug: Serde derive field named serialized shadows deserialize param

3 participants