Skip to content

feat: support argument names#612

Merged
ilbertt merged 14 commits into
masterfrom
luca/support-arg-names
Jun 11, 2025
Merged

feat: support argument names#612
ilbertt merged 14 commits into
masterfrom
luca/support-arg-names

Conversation

@ilbertt

@ilbertt ilbertt commented Jun 10, 2025

Copy link
Copy Markdown
Contributor

Overview
Adds support for named arguments: if a .did Candid declaration file declares functions, classes or services with named arguments, those names are parsed by the candid-parser and preserved in the generated bindings.
At the same time, the names of the arguments are preserved if the Candid declaration file is generated from a Rust canister.

Requirements
To consider this feature complete, the Motoko compiler should preserve the actor methods names from Motoko canisters to the Candid declaration files.

Considerations
This change should be backwards compatible.

Picked up from https://github.com/ilbertt/candid/tree/luca/arg-names

@ilbertt ilbertt requested a review from a team as a code owner June 10, 2025 07:50
Comment thread rust/candid/src/types/internal.rs Outdated
Comment thread rust/candid/src/pretty/candid.rs Outdated
Comment thread rust/candid_parser/src/bindings/motoko.rs Outdated

@luc-blaeser luc-blaeser left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Very nice PR. Just a few little comments, mostly just questions for my own understanding. Thank you, Luca!

@ilbertt ilbertt requested a review from luc-blaeser June 10, 2025 08:58

@luc-blaeser luc-blaeser left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for following up. Ready to merge IMO

@ilbertt

ilbertt commented Jun 10, 2025

Copy link
Copy Markdown
Contributor Author

Let's wait for the fixes on the bench tests before merging this PR, cc @mraszyk

@github-actions

github-actions Bot commented Jun 10, 2025

Copy link
Copy Markdown
Name Max Mem (Kb) Encode Decode
blob 4_224 20_459_344 ($\textcolor{red}{0.00\%}$) 12_083_680 ($\textcolor{green}{-0.00\%}$)
btreemap 75_456 4_641_549_583 ($\textcolor{red}{0.00\%}$) 15_320_117_359 ($\textcolor{green}{-0.00\%}$)
nns 128 2_094_411 ($\textcolor{green}{-0.01\%}$) 5_652_196 ($\textcolor{red}{0.21\%}$)
nns_list_proposal 1_088 7_777_860 ($\textcolor{red}{0.26\%}$) 80_650_009 ($\textcolor{green}{-0.01\%}$)
option_list 128 7_909_886 ($\textcolor{red}{0.01\%}$) 26_307_108 ($\textcolor{red}{0.72\%}$)
text 6_336 20_456_583 ($\textcolor{red}{0.00\%}$) 17_315_582 ($\textcolor{green}{-0.00\%}$)
variant_list 128 7_962_587 ($\textcolor{red}{0.00\%}$) 24_569_887 ($\textcolor{green}{-0.00\%}$)
vec_int16 16_704 168_584_944 ($\textcolor{red}{0.00\%}$) 1_096_834_739 ($\textcolor{green}{-0.00\%}$)
  • Parser cost: 17_722_077 ($\textcolor{red}{0.01\%}$)
  • Extra args: 3_253_907 ($\textcolor{green}{-0.03\%}$)
Click to see raw report
---------------------------------------------------

