Skip to content

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

@tadeokondrak

Description

@tadeokondrak

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.

  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThis proposal is planned.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions