Skip to content

Make C pointers more normal (non-optional and not coercible to/from integers) #6597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tadeokondrak opened this issue Oct 7, 2020 · 0 comments
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@tadeokondrak
Copy link
Contributor

Motivation: I want to support gcc's __attribute__((nonnull)) and clang's _Nonnull pointer qualifier in translate-c, but I think this is a good idea either way.

C pointers currently (taken from language reference):

  • Supports all the syntax of the other two pointer types.
  • Coerces to other pointer types, as well as Optional Pointers. When a C pointer is coerced to a non-optional pointer, safety-checked Undefined Behavior occurs if the address is 0.

You should still be able to index with [] and dereference with .*. .?/try/orelse come free with optional types.

  • Allows address 0. On non-freestanding targets, dereferencing address 0 is safety-checked Undefined Behavior. Optional C pointers introduce another bit to keep track of null, just like ?usize. Note that creating an optional C pointer is unnecessary as one can use normal Optional Pointers.

C pointers should be non-optional by default, and translate-c should translate most pointers as ?[*c]T.

  • This reduces the number of special cases in the compiler, syntax for optional pointers and c pointers have separate code paths
  • This allows using [*c]T as a type in translate-c for __attribute__((nonnull))-annotated function parameters or return types, or
    clang's _Nonnull annotatation (translate-c: ability to annotate types in C code for better translation #2457 but useful for C consumers too!)
  • Supports Type Coercion to and from integers.
  • Supports comparison with integers.

This should be removed completely.

Removing this is already partially accepted, other than comptime-known 0.
From what I can gather, this was initially for translate-c before it became as smart as it is now.

I think C APIs that benefit from this implicit behaviour are pretty rare, and we shouldn't make the language more weird to accomodate them.
We already don't change the language to accomodate using enums as bitflags, which are much more common.

  • Does not support Zig-only pointer attributes such as alignment. Use normal Pointers please!

No change here.

@daurnimator daurnimator added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Oct 7, 2020
@andrewrk andrewrk added this to the 0.8.0 milestone Oct 10, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Apr 23, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.16.0, 0.15.0 Apr 22, 2025
@andrewrk andrewrk added the accepted This proposal is planned. label Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted This proposal is planned. proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

3 participants