Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error generating C code for match #23589

Open
hedgeg0d opened this issue Jan 26, 2025 · 2 comments
Open

Error generating C code for match #23589

hedgeg0d opened this issue Jan 26, 2025 · 2 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@hedgeg0d
Copy link
Contributor

hedgeg0d commented Jan 26, 2025

Describe the bug

The variable is initialized using the match construct, but an error occurs in the generated C code

Reproduction Steps

const grid_size = 4

fn main() {
	title_parts := match grid_size {
		3 {['Small', '3x3']!}
		4 {['Classic', '4x4']!}
		5 {['Big', '5x5']!}
		else {['Large', '6x6']!}
	}
	println(title_parts[0])
}

Expected Behavior

Variable title_parts should be [2]string containing ['Classic', '4x4']

Current Behavior

Generated C code:

Array_fixed_string_2 title_parts = ((_const_main__grid_size == (3))? ({_SLIT("Small"), _SLIT("3x3")}) : (_const_main__grid_size == (4))? ({_SLIT("Classic"), _SLIT("4x4")}) : (_const_main__grid_size == (5))? ({_SLIT("Big"), _SLIT("5x5")}) : ({_SLIT("Large"), _SLIT("6x6")}));

Error:

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/VFifteen.01JJHEZ9HYVSQPM0NEGQ1VW5HM.tmp.c:13157: error: ';' expected (got "}")
=================================================================

Possible Solution

No response

Additional Information/Context

No response

V version

0.4.9 d9f5112.801600c

Environment details (OS name and version, etc.)

V full version V 0.4.9 d9f5112.801600c
OS linux, "Arch Linux"
Processor 12 cpus, 64bit, little endian, AMD Ryzen 5 7600X 6-Core Processor
Memory 5.02GB/14.74GB
V executable /home/hedgegod/v-master/v
V last modified time 2025-01-26 14:06:36
V home dir OK, value: /home/hedgegod/experiments/v
VMODULES OK, value: /home/hedgegod/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/hedgegod/experiments/VFifteen
Git version git version 2.48.1
V git status weekly.2025.1-127-g801600c0-dirty (1 commit(s) behind V master)
.git/config present true
cc version cc (GCC) 14.2.1 20240910
gcc version gcc (GCC) 14.2.1 20240910
clang version clang version 19.1.7
tcc version tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
tcc git status thirdparty-linux-amd64 0134e9b9
emcc version N/A
glibc version ldd (GNU libc) 2.40

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.

@hedgeg0d hedgeg0d added the Bug This tag is applied to issues which reports bugs. label Jan 26, 2025
Copy link

Connected to Huly®: V_0.6-22015

@kbkpbot
Copy link
Contributor

kbkpbot commented Jan 27, 2025

In C, initializes all elements of array can only be done during its declaration. So match can't work at this case.

A suggest generated c code:

Array_fixed_int_2 title_parts ;
Array_fixed_int_2 title_parts_3 = {2,3};
Array_fixed_int_2 title_parts_4 = {4,5};
Array_fixed_int_2 title_parts_5 = {6,7};
Array_fixed_int_2 title_parts_else = {8,9};

	(
	(_const_main__grid_size == (3))? memcpy(title_parts,title_parts_3,sizeof(title_parts)) : 
	(_const_main__grid_size == (4))? memcpy(title_parts,title_parts_4,sizeof(title_parts)) : 
	(_const_main__grid_size == (5))? memcpy(title_parts,title_parts_5,sizeof(title_parts)) : 
	memcpy(title_parts,title_parts_else,sizeof(title_parts))
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

2 participants