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) {...@@ -1977,6 +1977,10 @@ static Error resolve_union_alignment(CodeGen *g, ZigType *union_type) {
1977 field->align = field->type_entry->abi_align;1977 field->align = field->type_entry->abi_align;
1978 } else {1978 } else {
1979 if ((err = type_val_resolve_abi_align(g, field->type_val, &field->align))) {1979 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 }
1980 union_type->data.unionation.resolve_status = ResolveStatusInvalid;1984 union_type->data.unionation.resolve_status = ResolveStatusInvalid;
1981 return err;1985 return err;
1982 }1986 }
...@@ -2497,6 +2501,10 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {...@@ -2497,6 +2501,10 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) {
2497 field->align = 1;2501 field->align = 1;
2498 } else {2502 } else {
2499 if ((err = type_val_resolve_abi_align(g, field->type_val, &field->align))) {2503 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 }
2500 struct_type->data.structure.resolve_status = ResolveStatusInvalid;2508 struct_type->data.structure.resolve_status = ResolveStatusInvalid;
2501 return err;2509 return err;
2502 }2510 }
test/compile_errors.zig+18
...@@ -2,6 +2,24 @@ const tests = @import("tests.zig");...@@ -2,6 +2,24 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub 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
5 cases.add(23 cases.add(
6 "alignment of enum field specified",24 "alignment of enum field specified",
7 \\const Number = enum {25 \\const Number = enum {