Skip to content

Incorrect void[] casts are generated in cgen #24048

Description

@XiaoPangxie732

Describe the bug

void[] casts are incorrectly generated in cgen

Reproduction Steps

fn main() {
    v := match 12 {
        12 {
            x := if true { 'b' } else { some_func()? }
            println(x)
            x
        }
        else { none }
    }
    println(v)
}

fn some_func() ?string {
    return 'a'
}

Expected Behavior

b

Current Behavior

================== C compilation error (from tcc): ==============
cc: C:/Users/***/AppData/Local/Temp/v_0/test.01JQ7ADWEF0G0N8ZR7H1CDQS5S.tmp.c:3786: warning: implicit declaration of function 'tcc_backtrace'
cc: C:/Users/***/AppData/Local/Temp/v_0/test.01JQ7ADWEF0G0N8ZR7H1CDQS5S.tmp.c:5806: error: declaration of an array of incomplete type elements
...
cc: C:/Users/***/AppData/Local/Temp/v_0/test.01JQ7ADWEF0G0N8ZR7H1CDQS5S.tmp.c:5806: error: declaration of an array of incomplete type elements
(note: the original output was 3 lines long; it was truncated to its first 2 lines + the last line)
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
builder error:
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .

Possible Solution

No response

Additional Information/Context

This C code is generated:

_option_string _t1 = (_option_string){.state=2, .err=_const_none__, .data={EMPTY_STRUCT_INITIALIZATION}};
    
if (12 == (12)) {
    string _t2; /* if prepend */
    if (true) {
        _option_ok(&(string[]) { _SLIT("b") }, (_option*)(&_t2), sizeof(string));
    } else {
        _option_string _t3 = main__some_func();
        if (_t3.state != 0) {
            panic_option_not_set( IError_name_table[_t3.err._typ]._method_msg(_t3.err._object) );
        }
        
         _option_ok(&(string[]) { (*(string*)_t3.data) }, (_option*)(&_t2), sizeof(string));
    }
    string x = _t2;
    println(x);
    _option_ok(&(void[]) { x }, (_option*)(&_t1), sizeof(void));
}
else {
    _t1.state = 2;
    _t1.err = _const_none__;
}_option_string v = _t1;
println(_option_string_str(v));

There is a void[] cast, which is meaningless
Also, this example triggers #24047

V version

V 0.4.10 e968139

Environment details (OS name and version, etc.)

V full version V 0.4.10 725e259.e968139
OS windows, Microsoft Windows 11 Pro 26100 64-bit
Processor 12 cpus, 64bit, little endian, Intel(R) Core(TM) i7-8700K CPU @ 3.70GHz
Memory 2.44GB/15.86GB
V executable E:\Programs\v\v.exe
V last modified time 2025-03-25 18:45:38
V home dir OK, value: E:\Programs\v
VMODULES OK, value: C:\Users***.vmodules
VTMP OK, value: C:\Users***\AppData\Local\Temp\v_0
Current working dir OK, value: ***
Git version git version 2.38.1.windows.1
V git status 0.4.10-39-ge9681394
.git/config present true
cc version N/A
gcc version N/A
clang version N/A
msvc version N/A
tcc version tcc version 0.9.27 (x86_64 Windows)
tcc git status thirdparty-windows-amd64 b425ac82
emcc version N/A
glibc version N/A

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

Labels

BugThis tag is applied to issues which reports bugs.Option TypeBugs/feature requests, that are related to `?Type`.Status: ConfirmedThis bug has been confirmed to be valid by a contributor.Unit: cgenBugs/feature requests, that are related to the default C generating backend.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions