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

C compilation error in nested array declaration #23818

Open
Linklancien opened this issue Feb 26, 2025 · 3 comments
Open

C compilation error in nested array declaration #23818

Linklancien opened this issue Feb 26, 2025 · 3 comments
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@Linklancien
Copy link

Linklancien commented Feb 26, 2025

Describe the bug

C compilation error

Reproduction Steps

fn main(){
	map_test := [][][]int{len: 10, init: 
		if index == 0 || index == 9{
			[][]int{len: 10, init:[1]}
		}
		else{
			[][]int{len: 10, init:
					if index == 0 || index == 9{[1]}else{[]}
				}
		}
	}
}

Expected Behavior

Suppose to create triple nest array [][][] using nested conditions

Current Behavior

C compilation error

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.9 81fd0db.a1f7213

Environment details (OS name and version, etc.)

windows, Microsoft Windows 10 Pro 19045 64-bit

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.

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

Connected to Huly®: V_0.6-22226

@jorgeluismireles
Copy link

If I understood your idea, I think here is a workaround

fn main() {
	for r in works() {
		println(r)
	}
	//for r in fails() {
	//	println(r)
	//}
}

fn works() [][][]int {
	return [][][]int{ len: 10, init: if index == 0 || index == 9 {
			[][]int{ len:10, init:[1] }
		} else {
			[ []int{ len:1, init:1 }, []int{}, []int{}, []int{}, []int{}, []int{}, []int{}, []int{}, []int{}, []int{ len:1, init:1 } ]
		}
	}
}

fn fails() [][][]int {
	return [][][]int{ len: 10, init: if index == 0 || index == 9 {
			[][]int{ len: 10, init:[1] }
		} else{
			[][]int{ len: 10, init: if index == 0 || index == 9 {
					[1]
				} else {
					[]
				}
			}
		}
	}
}

produces:

[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]
[[1], [], [], [], [], [], [], [], [], [1]]
[[1], [], [], [], [], [], [], [], [], [1]]
[[1], [], [], [], [], [], [], [], [], [1]]
[[1], [], [], [], [], [], [], [], [], [1]]
[[1], [], [], [], [], [], [], [], [], [1]]
[[1], [], [], [], [], [], [], [], [], [1]]
[[1], [], [], [], [], [], [], [], [], [1]]
[[1], [], [], [], [], [], [], [], [], [1]]
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]

If I uncomment your code I set in function fn fails() I also got a C error:

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/23818_workaround.01JN2H576VD517T05HQ994S0B6.tmp.c:6955: error: ')' expected (got "_t3")
=================================================================
(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 .

I wonder if special variable index could be used twice

@jorgeluismireles
Copy link

With -show-c-output:

$ v -show-c-output run bugs/23818_workaround.v 
======== Output of the C Compiler (/home/jorge/v/thirdparty/tcc/tcc.exe) ========
/tmp/v_1000/23818_workaround.01JN2HFFAKAA5RGQWGT0NRM877.tmp.c:6955: error: ')' expected (got "_t3")
=================================================================================
======== Output of the C Compiler (cc) ========
/tmp/v_1000/23818_workaround.01JN2HFFAKAA5RGQWGT0NRM877.tmp.c: In function ‘main__fails’:
/tmp/v_1000/23818_workaround.01JN2HFFAKAA5RGQWGT0NRM877.tmp.c:6955:256: error: expected ‘)’ before ‘_t3’
 6955 |                         *pelem = (index == 0 || index == 9 ? (__new_array_with_array_default(10, 0, sizeof(Array_int), (Array_int[]){new_array_from_c_array_noscan(1, 1, sizeof(int), _MOV((int[1]){1}))}[0], 0)) : (                           Array_Array_int _t3 =__new_array_with_array_default(10, 0, sizeof(Array_int), (Array_int[]){__new_array_noscan(0, 0, sizeof(int))}[0], 0);
      |                                                                                                                                                                                                                     ~                                          ^~~~
      |                                                                                                                                                                                                                                                                )
/tmp/v_1000/23818_workaround.01JN2HFFAKAA5RGQWGT0NRM877.tmp.c:6963:38: error: expected expression before ‘)’ token
 6963 |                                  _t3));
      |                                      ^
===============================================
No such file or directory; code: 2

@felipensp felipensp added Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Mar 1, 2025
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. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

No branches or pull requests

3 participants