mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
Merge fb47425185 into d0ba6642b5
This commit is contained in:
commit
b750c24ee4
3 changed files with 39 additions and 0 deletions
|
|
@ -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| {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
pub fn main() !void {
|
||||||
|
comptime {
|
||||||
|
var foo: ?u8 = null;
|
||||||
|
foo.? = 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// error
|
||||||
|
//
|
||||||
|
// 4:12: error: unable to unwrap null
|
||||||
16
test/cases/safety/optional unwrap assign.zig
Normal file
16
test/cases/safety/optional unwrap assign.zig
Normal 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
|
||||||
Loading…
Add table
Reference in a new issue