Skip to content

Commit feb2f1d

Browse files
committed
Add unwrapped optional assignment tests
Zig supports: foo.? = val assignment, currently there are no much tests for them, so lets add few.
1 parent 4ea4728 commit feb2f1d

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

test/behavior/optional.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,20 @@ test "self-referential struct through a slice of optional" {
304304
try expect(n.data == null);
305305
}
306306

307+
test "assigning to an unwrapped optional" {
308+
var foo: ?u8 = 10;
309+
foo.? = 12;
310+
try expect(foo.? == 12);
311+
}
312+
313+
test "assigning to an unwrapped optional in comptime context" {
314+
comptime {
315+
var foo: ?u8 = 10;
316+
foo.? = 12;
317+
try expect(foo.? == 12);
318+
}
319+
}
320+
307321
test "assigning to an unwrapped optional field in an inline loop" {
308322
comptime var maybe_pos_arg: ?comptime_int = null;
309323
inline for ("ab") |x| {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub fn main() !void {
2+
comptime {
3+
var foo: ?u8 = null;
4+
foo.? = 10;
5+
}
6+
}
7+
// error
8+
//
9+
// 4:12: error: unable to unwrap null
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const std = @import("std");
2+
3+
pub fn panic(message: []const u8, _: ?*std.builtin.StackTrace, _: ?usize) noreturn {
4+
if (std.mem.eql(u8, message, "attempt to use null value")) {
5+
std.process.exit(0);
6+
}
7+
std.process.exit(1);
8+
}
9+
pub fn main() !void {
10+
var foo: ?u8 = null;
11+
foo.? = 10; // should fail, since foo is null.
12+
return error.TestFailed;
13+
}
14+
// run
15+
// backend=selfhosted,llvm
16+
// target=x86_64-linux

0 commit comments

Comments
 (0)