Zig Version
0.12.0-dev.1128+1bbe52107
Steps to Reproduce and Observed Behavior
Assignment of a sentinel-terminated value to a non-sentinel terminated value loses data without warning.
const std = @import("std");
test "tricky" {
// allocate sentinel-terminated
var s = try std.testing.allocator.dupeZ(u8, "foo");
// lose the sentinel with assignment
const T = struct { s: []const u8 };
var t = T{ .s = s };
// [BUG] free won't work due size mismatch
std.testing.allocator.free(t.s);
}
Expected Behavior
Zig tends to prevent mistakes with explicitness rather than barricading unsafe options. The strategy works quite well, even for people who ain't used to such freedom. I think Zig should require @as (or something like that) when assignment loses the sentinel.
var t = T{ .s = @as([]const u8, s) };
Zig Version
0.12.0-dev.1128+1bbe52107
Steps to Reproduce and Observed Behavior
Assignment of a sentinel-terminated value to a non-sentinel terminated value loses data without warning.
Expected Behavior
Zig tends to prevent mistakes with explicitness rather than barricading unsafe options. The strategy works quite well, even for people who ain't used to such freedom. I think Zig should require
@as(or something like that) when assignment loses the sentinel.