mozilla / mozilla/cbindgen

Optional function argument is not exported correctly

Open
#1,037 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted
Dominant language
Rust
Stars
3k
Forks
386
Avg merge
1h 43m
Merged PRs (30d)
1

Description

I stumbled upon a weird case... it's unlikely to appear in real code but rust supports it, so cbindgen should support it too.

example.rs

#[allow(unexpected_cfgs)]
#[no_mangle]
pub extern "C" fn foobar(a1: u32, #[cfg(feature = "baz")] a2: u32) -> u32 {
    #[cfg(feature = "baz")]
    {
        a1 + a2
    }
    #[cfg(not(feature = "baz"))]
    {
        a1
    }
}

cbindgen.toml

language = "C"
[defines]
"feature = baz" = "FEATURE_BAZ"

BAD example.h (generated)

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

uint32_t foobar(uint32_t a1, uint32_t a2);

What would be the preferred way to handle this case?

  • generate multiple function declarations, each surrounded by an appropriate ifdef
  • surround each optional argument with an appropriate ifdef
  • apply a macro call to the optional argument text
  • something else

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the issue with the provided example.rs and cbindgen.toml, then inspect the generated example.h and the existing handling of cfg-gated function arguments. Done means reaching an agreed representation for the optional argument and adding coverage that verifies the generated C declaration for both feature configurations.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.