Skip to content

Commit 428a2fd

Browse files
committed
better handle struct depends on itself via optional field
closes #1995
1 parent e1b258f commit 428a2fd

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/analyze.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@ const tests = @import("tests.zig");
22
const builtin = @import("builtin");
33

44
pub 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 {

0 commit comments

Comments
 (0)