refactor: collapse ObjectKind accessor boilerplate into kind_accessor! macro#187
Draft
pmatos wants to merge 1 commit into
Draft
refactor: collapse ObjectKind accessor boilerplate into kind_accessor! macro#187pmatos wants to merge 1 commit into
pmatos wants to merge 1 commit into
Conversation
…! macro
The 17 standard ObjectKind accessor pairs (and immutable-only variants)
all followed an identical `if let ObjectKind::Variant(ref x) = self.kind
{ Some(x) } else { None }` pattern, repeated verbatim for both the
immutable and mutable form — ~30 method bodies in total.
Introduces a `kind_accessor!` declarative macro with two arms:
- `(name, mut_name, Variant, Type)` for immutable+mutable pairs
- `(name, Variant, Type)` for immutable-only
The three special-case methods that don't fit the pattern are kept as
hand-written impls with a comment explaining why:
- `intl_data` — boxed inner value requires an extra `.as_ref()` deref
- `finalization_registry{,_mut}` — struct variant with two named fields
- `shadow_realm_id` — Copy return type, not a reference
Adds 17 unit tests (kind_accessor_tests module) covering: Some/None
discrimination by kind, mutable access enables mutation, and mutual
exclusion between different kinds.
🤖 Generated with Claude Code
Claude-Session: https://claude.ai/code/session_012MbKtDLjpVxxY655NVhTSE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
kind_accessor!declarative macro insrc/interpreter/types.rsthat generates the standardObjectKindaccessor pattern (Somewhen variant matches,Noneotherwise) for both immutable and mutable forms.intl_data(boxed inner value needs.as_ref()),finalization_registry{,_mut}(struct variant with two named fields, tuple return), andshadow_realm_id(Copy return type, not a reference).kind_accessor_testsmodule covering:Some/Nonediscrimination by kind, mutable access enables mutation, and mutual exclusion between different kinds.Motivation
The audit identified this as the highest-ROI refactoring: every new
ObjectKindvariant previously required manually writing two structurally identical methods. A missed_mutaccessor was easy to overlook and would only be caught at a call site. The macro makes the pair atomic — you declare the variant once and both accessors appear.Test plan
cargo test kind_accessor— all 17 new tests passcargo test— full suite (181 tests) passes with no regressionscargo build— clean compile, no warningshttps://claude.ai/code/session_012MbKtDLjpVxxY655NVhTSE
Generated by Claude Code