From 06dd19b7747c08bef4f4fcf582e13445dd0c31f4 Mon Sep 17 00:00:00 2001 From: Jesus Alvarez Date: Sun, 25 Jan 2026 20:55:04 -0800 Subject: [PATCH] cgen: fix unused functions returning fixed arrays of custom structs (fix #26439) (#26440) Co-authored-by: Claude Opus 4.5 --- vlib/v/gen/c/cgen.v | 7 ++++ .../fns/unused_fn_fixed_array_ret_test.v | 37 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 vlib/v/tests/fns/unused_fn_fixed_array_ret_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 5391666ce..d70e0877d 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2029,6 +2029,13 @@ pub fn (mut g Gen) write_array_fixed_return_types() { // unresolved sizes e.g. [unknown_const]int continue } + // Skip if element type is a non-builtin struct that isn't marked as used + // This prevents generating wrapper structs that reference undefined types + elem_sym := g.table.sym(info.elem_type) + if g.pref.skip_unused && elem_sym.kind == .struct && !elem_sym.is_builtin() + && elem_sym.idx !in g.table.used_features.used_syms { + continue + } mut fixed_elem_name := g.styp(info.elem_type.set_nr_muls(0)) if info.elem_type.is_ptr() { fixed_elem_name += '*'.repeat(info.elem_type.nr_muls()) diff --git a/vlib/v/tests/fns/unused_fn_fixed_array_ret_test.v b/vlib/v/tests/fns/unused_fn_fixed_array_ret_test.v new file mode 100644 index 000000000..a5ed30a66 --- /dev/null +++ b/vlib/v/tests/fns/unused_fn_fixed_array_ret_test.v @@ -0,0 +1,37 @@ +// Test that unused functions returning fixed arrays of custom structs +// don't cause C compilation errors when skip_unused is enabled. +// Related to: functions returning [N]CustomStruct where the function is never called. + +struct QuadVertex { + pos [2]f32 + uv [2]f32 +} + +// This function is intentionally never called - it tests that skip_unused +// correctly handles fn_ret ArrayFixed types with custom struct elements. +pub fn make_quad_vertices(x f32, y f32, w f32, h f32) [4]QuadVertex { + return [ + QuadVertex{ + pos: [x, y]! + uv: [f32(0), 0]! + }, + QuadVertex{ + pos: [x + w, y]! + uv: [f32(1), 0]! + }, + QuadVertex{ + pos: [x + w, y + h]! + uv: [f32(1), 1]! + }, + QuadVertex{ + pos: [x, y + h]! + uv: [f32(0), 1]! + }, + ]! +} + +fn test_unused_fn_fixed_array_ret() { + // The test passes if compilation succeeds. + // make_quad_vertices is intentionally not called. + assert true +} -- 2.39.5