Skip to content

refactor: correct nesting depth for nested message type references, and introduce MessageScope to bundle codegen scope parameters#45

Merged
iainmcgin merged 2 commits intoanthropics:mainfrom
ariesdevil:dev_ariesdevil
Apr 16, 2026
Merged

Conversation

@ariesdevil
Copy link
Copy Markdown
Contributor

@ariesdevil ariesdevil commented Apr 9, 2026

Bugs fixed:

  1. protoc-gen-buffa: expose register_types option (default true). When json=true and using the packaging workflow (multiple files include!'d into the same module), the per-file register_types() functions collide. Setting register_types=false suppresses them.

  2. buffa-codegen: thread nesting depth through message and view codegen. Nested messages sit inside a pub mod, so their field type references need additional super:: hops. Previously all fields used nesting=0 regardless of depth.

  3. buffa-codegen: use ::core::result::Result in generated serde Deserialize impls. A proto message named "Result" creates a module that shadows std::result::Result, breaking the generated serde code.

Refactor:

Replace five repeatedly-threaded parameters (ctx, current_package,
proto_fqn, features, nesting) with a single MessageScope struct
across ~30 internal functions in message.rs and view.rs.

Motivation

Every new piece of scope-level state (most recently nesting and
oneof_idents) required touching dozens of function signatures and call
sites. MessageScope makes adding future scope state a one-field change.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 9, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@ariesdevil
Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Apr 9, 2026
@ariesdevil
Copy link
Copy Markdown
Contributor Author

@iainmcgin If you have time, PTAL

Three bugs fixed:

1. protoc-gen-buffa: expose register_types option (default true).
   When json=true and using the packaging workflow (multiple files
   include!'d into the same module), the per-file register_types()
   functions collide. Setting register_types=false suppresses them.

2. buffa-codegen: thread nesting depth through message and view
   codegen. Nested messages sit inside a pub mod, so their field
   type references need additional super:: hops. Previously all
   fields used nesting=0 regardless of depth.

3. buffa-codegen: use ::core::result::Result in generated serde
   Deserialize impls. A proto message named "Result" creates a
   module that shadows std::result::Result, breaking the generated
   serde code.

Replace five repeatedly-threaded parameters (`ctx`, `current_package`,
`proto_fqn`, `features`, `nesting`) with a single `MessageScope` struct
across ~30 internal functions in `message.rs` and `view.rs`.

**Motivation**

Every new piece of scope-level state (most recently `nesting` and
`oneof_idents`) required touching dozens of function signatures and call
sites. `MessageScope` makes adding future scope state a one-field change.
@ariesdevil ariesdevil changed the title fix: correct nesting depth for nested message type references refactor: correct nesting depth for nested message type references, and introduce MessageScope to bundle codegen scope parameters Apr 16, 2026
The initial fix plumbed nesting into classify_field and its immediate
helpers but left several sibling paths still passing 0 (or 1)
regardless of the containing message's actual depth. A nested message
that referenced a cross-package enum via any of these paths would
emit the wrong number of super:: hops and fail to compile.

Remaining sites fixed:
- oneof.rs collect_variant_info: oneof variant types now use
  nesting + 1 instead of the literal 1 (the enum's own module sits
  one level deeper than its containing message).
- message.rs custom_deser_oneof_group: the serde DeserSeed impl for
  oneof groups now resolves variant types at the enclosing message's
  nesting instead of 0.
- impl_text.rs enum_type_path (and its callers scalar_merge_arm /
  repeated_merge_arm / oneof_merge_arms / map_merge_arm): textproto
  enum references now use the impl block's nesting depth.
- defaults.rs parse_default_value: proto2 [default = ENUM_VALUE]
  references now resolve at the consumer's nesting depth.
- message.rs generate_extensions call site: extensions declared
  inside a nested message use nesting + 1, not a hardcoded 1.

The dead scalar_or_message_type (nesting-0) wrapper was removed
after the last caller moved to scalar_or_message_type_nested.

Added a generation-level integration test that exercises nesting=2
through the owned-field, oneof-variant, and textproto enum paths
simultaneously, asserting that all cross-package references in a
triply-nested message use the correct number of super:: hops.
Copy link
Copy Markdown
Collaborator

@iainmcgin iainmcgin left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

@iainmcgin iainmcgin merged commit 155fe0b into anthropics:main Apr 16, 2026
7 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 16, 2026
@ariesdevil ariesdevil deleted the dev_ariesdevil branch April 17, 2026 03:00
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants