Commit 428a2fdedd

Andrew Kelley <andrew@ziglang.org>
2019-08-27 19:59:18
better handle struct depends on itself via optional field
closes #1995
1 parent e1b258f
Changed files (2)
src/analyze.cpp
@@ -1977,6 +1977,10 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
             field->align = field->type_entry->abi_align;
         } else {
             if ((err = type_val_resolve_abi_align(g, field->type_val, &field->align))) {
+                if (g->trace_err != nullptr) {
+                    g->trace_err = add_error_note(g, g->trace_err, field->decl_node,
+                        buf_create_from_str("while checking this field"));
+                }
                 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
                 return err;
             }
@@ -2497,6 +2501,10 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
             field->align = 1;
         } else {
             if ((err = type_val_resolve_abi_align(g, field->type_val, &field->align))) {
+                if (g->trace_err != nullptr) {
+                    g->trace_err = add_error_note(g, g->trace_err, field->decl_node,
+                        buf_create_from_str("while checking this field"));
+                }
                 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
                 return err;
             }
test/compile_errors.zig
@@ -2,6 +2,24 @@ const tests = @import("tests.zig");
 const builtin = @import("builtin");
 
 pub fn addCases(cases: *tests.CompileErrorContext) void {
+    cases.add(
+        "struct depends on itself via optional field",
+        \\const LhsExpr = struct {
+        \\    rhsExpr: ?AstObject,
+        \\};
+        \\const AstObject = union {
+        \\    lhsExpr: LhsExpr,
+        \\};
+        \\export fn entry() void {
+        \\    const lhsExpr = LhsExpr{ .rhsExpr = null };
+        \\    const obj = AstObject{ .lhsExpr = lhsExpr };
+        \\}
+    ,
+        "tmp.zig:1:17: error: struct 'LhsExpr' depends on itself",
+        "tmp.zig:5:5: note: while checking this field",
+        "tmp.zig:2:5: note: while checking this field",
+    );
+
     cases.add(
         "alignment of enum field specified",
         \\const Number = enum {