Skip to content

_option_ functions are used on non-option types in cgen #24047

Description

@XiaoPangxie732

Describe the bug

_option_ functions are used on non-option types in cgen

Reproduction Steps

fn main() {
    some_func2(1)?
}

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

fn some_func2(i int) ?string {
    return match i {
        1 {
            x := if true { 'b' } else { some_func()? }
            println(x)
            'b'
        }
        else { none }
    }
}

Expected Behavior

b

Current Behavior

println(NIL)

Possible Solution

No response

Additional Information/Context

This C code is generated for the above code:

string _t3; /* if prepend */
if (true) {
	_option_ok(&(string[]) { _SLIT("b") }, (_option*)(&_t3), sizeof(string));
} else {
	_option_string _t4 = main__some_func();
	if (_t4.state != 0) {
		_option_string _t5;
		memcpy(&_t5, &_t4, sizeof(_option));
		return _t5;
	}
	
 	_option_ok(&(string[]) { (*(string*)_t4.data) }, (_option*)(&_t3), sizeof(string));
}

_t3 here is of type string, is passed to _option_ok, which only accepts option types

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