That function is called without parameters, so I’m totally lost about what can have happened there. And why it happens only the 4th time it?s called, too…
I have refactored the code, bringing that function out… And this is what I get now:
src/eo.zig:7:16: error: expected type '[]const u8', found '*[]u8'
var count: u32 = 0;
^~~
referenced by:
test.crossover: src/eo.zig:78:9
remaining reference traces hidden; use '-freference-trace' to see all reference traces
What am I missing here? Did I commit some bug error somewhere that’s making the compiler go bonkers?
I don’t think it’s an issue with the Random interface itself. I’ve factored that function to its own file:
const std = @import("std");
const expect = std.testing.expect;
const ourRng = @import("utils.zig").ourRng;
// Crossover operator that combines two strings, cutting them in two random points, interchanging the result
pub fn crossover(allocator: std.mem.Allocator, binary_string_1: *[]u8, binary_string_2: *[]u8, random: std.rand.Random) !void {
var binary_string_1Clone: []u8 = try allocator.dupeZ(u8, binary_string_1);
defer allocator.free(binary_string_1Clone);
var binary_string_2Clone: []u8 = try allocator.dupeZ(u8, binary_string_2);
defer allocator.free(binary_string_2Clone);
var index1 = random.int(u32) % binary_string_1.len;
var index2 = random.int(u32) % binary_string_2.len;
if (index1 > index2) {
var temp = index1;
index1 = index2;
index2 = temp;
}
for (index1..index2) |i| {
binary_string_2[i] = binary_string_1Clone[i];
binary_string_1[i] = binary_string_2Clone[i];
}
}
test "crossover" {
var prng = try ourRng();
var allocator = std.heap.page_allocator;
var binary_string_1 = try allocator.dupeZ(u8, "101010");
defer allocator.free(binary_string_1);
const copy_binary_string_1 = try allocator.dupeZ(u8, "101010");
defer allocator.free(copy_binary_string_1);
var binary_string_2 = try allocator.dupeZ(u8, "010101");
defer allocator.free(binary_string_2);
const copy_binary_string_2 = try allocator.dupeZ(u8, "010101");
defer allocator.free(copy_binary_string_2);
try crossover(allocator, &binary_string_1, &binary_string_2, prng.random());
try expect(copy_binary_string_1.len == binary_string_1.len);
try expect(copy_binary_string_2.len == binary_string_2.len);
try expect(!std.mem.eql(u8, copy_binary_string_1, binary_string_1));
try expect(!std.mem.eql(u8, copy_binary_string_2, binary_string_2));
}
And now it fails here:
src/crossover.zig:2:27: error: expected type '[]const u8', found '*[]u8'
const expect = std.testing.expect;
~~~~~~~~~~~^~~~~~~
referenced by:
test.crossover: src/crossover.zig:40:9
remaining reference traces hidden; use '-freference-trace' to see all reference traces
The only thing that’s constant is the line where it fails, which is this one:
So thanks a lot for the answer, but this smells like a bug, maybe due to the way the parameters are passed to the function? Should value parameters be passed before pointers or something like that?
No, not the parameter order (although it looks more reasonable to do it that way). Passing the prng after the allocator fails in the same place, in the same way.
Changing the content of the line shows the error in the new content of the line. So looks like something is going awry in the compiler, because this does not make any sense.
This error does not make sense.
Looks like a compiler bug.
Which version do you use? (zig version)
Try to reduce the lines that trigger the problem.
Does it compile the 3 first lines? (EDIT: You also need to reference the unused symbols from a dummy test)