Benchmark: blob
  total:
    instructions: 32.55 M (-0.00%) (change within noise threshold)
    heap_increase: 66 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    calls: 1 (no change)
    instructions: 20.46 M (0.00%) (change within noise threshold)
    heap_increase: 66 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    calls: 1 (no change)
    instructions: 12.08 M (-0.00%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: text
  total:
    instructions: 37.77 M (-0.00%) (change within noise threshold)
    heap_increase: 99 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    calls: 1 (no change)
    instructions: 20.46 M (0.00%) (change within noise threshold)
    heap_increase: 66 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    calls: 1 (no change)
    instructions: 17.32 M (-0.00%) (change within noise threshold)
    heap_increase: 33 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: vec_int16
  total:
    instructions: 1.27 B (-0.00%) (change within noise threshold)
    heap_increase: 261 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    calls: 1 (no change)
    instructions: 168.58 M (0.00%) (change within noise threshold)
    heap_increase: 261 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    calls: 1 (no change)
    instructions: 1.10 B (-0.00%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: btreemap
  total:
    instructions: 19.96 B (0.00%) (change within noise threshold)
    heap_increase: 1179 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    calls: 1 (no change)
    instructions: 4.64 B (0.00%) (change within noise threshold)
    heap_increase: 159 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    calls: 1 (no change)
    instructions: 15.32 B (-0.00%) (change within noise threshold)
    heap_increase: 1020 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: option_list
  total:
    instructions: 34.22 M (0.56%) (change within noise threshold)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    calls: 1 (no change)
    instructions: 7.91 M (0.01%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    calls: 1 (no change)
    instructions: 26.31 M (0.72%) (change within noise threshold)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: variant_list
  total:
    instructions: 32.53 M (-0.00%) (change within noise threshold)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    calls: 1 (no change)
    instructions: 7.96 M (0.00%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    calls: 1 (no change)
    instructions: 24.57 M (-0.00%) (change within noise threshold)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: nns
  total:
    instructions: 26.30 M (0.05%) (change within noise threshold)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

  0. Parsing (scope):
    calls: 1 (no change)
    instructions: 17.72 M (0.01%) (change within noise threshold)
    heap_increase: 2 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    calls: 1 (no change)
    instructions: 2.09 M (-0.01%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    calls: 1 (no change)
    instructions: 5.65 M (0.21%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: nns_list_proposal
  total:
    instructions: 88.43 M (0.02%) (change within noise threshold)
    heap_increase: 17 pages (no change)
    stable_memory_increase: 0 pages (no change)

  1. Encoding (scope):
    calls: 1 (no change)
    instructions: 7.78 M (0.26%) (change within noise threshold)
    heap_increase: 3 pages (no change)
    stable_memory_increase: 0 pages (no change)

  2. Decoding (scope):
    calls: 1 (no change)
    instructions: 80.65 M (-0.01%) (change within noise threshold)
    heap_increase: 14 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Benchmark: extra_args
  total:
    instructions: 3.25 M (-0.03%) (change within noise threshold)
    heap_increase: 0 pages (no change)
    stable_memory_increase: 0 pages (no change)

---------------------------------------------------

Summary:
  instructions:
    status:   No significant changes 👍
    counts:   [total 9 | regressed 0 | improved 0 | new 0 | unchanged 9]
    change:   [max +189.35K | p75 +12.92K | median -34 | p25 -64 | min -1.12K]
    change %: [max +0.56% | p75 +0.02% | median -0.00% | p25 -0.00% | min -0.03%]

  heap_increase:
    status:   No significant changes 👍
    counts:   [total 9 | regressed 0 | improved 0 | new 0 | unchanged 9]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

  stable_memory_increase:
    status:   No significant changes 👍
    counts:   [total 9 | regressed 0 | improved 0 | new 0 | unchanged 9]
    change:   [max 0 | p75 0 | median 0 | p25 0 | min 0]
    change %: [max 0.00% | p75 0.00% | median 0.00% | p25 0.00% | min 0.00%]

---------------------------------------------------
Successfully persisted results to canbench_results.yml

@ilbertt ilbertt requested a review from luc-blaeser June 11, 2025 08:31
let args = <>;
let mut named_args: Vec<String> = args.iter().filter_map(|a| a.name.clone()).collect();
named_args.sort();
check_unique(named_args.iter()).map_err(|e| error(e))?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just a note for future refactoring, nothing to fix here: Probably the function check_unique should be renamed as it requires sorted input. One could assume that a general unique check works generally on unsorted input.

Comment thread rust/candid_parser/src/types.rs Outdated
Comment thread rust/candid_parser/tests/assets/ok/fieldnat.did Outdated

@luc-blaeser luc-blaeser left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice adjustments, good catch of numbered args case.

@ilbertt ilbertt merged commit 61a49c8 into master Jun 11, 2025
9 checks passed
@ilbertt ilbertt deleted the luca/support-arg-names branch June 11, 2025 17:57
lwshang added a commit that referenced this pull request Jul 25, 2025
…ate (#652)

This PR refactors recent changes (#612 and #640) to avoid a major
version bump in `candid` crate.
> * Breaking changes:
> + `pp_args` and `pp_init_args` now require a `&[ArgType]` parameter.
The `pp_rets` function has been added, with the signature of the old
`pp_args`.
> + `pretty::candid::pp_label` now takes a `&Label` parameter, instead
of a `&SharedLabel`.


The previously altered API (`pp_args`, `pp_init_args`, `pp_label`) has
been restored to maintain backward compatibility.

The intended new behavior is now exposed via new, non-breaking methods:
`pp_named_args`, `pp_named_init_args` and `pp_label_raw`.
ilbertt added a commit that referenced this pull request Jul 28, 2025
@ilbertt ilbertt mentioned this pull request Jul 28, 2025
ilbertt added a commit that referenced this pull request Jul 29, 2025
**Overview**
Revert the changes made in #612 to remove the breaking changes.
ilbertt added a commit that referenced this pull request Jul 29, 2025
ilbertt added a commit that referenced this pull request Oct 9, 2025
Named arguments can also be in the return type, and can be ignored. This PR restores that was changed in https://github.com/dfinity/candid/pull/612/files#diff-c5ff9610c5d6538ec79f3d465965d2b3a3e3b628b16854a91b4b4a90ba766a57L220-L230, in order to keep parsing the return types with arguments without throwing.

Updates a test case to check if the behavior has been restored.
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