ziglang/zig

translate-c should not emit 'pub' for static functions

daurnimator opened this issue · 3 comments

static void foo() {}
void bar() {
    return foo();
}

Running translate-c at the moment you get:

pub fn foo() void {}
pub export fn bar() void {
    return foo();
}
  • a heap of C builtin defines.

I don't expect pub on foo.

isn't static also a c way (hacky) for marking functions as private to that compilation unit?

isn't static also a c way (hacky) for marking functions as private to that compilation unit?

It's not "also", but the way to mark a function as private to a compilation unit.

The static keyword:

  • For file-scope functions and variables: marks it as private to the compilation unit
  • For function-scoped variables: marks them as statically allocated: the variables storage is for the lifetime of the program. The more practical meaning is: the value will be remembered across function invocations.

Note that it does need to put pub on static functions when doing a @cImport, so that the functions are exposed. But that's right - the command line translate-c should not. There's an enum value given to the implementation to distinguish.

If you think about it, if the file is supposed to be used with #include then static functions should be pub. But if the file is supposed to be used with -o foo.o -c foo.c and linked in later, then the static functions should not be pub.

Note: this comment no longer applies after #20630 is implemented.