Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions test/behavior/optional.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
9 changes: 9 additions & 0 deletions test/cases/compile_errors/optional unwrap assign comptime.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub fn main() !void {
comptime {
var foo: ?u8 = null;
foo.? = 10;
}
}
// error
//
// 4:12: error: unable to unwrap null
16 changes: 16 additions & 0 deletions test/cases/safety/optional unwrap assign.zig
Original file line number Diff line number Diff line change
@@ -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