Zig Version
0.10.0
Steps to Reproduce and Observed Behavior
This was found while doing a comptime only advent of code day 1 solution. Seems like the array created in parse() can't be mutated at runtime when trying to sort it. The error goes away if sorting is also done at comptime.
// bug.zig
const std = @import("std");
const input_raw = "1\n2\n3\n4\n5";
pub fn main() !void {
const input = comptime try parse(input_raw);
std.sort.sort(usize, input, {}, std.sort.desc(usize));
}
fn parse(comptime input: []const u8) ![]usize {
@setEvalBranchQuota(input.len * 20);
const len = std.mem.count(u8, input, "\n") + 1;
var totals: [len]usize = undefined;
var i: usize = 0;
var it = std.mem.split(u8, input, "\n");
while (it.next()) |line| : (i += 1) {
if (line.len == 0) continue;
totals[i] = try std.fmt.parseInt(usize, line, 10);
}
return &totals;
}
$ zig run bug.zig
Segmentation fault at address 0x2099b0
~/zig/zig/download/0.10.0/files/lib/std/mem.zig:2856:5: 0x21daba in swap__anon_4059 (bug)
a.* = b.*;
^
~/zig/zig/download/0.10.0/files/lib/std/sort.zig:1153:17: 0x21e090 in swap__anon_4071 (bug)
mem.swap(T, &items[x], &items[y]);
^
~/zig/zig/download/0.10.0/files/lib/std/sort.zig:287:21: 0x212c7e in sort__anon_3483 (bug)
swap(T, sliced_items, context, lessThan, &order, 0, 1);
^
./bug.zig:7:18: 0x211db7 in main (bug)
std.sort.sort(usize, input, {}, std.sort.desc(usize));
^
~/zig/zig/download/0.10.0/files/lib/std/start.zig:606:37: 0x211997 in posixCallMainAndExit (bug)
const result = root.main() catch |err| {
^
~/zig/zig/download/0.10.0/files/lib/std/start.zig:368:5: 0x211421 in _start (bug)
@call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
^
Aborted
Expected Behavior
I expected the sort to succeed.
Zig Version
0.10.0
Steps to Reproduce and Observed Behavior
This was found while doing a comptime only advent of code day 1 solution. Seems like the array created in parse() can't be mutated at runtime when trying to sort it. The error goes away if sorting is also done at comptime.
Expected Behavior
I expected the sort to succeed.