@@ -22,6 +22,10 @@ fn test_local_fixed_array_zero_init_declarations_use_direct_c_arrays() {
2222
2323type Handle = voidptr
2424
25+ struct Empty {}
26+
27+ type EmptyFixed = [2]Empty
28+
2529fn local_score() int {
2630 mut direct := [4]int{}
2731 direct[0] = 1
@@ -31,8 +35,9 @@ fn local_score() int {
3135 handles[0] = voidptr(0)
3236 mut nested := unsafe { [2][3]int{} }
3337 nested[1][2] = 3
38+ empty := EmptyFixed{}
3439 handle_score := if handles[0] == voidptr(0) { 4 } else { 0 }
35- return direct[0] + wrapped[1] + nested[1][2] + handle_score
40+ return direct[0] + wrapped[1] + nested[1][2] + empty.len + handle_score
3641}
3742
3843fn main() {
@@ -46,7 +51,7 @@ fn main() {
4651 assert compile.exit_code == 0 , compile.output
4752 run := os.execute (bin)
4853 assert run.exit_code == 0 , run.output
49- assert run.output.trim_space () == '10 '
54+ assert run.output.trim_space () == '12 '
5055 generated := os.read_file (bin + '.c' ) or { panic (err) }
5156 compact := generated.replace ('\t ' , '' ).replace (' ' , '' ).replace ('\n ' , '' )
5257 assert compact.contains ('intdirect[4]={0};' )
@@ -61,3 +66,32 @@ fn main() {
6166 assert ! generated.contains (' = (Array_fixed_' ), generated
6267 assert ! generated.contains ('Array_fixed_voidptr_32 handles' ), generated
6368}
69+
70+ fn test_struct_field_rejects_pointer_to_fixed_array_alias () {
71+ v3_bin := local_fixed_array_build_v3 ()
72+ src := os.join_path (os.temp_dir (), 'v3_fixed_array_struct_field_${os .getpid ()}.v' )
73+ os.write_file (src, 'module main
74+
75+ type Arr = [2]int
76+
77+ struct Foo {
78+ arr Arr
79+ }
80+
81+ fn main() {
82+ a := Arr{}
83+ foo := Foo{
84+ arr: &a
85+ }
86+ println(foo.arr.len)
87+ }
88+ ' ) or {
89+ panic (err)
90+ }
91+ bin := os.join_path (os.temp_dir (), 'v3_fixed_array_struct_field_${os .getpid ()}' )
92+ compile := os.execute ('${v3_bin } -nocache ${src } -b c -o ${bin }' )
93+ assert compile.exit_code != 0 , compile.output
94+ assert compile.output.contains ('cannot initialize field `arr` with `&Arr`; expected `Arr`' ), compile.output
95+
96+ assert ! compile.output.contains ('C compilation failed' ), compile.output
97+ }
0 commit comments