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

Bug: Returning and concatenating a zero-length array results in undefined behaviour #8547

Open
MasterQ32 opened this issue Apr 16, 2021 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@MasterQ32
Copy link
Contributor

The following code yields a pretty unexpected error:

comptime {
    @compileLog(wtf() ++ [0]u8{});
}
fn wtf() [0]u8 { return [0]u8 {}; }
./main.zig:4:20: error: use of undefined value here causes undefined behavior
    @compileLog(wtf() ++ [0]u8{});
                   ^
./main.zig:4:23: note: referenced here
    @compileLog(wtf() ++ [0]u8{});

when using a literal:

comptime {
    @compileLog([0]u8{} ++ [0]u8{});
}

it works as expected:

| [0]u8{}
./main.zig:5:5: error: found compile log statement
    @compileLog([0]u8{} ++ [0]u8{});
    ^

I assume it's a bug in the result location semantics that will do weird things with zero-sized return types

@alexnask alexnask added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Apr 16, 2021
@alexnask alexnask added this to the 0.8.0 milestone Apr 16, 2021
@andrewrk andrewrk modified the milestones: 0.8.0, 0.10.0 May 19, 2021
@Hadron67
Copy link
Contributor

Seems also affects empty structs.

fn soor() struct {}{
    return .{};
}

fn hkm(comptime aaa: anytype) void {
    std.debug.print("{}\n", .{aaa});
}

pub fn main() !void {
    // ok
    const b = soor();
    hkm(b);
    // error: use of undefined value here causes undefined behavior
    hkm(comptime soor());
}

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

4 participants