From 428a2fdedd1d2d9ce6c7a3b28a379e4484468b9c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 27 Aug 2019 13:59:18 -0400 Subject: [PATCH] better handle struct depends on itself via optional field closes #1995 --- src/analyze.cpp | 8 ++++++++ test/compile_errors.zig | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/analyze.cpp b/src/analyze.cpp index e0223dd9f..893c8121a 100644 --- 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 index bc2d43706..812b23671 100644 --- a/test/compile_errors.zig +++ b/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 {