This commit is contained in:
Mateusz Poliwczak 2025-11-25 14:59:56 +01:00 committed by GitHub
commit b750c24ee4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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); 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" { test "assigning to an unwrapped optional field in an inline loop" {
comptime var maybe_pos_arg: ?comptime_int = null; comptime var maybe_pos_arg: ?comptime_int = null;
inline for ("ab") |x| { 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