Fix assignment to optional payload

Closes #3081
master
LemonBoy 2019-09-12 23:02:15 +02:00 committed by Andrew Kelley
parent 0bdc85181c
commit 93367adaa7
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
2 changed files with 15 additions and 1 deletions

View File

@ -15487,6 +15487,7 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
if (actual_elem_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional &&
value_type->id != ZigTypeIdNull)
{
result_loc_pass1->written = false;
return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, result_loc, false, true);
} else if (actual_elem_type->id == ZigTypeIdErrorUnion && value_type->id != ZigTypeIdErrorUnion) {
if (value_type->id == ZigTypeIdErrorSet) {

View File

@ -488,7 +488,7 @@ test "@typeName" {
expect(mem.eql(u8, @typeName(i64), "i64"));
expect(mem.eql(u8, @typeName(*usize), "*usize"));
// https://github.com/ziglang/zig/issues/675
expectEqualSlices(u8, "behavior.misc.TypeFromFn(u8)", @typeName(TypeFromFn(u8)));
expect(mem.eql(u8, "behavior.misc.TypeFromFn(u8)", @typeName(TypeFromFn(u8))));
expect(mem.eql(u8, @typeName(Struct), "Struct"));
expect(mem.eql(u8, @typeName(Union), "Union"));
expect(mem.eql(u8, @typeName(Enum), "Enum"));
@ -741,3 +741,16 @@ test "peer result location with typed parent, runtime condition, comptime prongs
expect(S.doTheTest(0) == 1234);
expect(S.doTheTest(1) == 1234);
}
test "nested optional field in struct" {
const S2 = struct {
y: u8,
};
const S1 = struct {
x: ?S2,
};
var s = S1{
.x = S2{ .y = 127 },
};
expect(s.x.?.y == 127);
}