Skip to content
This repository was archived by the owner on Nov 26, 2025. It is now read-only.

Commit e1bb6da

Browse files
committed
fix invalid translation when unnamed typedef references itself
1 parent efc6480 commit e1bb6da

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

src/Translator.zig

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,9 @@ fn maybeSuppressResult(t: *Translator, used: ResultUsed, result: ZigNode) TransE
140140

141141
pub fn addTopLevelDecl(t: *Translator, name: []const u8, decl_node: ZigNode) !void {
142142
const gop = try t.global_scope.sym_table.getOrPut(t.gpa, name);
143-
if (!gop.found_existing) {
144-
gop.value_ptr.* = decl_node;
145-
try t.global_scope.nodes.append(t.gpa, decl_node);
146-
}
143+
if (gop.found_existing) return; // Any duplicate decls are equivalent
144+
gop.value_ptr.* = decl_node;
145+
try t.global_scope.nodes.append(t.gpa, decl_node);
147146
}
148147

149148
fn fail(
@@ -317,10 +316,12 @@ fn prepopulateGlobalNameTable(t: *Translator) !void {
317316
const gop = try t.unnamed_typedefs.getOrPut(t.gpa, base.qt);
318317
if (gop.found_existing) {
319318
// One typedef can declare multiple names.
320-
// TODO Don't put this one in `decl_table` so it's processed later.
319+
// Don't put this one in `decl_table` so it's processed later.
321320
continue;
322321
}
323322
gop.value_ptr.* = decl_name;
323+
try t.type_decls.put(t.gpa, decl, decl_name);
324+
try t.typedefs.put(t.gpa, decl_name, {});
324325
},
325326

326327
.struct_decl,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
typedef struct {
2+
struct TypeB* b_member;
3+
} TypeA;
4+
5+
struct TypeB {
6+
TypeA* a_member;
7+
};
8+
9+
// translate
10+
//
11+
// pub const struct_TypeB = extern struct {
12+
// a_member: [*c]TypeA = null,
13+
// };
14+
// pub const TypeA = extern struct {
15+
// b_member: [*c]struct_TypeB = null,
16+
// };

0 commit comments

Comments
 (0)