-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
proposal: type for null terminated pointer #265
Copy link
Copy link
Closed
Labels
acceptedThis proposal is planned.This proposal is planned.enhancementSolving this issue will likely involve adding new logic or components to the codebase.Solving this issue will likely involve adding new logic or components to the codebase.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.This issue suggests language modifications. If it also has the "accepted" label then it is planned.
Milestone
Metadata
Metadata
Assignees
Labels
acceptedThis proposal is planned.This proposal is planned.enhancementSolving this issue will likely involve adding new logic or components to the codebase.Solving this issue will likely involve adding new logic or components to the codebase.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.This issue suggests language modifications. If it also has the "accepted" label then it is planned.
Currently the type of
c"aoeu"is*const u8.Instead, the type should indicate that the pointer is null terminated. Here are two ideas to represent that:
*0 const u8*null const u8This type would be implicitly castable to
*const u8. You can explicitly cast the other way, and in debug mode this inserts a safety check to make sure there actually is a null byte there.It should probably work for any type that supports
T == 0orT == null.We want to steer users away from this type and instead use
[]const u8, which includes a pointer and a length. However, we still have to deal with null terminated things from C land, which makes this useful, and some kernel interfaces. For example, we currently have this:Having the
open_cprototype be*0 const u8would make it more type-safe. Further, we could provide anopenfunction that supported either type forpath, and if it happened to be null terminated then it could avoid the stack allocation.We could also make the type of string literals be
[]0 const u8meaning that the pointer value for the slice has a 0 after the last byte. The length would still indicate the memory before the null byte. If you slice this type then the pointer component would change from*0 const u8to*const u8.It would be extra helpful if automatic .h import could identify when a pointer in a function is supposed to be null-terminated, and we could emit a compile error if the user passes a pointer that is not null terminated. I'm not sure how we could detect this automatically though.