Skip to content

cgen: invalid C cast to array type when or-block returns fixed array constant #26442

@demizer

Description

@demizer

Describe the bug

When an or-block returns a fixed array constant or variable, the C code generator produces invalid C code that attempts to cast to an array type. This causes compilation failure when using GCC 15.2 as the C backend.

Reproduction Steps

const default_arr = [f32(1.0), 2.0, 3.0, 4.0]!

fn get_arr(key string) ?[4]f32 {
	if key == 'valid' {
		return [f32(0.1), 0.2, 0.3, 0.4]!
	}
	return none
}

fn main() {
	result := get_arr('invalid') or { default_arr }
	println(result)
}

Compile with GCC:

v -cc gcc test.v

Expected Behavior

The code should compile successfully with all supported C compilers.

Current Behavior

GCC compilation fails with:

error: cast specifies array type
 8489 |                 memcpy(_t1.data, (Array_fixed_f32_4)_const_main__default_arr, sizeof(Array_fixed_f32_4));
      |                                  ^

The generated C code incorrectly casts a constant/variable to an array type, which is invalid C. The cast (Array_fixed_f32_4) is only valid for compound literals (brace-enclosed initializers), not for existing variables or constants.

Note: TCC accepts this cast, so the bug only manifests when using GCC. Per C99 6.5.4, cast operators shall not specify array types.

Possible Solution

In gen_or_block_stmts(), only apply the compound literal cast for expressions that generate brace-enclosed initializers (ArrayInit, StructInit, or CastExpr wrapping ArrayInit). For constants and variables, omit the cast and pass them directly to memcpy.

Additional Information/Context

The fix needs to distinguish between:

  • or { [1, 2, 3]! }
    • generates {1, 2, 3} which needs cast to become compound literal
  • or { some_const }
    • generates _const_some_const which should not be cast

Environment details (OS name and version, etc.)

V full version V 0.5.0 c3b924c.0b0bc20
OS linux, "Arch Linux" (VM)
Processor 12 cpus, 64bit, little endian, AMD Ryzen Threadripper 7960X 24-Cores
Memory 5.1GB/47GB
V executable /home/jesusa/code/v/v/v
V last modified time 2026-01-25 19:00:59
V home dir OK, value: /home/jesusa/code/v/v
VMODULES OK, value: /home/jesusa/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/jesusa/code/v/vrasta
Git version git version 2.52.0
V git status weekly.2026.03-49-g0b0bc205 (26 commit(s) behind V master)
.git/config present true
cc version cc (GCC) 15.2.1 20260103
gcc version gcc (GCC) 15.2.1 20260103
clang version clang version 21.1.6
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (GNU libc) 2.42

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions