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

Comptime slice of undefined zero-sized array pointer causes compile error #6936

Closed
SpexGuy opened this issue Nov 2, 2020 · 5 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@SpexGuy
Copy link
Contributor

SpexGuy commented Nov 2, 2020

// repro.zig
comptime { const y = @as([*]void, undefined)[0..4]; }
<source>:1:45: error: out of bounds slice
comptime { const y = @as([*]void, undefined)[0..4]; }
                                            ^

@as([*]void, undefined) yields a defined instance of [*]void, since zero-sized values cannot be undefined. Since the stride is zero, a slice of this value is the same size as the value, so this is not out of bounds.

@SpexGuy SpexGuy added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Nov 3, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 Nov 3, 2020
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@mikdusan
Copy link
Member

mikdusan commented Jan 7, 2023

With latest compiler this returns a different error. Can we consider this issue fixed?

repro.zig
comptime {
    const y = @as([*]void, undefined)[0..4];
    _ = y;
}
$ uname -a
Linux archlinux 6.1.2-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 31 Dec 2022 17:40:35 +0000 x86_64 GNU/Linux

$ zig version
0.11.0-dev.1247+87b223428

zig build-obj repro.zig
repro.zig:2:38: error: non-zero length slice of undefined pointer
    const y = @as([*]void, undefined)[0..4];
              ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~

@nektro
Copy link
Contributor

nektro commented Jan 8, 2023

this is a stage2 issue with it producing an error when end is comptime known. the following passes as intended

test {
    var x: usize = 4;
    const y = @as([*]void, undefined)[0..x];
    _ = y;
}

@nektro
Copy link
Contributor

nektro commented Jan 8, 2023

related: #6706

@Vexu
Copy link
Member

Vexu commented Jan 9, 2023

That error is unrelated to zero-bit types and seems quite intentional (72ca2b2) though I'm unsure why it was added and whether it is still necessary.

@Vexu
Copy link
Member

Vexu commented Mar 28, 2024

Errors correctly in master.

@Vexu Vexu closed this as completed Mar 28, 2024
@Vexu Vexu modified the milestones: 0.15.0, 0.12.0 Mar 28, 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 stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

5 participants