Zig: make the C pointer type always a compile error unless the file declares that it is generated code

Created on 1 Aug 2019  路  10Comments  路  Source: ziglang/zig

Unsurprisingly, the relaxed rules about C pointer types have resulted in people using them where other, more restricted types, would be preferred. Perhaps it's time for the language to get more involved in policing this.

accepted proposal

Most helpful comment

How this will work:

  • New keyword: thisfileisautotranslatedfromc. Nobody wants to type that!
  • The keyword may be the first token in a file, followed by a semicolon. It is illegal otherwise.
  • If the keyword is found, then a file is allowed to contain [*c] pointers. Otherwise, the compile error is error: [*c] pointers are only allowed in auto-translated C code, note: * and [*] pointers may be used where a [*c] pointer is expected.

All 10 comments

Do you have a link to examples of people using it as such?

How this will work:

  • New keyword: thisfileisautotranslatedfromc. Nobody wants to type that!
  • The keyword may be the first token in a file, followed by a semicolon. It is illegal otherwise.
  • If the keyword is found, then a file is allowed to contain [*c] pointers. Otherwise, the compile error is error: [*c] pointers are only allowed in auto-translated C code, note: * and [*] pointers may be used where a [*c] pointer is expected.

Another use case for using C pointers is for testing, i.e.

https://github.com/ziglang/zig/pull/3111/files#diff-9bb6309b18efdcc04260fad6ad9f55edR83

So would we add the thisfileisautotranslatedfromc token to this test file...even though it isn't translated from C?

https://github.com/ziglang/zig/pull/3111/files#diff-9bb6309b18efdcc04260fad6ad9f55edR91

I could be missing something, but there seems to be more valid use cases for C pointers besides translating from C. If you're interfacing with a C library, maybe you need to write some utility functions that help bridge the GAP between Zig and C? It may make sense to use C pointers to add special handling for those specific pointer types?

One thought came to mind. You could also always allow C pointers in test blocks. That would solve the problem of using C pointers in test code. But still not sure whether or not there are other valid use cases for C pointers...

I had a thought today re: C pointers: all [*c]T are currently nullable. What if we instead translated C pointers as ?[*c]T, which would be a "optional C pointer". (you could get a plain [*c]T if the C source had __attribute__((nonnull)))
That was [*c] only means "unknown if single pointer or pointer to many"?

Another example today:

<riba> i need to provide a callback to C code that takes a ** argument
<riba> i tried it like this: [*c][*c]
<riba> now i want to check both pointers to null
<riba> and "if (stanza == null || stanza.* == null)" does not work

perhaps, we could add a note suggesting to use a different pointer type, rather than adding a new keyword. e.g. note: * and [*] pointers may be used where a [*c] pointer is expected.

Random thought: rename [*c]T to [*ambiguous]T or even [*autotranslated]T

@hryx [*ambiguous]T seems descriptive plus awful to type which probably is good for C pointers :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bronze1man picture bronze1man  路  3Comments

bheads picture bheads  路  3Comments

andrewrk picture andrewrk  路  3Comments

S0urc3C0de picture S0urc3C0de  路  3Comments

andrewrk picture andrewrk  路  3Comments