Skip to content

bridge-cpp: static and instance methods - #4088

Merged
codeshaunted merged 2 commits into
canaryfrom
avery/cpp-methods
Jul 19, 2026
Merged

codeshaunted merged 2 commits into
canaryfrom
avery/cpp-methods

Conversation

@codeshaunted

@codeshaunted codeshaunted commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Bridge-week step 11, re-landed from -full under all the current machinery.

What

class Greeter {
  name string
  function create(name: string) -> Greeter { ... }          // static
  function who(self) -> string { ... }                      // instance
}
Greeter g = Greeter::create("ada");            // static member
std::string n = g.who();                        // const member; *this rides as the self kwarg
auto fut = g.who_async();                       // every method gets its _async sibling
  • Instance methods encode *this as the self kwarg (deep-copy semantics, python parity); dispatch = class wire symbol + .member.
  • Typed throws, opts structs (nested in the class: OptBox::make_opts, owner-qualified in the out-of-line bindings.cc definitions), /// Raises: doc blocks, and _async siblings returning baml::future<T, variant<...>> all ride the existing callable machinery — zero runtime changes.
  • Naming pool grows CppNameKind::Method (tag appended; existing typed-hash tags never move).
  • Class emission ordering waits on method optional-param types during the first fixed-point pass, so nested opts structs never reference an undeclared type (the known gotcha from the first landing).

Tests

  • test_methods_on_classes.cc: python port, 6 engine round-trip cases (static create, instance who/greet, all async siblings).
  • opt_box_method_matrix in test_optional_args.cc: static + instance methods with opts structs.
  • method_raises_blocks in test_raises.cc: the Raises: blocks python pins in its .pyi stubs.

All 12 sdk_test_cpp fixture jobs pass locally.

Summary by CodeRabbit

  • New Features

    • The C++ SDK generator now emits both static and instance class methods, including synchronous and asynchronous variants.
    • Method calling supports parameters, optional arguments, and instance receiver handling.
    • Generated C++ documentation now includes method-specific exception (Raises) details.
  • Tests

    • Added C++ tests covering class method creation and invocation, asynchronous results, optional-argument probing, and Raises documentation formatting/reachability.

Bridge-week step 11, re-landed from -full under the current machinery.
Classes with BAML methods emit real member functions: statics as static
members, instance methods as const members encoding *this as the self
kwarg (deep-copy semantics, python parity), each with its _async sibling
returning baml::future - typed throws, opts structs (nested in the
class, owner-qualified in the out-of-line bindings.cc definitions), and
Raises: doc blocks all ride the existing callable machinery. Dispatch
goes to the class wire symbol plus the method's source member token.

The naming pool grows CppNameKind::Method (tag appended; existing
typed-hash tags never move), and class emission ordering waits on method
optional-param types during the first fixed-point pass so nested opts
structs never reference an undeclared type.

Ports: test_methods_on_classes (6 engine round-trip cases),
opt_box_method_matrix, and the method Raises: blocks python pins in its
.pyi stubs.
@vercel

vercel Bot commented Jul 19, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
beps Ready Ready Preview, Comment Jul 19, 2026 6:53am
promptfiddle Ready Ready Preview, Comment Jul 19, 2026 6:53am
promptfiddle2 Ready Ready Preview, Comment Jul 19, 2026 6:53am

Request Review

@github-actions

Copy link
Copy Markdown

⏭️ Performance benchmarks were skipped

Perf benchmarks (CodSpeed) are opt-in on pull requests — they no longer run on every push. They always run automatically after merge to canary/main.

To run them on this PR, do any of the following, then push a commit (or re-run CI):

  • Add RUN_CODSPEED=1 to the PR description, or
  • Include run-perf or /perf in the PR title or any commit message.

@vercel
vercel Bot temporarily deployed to Preview – beps July 19, 2026 06:17 Inactive
@coderabbitai

coderabbitai Bot commented Jul 19, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 75fc92be-2df0-4b36-825f-b0f7c9908551

📥 Commits

Reviewing files that changed from the base of the PR and between ef726d6 and 9e9bfb4.

📒 Files selected for processing (1)
  • baml_language/sdk_tests/crates/cpp/function_calls/customizable/tests/test_raises.cc
🚧 Files skipped from review as they are similar to previous changes (1)
  • baml_language/sdk_tests/crates/cpp/function_calls/customizable/tests/test_raises.cc

📝 Walkthrough

Walkthrough

The C++ SDK generator now emits static and instance class methods with synchronous and asynchronous variants, method-specific naming and dispatch, qualified bindings, receiver encoding, option structs, and tests covering calls, optional arguments, symbol reachability, and Raises documentation.

