Skip to content

cgen: fix struct field init with optional fixed array (fix #23195)#23199

Merged
spytheman merged 2 commits intovlang:masterfrom
yuyi98:fix_struct_field_optional
Dec 18, 2024
Merged

cgen: fix struct field init with optional fixed array (fix #23195)#23199
spytheman merged 2 commits intovlang:masterfrom
yuyi98:fix_struct_field_optional

Conversation

@yuyi98
Copy link
Copy Markdown
Member

@yuyi98 yuyi98 commented Dec 18, 2024

This PR fix struct field init with optional fixed array (fix #23195, fix #23193).

  • Fix struct field init with optional fixed array.
  • Add test.
type Arr = [4]u8

struct Foo {
	bar int
	baz ?Arr
}

fn main() {
	f1 := Foo{
		bar: 1
		baz: Arr([u8(5), 4, 3, 2]!)
	}
	println(f1)
	assert f1.baz as Arr == [u8(5), 4, 3, 2]!

	f2 := Foo{
		bar: 1
		baz: ?Arr(none)
	}
	println(f2)
	assert f2.bar == 1
	assert f2.baz == none

	arr := Arr([u8(5), 4, 3, 2]!)
	f3 := Foo{
		bar: 1
		baz: arr
	}
	println(f3)
	assert f3.bar == 1
	assert f3.baz as Arr == [u8(5), 4, 3, 2]!
}

PS D:\Test\v\tt1> v run .
Foo{
    bar: 1
    baz: Option(    Arr([5, 4, 3, 2]))
}
Foo{
    bar: 1
    baz: Option(none)
}
Foo{
    bar: 1
    baz: Option(    Arr([5, 4, 3, 2]))
}

Huly®: V_0.6-21636

Comment thread vlib/v/tests/structs/struct_field_init_with_fixed_array_opt_test.v
Comment thread vlib/v/gen/c/assign.v
@felipensp
Copy link
Copy Markdown
Member

Excellent, @yuyi98! Thanks.

@spytheman spytheman merged commit e8ee207 into vlang:master Dec 18, 2024
@yuyi98 yuyi98 deleted the fix_struct_field_optional branch December 19, 2024 01:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

C error when assigning fixed array alias to struct optional field Struct optional field takes invalid value for fixed arrays

3 participants