feat(candid_parser)!: parse and preserve named function arguments#742
Conversation
Capture argument and result names in the parser AST so binding generators
(e.g. the JS bindgen) can emit meaningful parameter names instead of
positional `arg0`/`arg1`.
candid_parser (breaking, 0.3.2 -> 0.4.0):
- Add `syntax::IDLArgType { typ: IDLType, name: Option<String> }` with
`new`/`new_with_name` (purely numeric names normalize to `None`).
- `FuncType::{args,rets}`, `IDLType::ClassT`, `IDLTypes::args`, and
`IDLInitArgs::args` now hold `Vec<IDLArgType>` instead of `Vec<IDLType>`.
- Grammar captures named args; the pretty-printer round-trips names.
- Type checking is unchanged: names are dropped when lowering to
`candid::types::Function`, so the candid type model is unaffected.
candid (non-breaking, 0.10.29 -> 0.10.30):
- Add `pretty::utils::sep_enclose` / `sep_enclose_space`.
- Add `TypeEnv::to_sorted_iter()`.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Click to see raw report |
There was a problem hiding this comment.
Pull request overview
This PR ports the named-argument parsing feature into candid_parser so downstream binding generators can preserve meaningful function parameter/result names in the parser AST, while keeping the candid crate’s core type model unchanged. It also adds small utilities to candid for deterministic type environment iteration and reusable pretty-print combinators, plus corresponding version bumps and golden test fixture updates.
Changes:
- Introduces
syntax::IDLArgType { typ, name }and updates function/class/arg containers incandid_parserto carry optional argument/result names, including grammar support and pretty-print round-tripping. - Adds
pretty::utils::{sep_enclose, sep_enclose_space}andTypeEnv::to_sorted_iter()incandid, with crate version bumps and lockfile updates. - Updates
didcand parser typing/tests to work withIDLArgType, and refreshes.didgolden assets to reflect preserved names.
Reviewed changes
Copilot reviewed 17 out of 19 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/didc/src/main.rs | Adjusts type annotation parsing to use IDLArgType.typ. |
| rust/candid/src/types/type_env.rs | Adds deterministic to_sorted_iter() iterator. |
| rust/candid/src/pretty/utils.rs | Adds sep_enclose / sep_enclose_space pretty-print helpers. |
| rust/candid/Cargo.toml | Bumps candid to 0.10.30 and syncs candid_derive dependency. |
| rust/candid_derive/Cargo.toml | Bumps candid_derive to 0.10.30. |
| rust/candid_parser/Cargo.toml | Bumps candid_parser to 0.4.0 (breaking). |
| rust/candid_parser/src/typing.rs | Drops arg names during lowering/type-checking by using arg.typ. |
| rust/candid_parser/src/test.rs | Updates test harness to pass IDLArgType.typ to type lowering. |
| rust/candid_parser/src/syntax/pretty.rs | Pretty-prints named args/rets in function types and class constructors. |
| rust/candid_parser/src/syntax/mod.rs | Introduces IDLArgType and updates AST containers to use it. |
| rust/candid_parser/src/grammar.lalrpop | Parses named args and checks name uniqueness. |
| rust/candid_parser/tests/assets/ok/*.did | Updates golden .did outputs to include preserved argument names. |
| CHANGELOG.md | Documents releases for candid 0.10.30 and candid_parser 0.4.0. |
| Cargo.lock | Updates workspace lock to new crate versions. |
| rust/bench/Cargo.lock | Updates bench lock to new crate versions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Use error2 with the tuple span for the duplicate named-argument check, consistent with the other uniqueness checks in the grammar (was error, which reports a 0..0 span). Drop the now-unused error import. - Fix grammar typo in IDLArgType::new_with_name doc comment. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
✅ No security or compliance issues detected. Reviewed everything up to 39ad347. Security Overview
Detected Code Changes| Change Type | Relevant files ... (code changes summary truncated to fit VCS comment limits.) |
Summary
Lets binding generators emit meaningful parameter names (
transfer(to, amount)instead oftransfer(arg0, arg1)) by capturing argument/result names in thecandid_parserAST — without changing thecandidtype model (candidstays 0.10).This is the subset of the
nextbranch's named-args feature that the icp-js-bindgen project needs, ported tomasterso it can depend on published releases instead of the floatingnextbranch.Changes
candid_parser— breaking (0.3.2 → 0.4.0)syntax::IDLArgType { typ: IDLType, name: Option<String> }withIDLArgType::new/new_with_name(purely numeric names normalize toNone).FuncType::{args, rets},IDLType::ClassT,IDLTypes::args, andIDLInitArgs::argsnow holdVec<IDLArgType>instead ofVec<IDLType>.(from : principal)).candid::types::Function, so thecandidcrate's type model is unaffected.candid— non-breaking (0.10.29 → 0.10.30)pretty::utils::sep_enclose/sep_enclose_space(list/tuple combinators).TypeEnv::to_sorted_iter()for deterministic, key-sorted iteration.Test plan
cargo test --workspace --all-features(golden.didfiles updated to reflect preserved arg names)cargo clippy -p candid_parser --all-featureswasm32-unknown-unknown)🤖 Generated with Claude Code