Changes

C++ class method generation

Layer / File(s) Summary
Method naming and callable contracts
baml_language/sdks/cpp/sdkgen_cpp/src/naming.rs, baml_language/sdks/cpp/sdkgen_cpp/src/lib.rs
Adds CppNameKind::Method, stable method name tags, method symbol allocation, and class/member-based runtime dispatch keys.
Class method collection and emission
baml_language/sdks/cpp/sdkgen_cpp/src/lib.rs
Tracks static and instance methods per class, collects method-related symbols and options, skips generic methods, and handles incomplete method option types.
Method declarations and bindings
baml_language/sdks/cpp/sdkgen_cpp/src/lib.rs
Generates class declarations and option structs, then emits qualified sync/async definitions with instance receiver encoding.
Class method behavior validation
baml_language/sdk_tests/crates/cpp/function_calls/customizable/tests/test_methods_on_classes.cc, baml_language/sdk_tests/crates/cpp/function_calls/customizable/tests/test_optional_args.cc, baml_language/sdk_tests/crates/cpp/function_calls/customizable/tests/test_raises.cc
Tests Greeter methods, optional method arguments, symbol reachability, and method Raises documentation.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CppCaller
  participant GeneratedGreeterBinding
  participant BAMLDispatch
  CppCaller->>GeneratedGreeterBinding: invoke Greeter method
  GeneratedGreeterBinding->>BAMLDispatch: send method key, self, and arguments
  BAMLDispatch-->>GeneratedGreeterBinding: return sync result or async future
  GeneratedGreeterBinding-->>CppCaller: return value
Loading

Poem

A rabbit hops through methods bright,
Sync by day and async night.
“self” goes in, results come back,
Optional carrots fill the stack.
Raises docs stand neat and clear—
The C++ burrow cheers this year!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding static and instance method support in the C++ bridge.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch avery/cpp-methods

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 19, 2026 06:24 Inactive
@github-actions

github-actions Bot commented Jul 19, 2026 •

Copy link
Copy Markdown

Binary size checks failed

❌ 4 violations · ✅ 3 passed

⚠️ Please fix the size gate issues or acknowledge them by updating baselines.

Artifact Platform File Gzip Gated on Baseline Delta Status
❌ baml-cli Linux 🔒 25.3 MB 10.7 MB file 24.5 MB +764.7 KB (+3.1%) FAIL
✅ packed-program Linux 🔒 17.0 MB 7.0 MB file 17.0 MB -4.1 KB (-0.0%) OK
❌ baml-cli macOS 🔒 19.5 MB 9.3 MB file 18.9 MB +644.9 KB (+3.4%) FAIL
✅ packed-program macOS 🔒 13.2 MB 6.2 MB file 13.2 MB +0 B (+0.0%) OK
❌ baml-cli Windows 🔒 21.1 MB 9.5 MB file 20.4 MB +649.2 KB (+3.2%) FAIL
✅ packed-program Windows 🔒 14.1 MB 6.2 MB file 14.1 MB -512 B (-0.0%) OK
❌ bridge_wasm WASM 16.2 MB 🔒 4.4 MB gzip 4.3 MB +130.5 KB (+3.1%) FAIL

🔒 = the size this artifact is GATED on (ceiling + delta). Binaries gate on file size (installed binary); WASM gates on gzip (download size). The other size is shown for information only.

Details & how to fix

Violations:

  • baml-cli (Linux) file_bytes: 25.3 MB exceeds limit of 25.2 MB (exceeded by +29.5 KB, policy: max_file_bytes)
  • baml-cli (Linux) file_delta_pct: +3.1% exceeds limit of 3.0% (exceeded by +0.1pp, policy: max_delta_pct)
  • baml-cli (macOS) file_bytes: 19.5 MB exceeds limit of 19.5 MB (exceeded by +77.9 KB, policy: max_file_bytes)
  • baml-cli (macOS) file_delta_pct: +3.4% exceeds limit of 3.0% (exceeded by +0.4pp, policy: max_delta_pct)
  • baml-cli (Windows) file_bytes: 21.1 MB exceeds limit of 21.0 MB (exceeded by +36.4 KB, policy: max_file_bytes)
  • baml-cli (Windows) file_delta_pct: +3.2% exceeds limit of 3.0% (exceeded by +0.2pp, policy: max_delta_pct)
  • bridge_wasm (WASM) gzip_bytes: 4.4 MB exceeds limit of 4.4 MB (exceeded by +2.3 KB, policy: max_gzip_bytes)
  • bridge_wasm (WASM) gzip_delta_pct: +3.1% exceeds limit of 3.0% (exceeded by +0.1pp, policy: max_delta_pct)

