Commit 93367adaa7

LemonBoy <thatlemon@gmail.com>
2019-09-12 23:02:15
Fix assignment to optional payload
Closes #3081
1 parent 0bdc851
Changed files (2)
src
test
stage1
behavior
src/ir.cpp
@@ -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) {
test/stage1/behavior/misc.zig
@@ -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);
+}