Skip to content

Compiler crash when @memsetting member array #15634

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

Closed
Srekel opened this issue May 9, 2023 · 4 comments
Closed

Compiler crash when @memsetting member array #15634

Srekel opened this issue May 9, 2023 · 4 comments
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@Srekel
Copy link

Srekel commented May 9, 2023

Zig Version

0.11.0-dev.3031+f40539e5d

Steps to Reproduce and Observed Behavior

This is on Windows 10.

zig init

main.zig:

pub const IdLocal = struct {
    str: [191]u8 = .{0} ** 191,

    pub fn clear(self: *IdLocal) void {
        // @memset(self.str[0..], 0); <--- this is correct and works
        @memset(self.str, 0); // <--- this yields "exit code 1"
    }
};

pub fn main() !void {
    var lol: IdLocal = undefined;
    lol.clear();
}

zig build:

PS E:\Projects\zigtesting> zig build
zig build-exe zigtesting Debug native: error: the following command exited with error code 3:
E:\Programs\zig\zig.exe build-exe E:\Projects\zigtesting\src\main.zig --cache-dir E:\Projects\zigtesting\zig-cache --global-cache-dir C:\Users\Srekel\AppData\Local\zig --name zigtesting --listen=-
Build Summary: 0/3 steps succeeded; 1 failed (disable with -fno-summary)
install transitive failure
+- install zigtesting transitive failure
+- zig build-exe zigtesting Debug native failure
error: the following build command failed with exit code 1:
E:\Projects\zigtesting\zig-cache\o\d234a84f47ea5d41edd005b7e6d7c6fb\build.exe E:\Programs\zig\zig.exe E:\Projects\zigtesting E:\Projects\zigtesting\zig-cache C:\Users\Srekel\AppData\Local\zig
PS E:\Projects\zigtesting>

Expected Behavior

PS E:\Projects\zigtesting> zig build
PS E:\Projects\zigtesting>

@Srekel Srekel added the bug Observed behavior contradicts documented or intended behavior label May 9, 2023
@Vexu Vexu added this to the 0.12.0 milestone May 9, 2023
@hqhs
Copy link

hqhs commented May 9, 2023

The platform doesn't matter. I reproduced this on osx with a bit smaller example:

pub fn main() !void {
    var s = [_]u8{0} ** 191;
    // @memset(&s, 0); // works
    @memset(s, 0); // doesn't
}

@memset destination correctness check happens in Sema.zig zirMemset.

My guess the line try checkIndexable(sema, block, dest_src, dest_ptr_ty);
is not enough. It probably was intended for for loops checking only, e.g., the following code crashes the compiler too:


pub fn main() !void {
    const tuple_s = .{ @as(usize, 2), @as(usize, 4) };
    @memset(&tuple_s, @as(usize, 5));
}

Since tuples are indexable.

From the docs:

dest must be a mutable slice or a mutable pointer to an array. It may have any alignment, and it may have any element type.

In the cases above, dest is indexable, but it's not a pointer to an array.

If the doc is correct and the compiler isn't, then the fix is to restrict type checking of dest to .Pointers only.

@hqhs
Copy link

hqhs commented May 10, 2023

I created a small PR with the possible fix: #15638

@hqhs
Copy link

hqhs commented May 10, 2023

The alternative fix would be to properly implement memset for all remaining indexable containers like proposed here #15479

@Vexu Vexu modified the milestones: 0.12.0, 0.11.0 May 10, 2023
@Vexu Vexu linked a pull request May 10, 2023 that will close this issue
@jacobly0
Copy link
Member

jacobly0 commented May 12, 2023

Related #15633 (similar issue, but with @memcpy)

Vexu added a commit to Vexu/zig that referenced this issue May 14, 2023
Vexu added a commit to Vexu/zig that referenced this issue May 14, 2023
Vexu added a commit to Vexu/zig that referenced this issue May 14, 2023
Vexu added a commit to Vexu/zig that referenced this issue May 14, 2023
Vexu added a commit to Vexu/zig that referenced this issue May 15, 2023
TUSF pushed a commit to TUSF/zig that referenced this issue May 9, 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

Successfully merging a pull request may close this issue.

4 participants