zig icon indicating copy to clipboard operation
zig copied to clipboard

Segfault using anonymous tuple struct

Open FnControlOption opened this issue 1 year ago • 4 comments

Zig Version

0.14.0-dev.367+a57479afc

Steps to Reproduce and Observed Behavior

In the following code, replacing any usage of IntRange with struct { i32, i32 } will result in a segmentation fault:

const std = @import("std");
const debug = std.debug;
const math = std.math;
const sort = std.sort;

const IntRange = struct { i32, i32 };

pub fn main() void {
    const items = [_]IntRange{ // segfaults with struct { i32, i32 }
        .{ 1, 25 },
        .{ 26, 50 },
        .{ 51, 75 },
        .{ 76, 100 },
    };
    const key: i32 = 42;
    const index = sort.binarySearch(IntRange, key, &items, {}, compareFn); // segfaults with struct { i32, i32 }
    debug.print("{?}\n", .{index});
}

fn compareFn(_: void, key: i32, mid_item: struct { i32, i32 }) math.Order {
    const lo, const hi = mid_item;
    return if (key < lo) .lt else if (key > hi) .gt else .eq;
}

Expected Behavior

No segfault

FnControlOption avatar Jul 23 '24 19:07 FnControlOption

looks like a duplicate of https://github.com/ziglang/zig/issues/19497

xdBronch avatar Jul 23 '24 20:07 xdBronch

None of the examples in that issue are giving me a segfault 🤔

#20415 and #20746 look similar to my issue, though

FnControlOption avatar Jul 23 '24 20:07 FnControlOption

interesting, testing myself and yeah youre right. seems @Vexu mightve been wrong about the others being duplicates?

xdBronch avatar Jul 23 '24 20:07 xdBronch

Only the third example in #19497 crashes now but it is different from this. The other two are the same as this one.

Vexu avatar Jul 24 '24 16:07 Vexu