better handle struct depends on itself via optional field

closes #1995
This commit is contained in:
Andrew Kelley 2019-08-27 13:59:18 -04:00
parent e1b258f39f
commit 428a2fdedd
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
2 changed files with 26 additions and 0 deletions

View File

@ -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;
}

View File

@ -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 {