Add unwrapped optional assignment tests

Zig supports:

foo.? = val

assignment, currently there are no much tests for them, so lets add few.
This commit is contained in:
Mateusz Poliwczak 2025-11-18 16:06:49 +01:00
parent 3f2cf1c002
commit fb47425185
3 changed files with 39 additions and 0 deletions

View file

@ -304,6 +304,20 @@ test "self-referential struct through a slice of optional" {
try expect(n.data == null);
}
test "assigning to an unwrapped optional" {
var foo: ?u8 = 10;
foo.? = 12;
try expect(foo.? == 12);
}
test "assigning to an unwrapped optional in comptime context" {
comptime {
var foo: ?u8 = 10;
foo.? = 12;
try expect(foo.? == 12);
}
}
test "assigning to an unwrapped optional field in an inline loop" {
comptime var maybe_pos_arg: ?comptime_int = null;
inline for ("ab") |x| {

View file

@ -0,0 +1,9 @@
pub fn main() !void {
comptime {
var foo: ?u8 = null;
foo.? = 10;
}
}
// error
//
// 4:12: error: unable to unwrap null

View file

@ -0,0 +1,16 @@
const std = @import("std");
pub fn panic(message: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
if (std.mem.eql(u8, message, "attempt to use null value")) {
std.process.exit(0);
}
std.process.exit(1);
}
pub fn main() !void {
var foo: ?u8 = null;
foo.? = 10; // should fail, since foo is null.
return error.TestFailed;
}
// run
// backend=selfhosted,llvm
// target=x86_64-linux