I am debugging some comptime code. Here is a minimal reproduction:
const std = @import("std");
pub fn sumSlice(slice: []const u8) u32 {
var sum: u32 = 0;
for (slice) |number| {
sum += number;
}
return sum;
}
pub fn makeArray(comptime num_elements: u8) [num_elements]u8 {
var array: [num_elements]u8 = undefined;
for (&array) |*element| {
element.* = 2;
}
return array;
}
test {
var array = makeArray(35);
const sum = comptime sumSlice(&array);
array[0] = 3;
try std.testing.expectEqual(@as(u32, 70), sum);
}
Why can’t I evaluate sumSlice
at comptime?
error: unable to evaluate comptime expression
const sum = comptime sumSlice(&array);
^~~~~~