authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-27 13:59:18-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-27 13:59:18-04:00
log428a2fdedd1d2d9ce6c7a3b28a379e4484468b9c
treeba01c1cdb18bed90ee9ffe3ac350a2307e1aa6cc
parente1b258f39fcb4fff97a4fcf6ee9db10bb71646cc
signaturelock-open Commit is signed but in an unrecognized format.

better handle struct depends on itself via optional field

closes #1995

2 files changed, 26 insertions(+), 0 deletions(-)

src/analyze.cpp+8
......@@ -1977,6 +1977,10 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
19771977 field->align = field->type_entry->abi_align;
19781978 } else {
19791979 if ((err = type_val_resolve_abi_align(g, field->type_val, &field->align))) {
1980 if (g->trace_err != nullptr) {
1981 g->trace_err = add_error_note(g, g->trace_err, field->decl_node,
1982 buf_create_from_str("while checking this field"));
1983 }
19801984 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
19811985 return err;
19821986 }
......@@ -2497,6 +2501,10 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
24972501 field->align = 1;
24982502 } else {
24992503 if ((err = type_val_resolve_abi_align(g, field->type_val, &field->align))) {
2504 if (g->trace_err != nullptr) {
2505 g->trace_err = add_error_note(g, g->trace_err, field->decl_node,
2506 buf_create_from_str("while checking this field"));
2507 }
25002508 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
25012509 return err;
25022510 }
test/compile_errors.zig+18
......@@ -2,6 +2,24 @@ const tests = @import("tests.zig");
22const builtin = @import("builtin");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add(
6 "struct depends on itself via optional field",
7 \\const LhsExpr = struct {
8 \\ rhsExpr: ?AstObject,
9 \\};
10 \\const AstObject = union {
11 \\ lhsExpr: LhsExpr,
12 \\};
13 \\export fn entry() void {
14 \\ const lhsExpr = LhsExpr{ .rhsExpr = null };
15 \\ const obj = AstObject{ .lhsExpr = lhsExpr };
16 \\}
17 ,
18 "tmp.zig:1:17: error: struct 'LhsExpr' depends on itself",
19 "tmp.zig:5:5: note: while checking this field",
20 "tmp.zig:2:5: note: while checking this field",
21 );
22
523 cases.add(
624 "alignment of enum field specified",
725 \\const Number = enum {