Skip to content
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

([*]E) + N inconsistent with ([*]E)[N..]: compile error "pointer arithmetic requires element type 'void' to have runtime bits" #21409

Open
rohlem opened this issue Sep 14, 2024 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@rohlem
Copy link
Contributor

rohlem commented Sep 14, 2024

Zig Version

0.14.0-dev.617+208baa37c

Steps to Reproduce and Observed Behavior

The langref mentions pointer arithmetic, and contains a test stating in a comment that ptr[start..] == ptr + start.
However, while the semantics seem perfectly clear, the language arbitrarily disallows the second variant if @sizeOf(ptr[0]) == 0 by compile error.
Test case:

test f {
    f(u8); //ok
    f(u0); //compile error
    f(void); //compile error
    f(type); //compile error
}
fn f(comptime E: type) void {
    const array: [10]E = undefined;
    const ptr: [*]const E = &array;
    _ = &array[4]; //always allowed
    _ = &ptr[4]; //always allowed
    _ = ptr[4..]; //always allowed
    _ = ptr + 4; //compile error if @sizeOf(E) == 0
}

compiler output:

.zig:13:13: error: pointer arithmetic requires element type 'u0' to have runtime bits
    _ = ptr + 4; //compile error if @sizeOf(E) == 0
        ~~~~^~~
referenced by:
    decltest.f: .zig:3:6
.zig:13:13: error: pointer arithmetic requires element type 'void' to have runtime bits
    _ = ptr + 4; //compile error if @sizeOf(E) == 0
        ~~~~^~~
.zig:13:13: error: pointer arithmetic requires element type 'type' to have runtime bits
    _ = ptr + 4; //compile error if @sizeOf(E) == 0
        ~~~~^~~

Expected Behavior

Imo the language would be more consistent if we allowed the pointer-arithmetic variants for all element types.
ptr + offset has well-defined semantics exactly equivalent with ptr[offset..].

The fact that the comment in the langref agrees with me made me think this limitation was unintentional.
Now that I wrote this all out I guess it might be seen as a proposal - I'll let someone else decide which label fits better,

@rohlem rohlem added the bug Observed behavior contradicts documented or intended behavior label Sep 14, 2024
@rohlem rohlem changed the title [*] inconsistent with [N]: compile error "pointer arithmetic requires element type 'void' to have runtime bits" ([*]E) + N inconsistent with ([*]E)[N..]: compile error "pointer arithmetic requires element type 'void' to have runtime bits" Sep 14, 2024
@Vexu Vexu added this to the 0.15.0 milestone Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants