Sema: implement coerce_result_ptr for optionals
New AIR instruction: `optional_payload_ptr_set` It's like `optional_payload_ptr` except it sets the non-null bit. When storing to the payload via a result location that is an optional, `optional_payload_ptr_set` is now emitted. There is a new algorithm in `zirCoerceResultPtr` which stores a dummy value through the result pointer into a temporary block, and then pops off the AIR instructions from the temporary block in order to determine how to transform the result location pointer in case any in-between coercions need to happen. Fixes a couple of behavior tests regarding optionals.
This commit is contained in:
@@ -73,3 +73,33 @@ test "optional with void type" {
|
||||
var x = Foo{ .x = null };
|
||||
try expect(x.x == null);
|
||||
}
|
||||
|
||||
test "address of unwrap optional" {
|
||||
const S = struct {
|
||||
const Foo = struct {
|
||||
a: i32,
|
||||
};
|
||||
|
||||
var global: ?Foo = null;
|
||||
|
||||
pub fn getFoo() anyerror!*Foo {
|
||||
return &global.?;
|
||||
}
|
||||
};
|
||||
S.global = S.Foo{ .a = 1234 };
|
||||
const foo = S.getFoo() catch unreachable;
|
||||
try expect(foo.a == 1234);
|
||||
}
|
||||
|
||||
test "nested optional field in struct" {
|
||||
const S2 = struct {
|
||||
y: u8,
|
||||
};
|
||||
const S1 = struct {
|
||||
x: ?S2,
|
||||
};
|
||||
var s = S1{
|
||||
.x = S2{ .y = 127 },
|
||||
};
|
||||
try expect(s.x.?.y == 127);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user