Hi all, I’ve got the following:
pub const Context = struct {
const Self = @This();
args: [][:0]const u8,
pub fn init(allocator: Allocator) !Self {
const args = try std.process.argsAlloc(allocator);
return Self{ .args = args };
}
pub fn deinit(self: Self, allocator: Allocator) void {
std.process.argsFree(allocator, self.args);
}
};
test "create context" {
const allocator = std.testing.allocator;
const ctx = try Context.init(allocator);
defer ctx.deinit(allocator);
}
which fails with the following error:
expected type '[][:0]const u8', found '[][:0]u8'
on the line where I initialize Context
with return Self{ .args = args };
I’ve read through the documentation and, in the reference it says the following (under Type Coercion: Stricter Qualification):
Values which have the same representation at runtime can be
cast to increase the strictness of the qualifiers, no matter
how nested the qualifiers are:
* const - non-const to const is allowed
So I believe this should work (and that if there’s a good reason this doesn’t work, the documentation should be updated to reflect that). I’ve tried using @constCast
but, annoyingly, that only works for casting const to non-const.
Also, the error mentions this:
mutable '[:0]const u8' would allow illegal const pointers stored to type '[:0]u8'
which I’ve read over and over but I have no idea what it’s trying to say. Can someone smarter than me explain what this error actually means and how I can fix it?
Full error if needed:
test
└─ run test
└─ zig test Debug native 1 errors
src/root.zig:9:23: error: expected type '[][:0]const u8', found '[][:0]u8'
return Self{ .args = args };
~^~~~~~~~~~~
src/root.zig:9:23: note: pointer type child '[:0]u8' cannot cast into pointer type child '[:0]const u8'
src/root.zig:9:23: note: mutable '[:0]const u8' would allow illegal const pointers stored to type '[:0]u8'