From 18cd3aacf737eea9e28b8b0c6d83e3a89d2627a2 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 16 Nov 2026 10:31:01 +0100 Subject: [PATCH] Declare shared and box typedefs before interfaces An interface method may take a `shared T`, so its vtable needs those types declared first. Co-Authored-By: Claude Opus 5.5 --- src/codegen_c.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/codegen_c.rs b/src/codegen_c.rs index d42e3e7..740e47b 100644 --- a/src/codegen_c.rs +++ b/src/codegen_c.rs @@ -182,6 +182,13 @@ fn generate_with(p: &HProgram, tests: Option<&[String]>) -> String { ps.join(", ") )); } + // declared before the interfaces: an interface method may take a `shared T` + for b in &boxes { + g.out.push_str(&format!("typedef struct {0} {0};\n", box_name(*b))); + } + for s in &shareds { + g.out.push_str(&format!("typedef struct {0} {0};\n", c_ty(*s))); + } for (name, methods) in &p.ifaces { let c = c_ty(Ty::Iface(Box::leak(name.clone().into_boxed_str()))); g.out.push_str(&format!("typedef struct {0} {0};\n", c)); @@ -196,12 +203,6 @@ fn generate_with(p: &HProgram, tests: Option<&[String]>) -> String { struct {c} {{ void *data; const struct VT_{c} *vt; }};\n" )); } - for b in &boxes { - g.out.push_str(&format!("typedef struct {0} {0};\n", box_name(*b))); - } - for s in &shareds { - g.out.push_str(&format!("typedef struct {0} {0};\n", c_ty(*s))); - } for s in &shareds { let (Ty::Shared(i) | Ty::Weak(i)) = s else { unreachable!() }; g.out.push_str(&format!("struct {} {{ {} *p; }};\n", c_ty(*s), box_name(**i)));