Add/update baselines:

.ci/size-gate/aarch64-apple-darwin.toml:

[artifacts.baml-cli]
file_bytes = 19545088
stripped_bytes = 19545136
gzip_bytes = 9331880

.ci/size-gate/wasm32-unknown-unknown.toml:

[artifacts.bridge_wasm]
file_bytes = 16160508
gzip_bytes = 4402886

.ci/size-gate/x86_64-pc-windows-msvc.toml:

[artifacts.baml-cli]
file_bytes = 21075968
stripped_bytes = 21075968
gzip_bytes = 9542344

.ci/size-gate/x86_64-unknown-linux-gnu.toml:

[artifacts.baml-cli]
file_bytes = 25272400
stripped_bytes = 25272392
gzip_bytes = 10732613

Generated by cargo size-gate · workflow run

Parity-workflow finding: python's test_imports pins all five raises_test
symbols; InferredThrow and PureLen were never compiled against in the
C++ suite (header scrapes are namespace-blind), so a wrong-namespace
codegen regression would pass. The established (void)sizeof / (void)&
reachability pattern closes it.
@vercel
vercel Bot temporarily deployed to Preview – beps July 19, 2026 06:32 Inactive

@coderabbitai coderabbitai Bot 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.

🧹 Nitpick comments (1)
baml_language/sdks/cpp/sdkgen_cpp/src/lib.rs (1)

174-221: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add unit tests for the new method-dispatch path

  • baml_language/sdks/cpp/sdkgen_cpp/src/naming.rs: cover CppNameKind::Method so tag() stays 10 and renders_bare() stays true.
  • baml_language/sdks/cpp/sdkgen_cpp/src/lib.rs: cover emit_callable/pass 3 so method call_fqn uses class.member and static vs. instance methods land in the right vectors.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@baml_language/sdks/cpp/sdkgen_cpp/src/lib.rs` around lines 174 - 221, Add
unit coverage in baml_language/sdks/cpp/sdkgen_cpp/src/naming.rs:128-177 for
CppNameKind::Method, asserting tag() remains 10 and renders_bare() remains true.
Add pass-3 tests in baml_language/sdks/cpp/sdkgen_cpp/src/lib.rs:174-221
covering emit_callable and method dispatch, verifying call_fqn uses class.member
and static_methods versus instance_methods receive the correct emitted methods.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@baml_language/sdks/cpp/sdkgen_cpp/src/lib.rs`:
- Around line 174-221: Add unit coverage in
baml_language/sdks/cpp/sdkgen_cpp/src/naming.rs:128-177 for CppNameKind::Method,
asserting tag() remains 10 and renders_bare() remains true. Add pass-3 tests in
baml_language/sdks/cpp/sdkgen_cpp/src/lib.rs:174-221 covering emit_callable and
method dispatch, verifying call_fqn uses class.member and static_methods versus
instance_methods receive the correct emitted methods.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 2278b60d-8085-4dae-baa4-da18b4bd62d6

📥 Commits

Reviewing files that changed from the base of the PR and between 972fdd2 and ef726d6.

📒 Files selected for processing (5)
  • baml_language/sdk_tests/crates/cpp/function_calls/customizable/tests/test_methods_on_classes.cc
  • baml_language/sdk_tests/crates/cpp/function_calls/customizable/tests/test_optional_args.cc
  • baml_language/sdk_tests/crates/cpp/function_calls/customizable/tests/test_raises.cc
  • baml_language/sdks/cpp/sdkgen_cpp/src/lib.rs
  • baml_language/sdks/cpp/sdkgen_cpp/src/naming.rs

@codeshaunted
codeshaunted enabled auto-merge July 19, 2026 06:37
@codeshaunted
codeshaunted added this pull request to the merge queue Jul 19, 2026
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 19, 2026 06:40 Inactive
Merged via the queue into canary with commit fe33043 Jul 19, 2026
61 of 62 checks passed
@codeshaunted
codeshaunted deleted the avery/cpp-methods branch July 19, 2026 06:43
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 19, 2026 06:53 Inactive

This branch was previously deployed

3 inactive deployments
Preview – promptfiddle — 9e9bfb44 Deployed Jul 19, 2026 by vercel[bot]
Preview – promptfiddle2 — 9e9bfb44 Deployed Jul 19, 2026 by vercel[bot]
Preview – beps — 9e9bfb44 Deployed Jul 19, 2026 by vercel[bot]
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.

1 participant