Skip to content

Add borrowed Ref variants of types - #560

Closed
leighmcculloch wants to merge 26 commits into
mainfrom
generated-ref-types
Closed

leighmcculloch wants to merge 26 commits into
mainfrom
generated-ref-types

Conversation

@leighmcculloch

@leighmcculloch leighmcculloch commented Jul 28, 2026

Copy link
Copy Markdown
Member

Note

Part of a stack of PRs that must merge in this order.

A first group of PRs change contract specs so that they are produced at compile time instead of at proc-macro execution time. This provides the foundation to construct the specs from information that is only known at compile time, like the fully qualified name of a type:

  1. Add borrowed Ref variants of types #560 ← this PR
  2. Add const XDR serialization #562
  3. Encode contract spec XDR at compile time rs-soroban-sdk#1965

A second group of PRs changes the type names that the sdk stores in specs are fully qualified type names, and then the cli reduces them down to unique simple identifiers. During the contract build the types are given names like ::mycrate::mymod::MyType instead of MyType. Then the cli reduces them back down to simple names after spec shaking. Qualified type names make it possible to uniquely identify types in the spec, even when they have the same name. This resolves several problems with contract specs the type identify problem (stellar/rs-soroban-sdk#1570), type aliases limitations (stellar/rs-soroban-sdk#1857 stellar/rs-soroban-sdk#1063), and optimise spec shaking data section size (stellar/rs-soroban-sdk#1978):

  1. Widen contract spec type and event name limits stellar-xdr#312
  2. Widen contract spec type and event name limits #566
  3. Qualify type and event names rs-soroban-sdk#1970
  4. Reduce qualified type and event names stellar-cli#2674

Note that downstream clients and SDKs should see no, or little, change because the cli will during the build process reduce the fully qualified names back to simple unique names. Contracts that had colliding type names, which meant they could not be used with clients, will now work.

A third group of PRs are an optimisation to spec shaking v2, and will use the new unique type names to shake type specs by reachability, producing a dependency graph (thanks @mootz12), so that only spec entries that can't be reached from fns, like errors and events, get spec markers from dead-code-elimination:

  1. Shake specs by reachability and markers rs-soroban-sdk#2043
  2. Shake by the recorded version stellar-cli#2720

What

Add a borrowing {Type}Ref<'a> counterpart for every generated type that directly or transitively contains heap data.

Why

The owned types require heap allocation to construct, so XDR values can't be built from borrowed slices or inside const contexts.

Known limitations

View types encode via WriteXdr/to_xdr but cannot implement ReadXdr/from_xdr (decoding must target the owned types), and they have no serde support.

@leighmcculloch
leighmcculloch changed the base branch from main to derive-default-for-cli-formats July 28, 2026 11:47
@leighmcculloch
leighmcculloch changed the base branch from derive-default-for-cli-formats to main July 28, 2026 11:47
@leighmcculloch leighmcculloch changed the title Add borrowed Ref variants of generated types Add borrowed View variants of generated types Aug 25, 2026
@leighmcculloch
leighmcculloch force-pushed the generated-ref-types branch 6 times, most recently from 2fc5c3f to ec4e8cb Compare August 26, 2026 12:07
@leighmcculloch leighmcculloch changed the title Add borrowed View variants of types Add borrowed Ref variants of types Sep 7, 2026
@leighmcculloch
leighmcculloch marked this pull request as ready for review September 8, 2026 02:23
Copilot AI balanced review requested due to automatic review settings September 8, 2026 02:23

Copilot AI 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.

Pull request overview

Adds allocation-free borrowed Ref counterparts for heap-backed generated XDR types.

Changes:

  • Adds borrowed container types, conversion support, and XDR encoding.
  • Extends generator analysis/templates to emit borrowed variants.
  • Regenerates XDR output and adds integration tests.

Reviewed changes

Copilot reviewed 1 out of 484 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/generated.rs Adds shared borrowed-type infrastructure.
src/generated/*.rs Regenerates affected XDR types with Ref or IntoOwned implementations.
tests/common/mod.rs Provides representative owned and borrowed fixtures.
tests/ref_types.rs Tests construction, limits, conversion, and encoding.
xdr-generator-rust/generator/header.rs Defines borrowed containers and conversion support.
xdr-generator-rust/generator/src/generator.rs Analyzes which types require borrowed variants.
xdr-generator-rust/generator/src/output.rs Adds borrowed-type template metadata.
xdr-generator-rust/generator/src/tests/generator.rs Tests generator borrow analysis.
xdr-generator-rust/generator/src/types.rs Resolves borrowed and cyclic field types.
xdr-generator-rust/generator/templates/enum.rs.jinja Emits identity conversions for enums.
xdr-generator-rust/generator/templates/into_owned.rs.jinja Adds reusable identity conversion generation.
xdr-generator-rust/generator/templates/struct.rs.jinja Emits borrowed structs and conversions.
xdr-generator-rust/generator/templates/typedef_newtype.rs.jinja Emits borrowed newtypes and conversions.
xdr-generator-rust/generator/templates/union.rs.jinja Emits borrowed unions and cyclic conversions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +655 to +659
let mut borrow = BorrowCfg::Never;
for def in defs {
let def_cfg = def.cfg().map(CfgExpr::render);
borrow = borrow.or(self.of_def(def).and_cfg(def_cfg.as_deref()));
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This gap is pretty narrow and won't break compilation. I think we should defer to look at this if we ever need it.

auto-merge was automatically disabled September 8, 2026 12:59

Pull request was closed

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.

2 participants