Skip to content

Odd error when using BoundedArray at comptime, but not runtime due to @memcpy #15633

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
euclidianAce opened this issue May 9, 2023 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@euclidianAce
Copy link

Zig Version

0.11.0-dev.3031+f40539e5d

Steps to Reproduce and Observed Behavior

run the following tmp.zig

pub fn main() anyerror!void {
    std.log.info("{}", .{array});
}

const array = blk: {
    var b = std.BoundedArray(u8, 8){};
    b.appendSlice("hello") catch unreachable;
    break :blk b;
};

const std = @import("std");

which produces the following error:

$ zig run tmp.zig
zig-0.11.0-dev/lib/std/bounded_array.zig:248:13: error: element access of non-indexable type 'u8'
            @memcpy(self.slice()[old_len..][0..items.len], items);
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
zig-0.11.0-dev/lib/std/bounded_array.zig:240:43: note: called from here
            self.appendSliceAssumeCapacity(items);
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
tmp.zig:7:18: note: called from here
    b.appendSlice("hello") catch unreachable;
    ~~~~~~~~~~~~~^~~~~~~~~

but moving the comptime block inside of main is fine:

pub fn main() anyerror!void {
    const array = blk: {
        var b = std.BoundedArray(u8, 8){};
        b.appendSlice("hello") catch unreachable;
        break :blk b;
    };

    std.log.info("{}", .{array});
}

const std = @import("std");
$ zig run tmp.zig
info: bounded_array.BoundedArrayAligned(u8,1,8){ .buffer = { 104, 101, 108, 108, 111, 0, 0, 0 }, .len = 5 }

Expected Behavior

The code at compile time to produce the correct value without erroring

@euclidianAce euclidianAce added the bug Observed behavior contradicts documented or intended behavior label May 9, 2023
@jacobly0
Copy link
Member

jacobly0 commented May 12, 2023

Unrelated to the bug, but b.appendSliceAssumeCapacity("hello"); is the usual way to assert that it can't overflow.

Reduction:

const S = struct {
    buffer: [8]u8 = undefined,
    fn set(self: *S, items: []const u8) void {
        @memcpy(self.buffer[0..items.len], items);
    }
};

comptime {
    var s = S{};
    s.set("hello");
}

@Vexu
Copy link
Member

Vexu commented May 13, 2023

Duplicate of #15634

@Vexu
Copy link
Member

Vexu commented May 14, 2023

Sorry, not a duplicate, didn't notice the slice.

@Vexu Vexu reopened this May 14, 2023
@Vexu Vexu added this to the 0.11.0 milestone 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 14, 2023
Vexu added a commit to Vexu/zig that referenced this issue May 15, 2023
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

3 participants