commit 428a2fdedd1d2d9ce6c7a3b28a379e4484468b9c (tree)
parent e1b258f39fcb4fff97a4fcf6ee9db10bb71646cc
Author: Andrew Kelley <andrew@ziglang.org>
Date: Tue, 27 Aug 2019 13:59:18 -0400
better handle struct depends on itself via optional field
closes #1995
Diffstat:
2 files changed, 26 insertions(+), 0 deletions(-)
diff --git a/src/analyze.cpp b/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;
}
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
@@ -3,6 +3,24 @@ 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 {
\\ a,