v2 / vlib / v / tests / structs / anon_struct_with_default_expr_test.v
33 lines · 28 sloc · 679 bytes · 6488041a749df9762348d019c4223908c476f2e2
Raw
1struct Bar {
2 anon struct {
3 foofoo Foo = Foo{'foofoo'}
4 }
5}
6
7struct Foo {
8 name string
9}
10
11fn test_anon_struct_with_default_expr() {
12 bar := Bar{}
13 println(bar.anon.foofoo.name)
14 assert bar.anon.foofoo.name == 'foofoo'
15}
16
17// for issue 20452
18// phenomenon:
19// cgen generates incorrect code when the default value of an anonymous struct field is the const variable.
20const hello = 'hello'
21
22// vfmt off
23fn method_passed_an_anon_struct_arg(arg struct {
24 name string
25 greeting string = hello
26 }) string {
27 return '${arg.greeting} ${arg.name}!'
28}
29// vfmt on
30
31fn test_anon_struct_with_const_default_expr() {
32 assert method_passed_an_anon_struct_arg(name: 'world') == 'hello world!'
33}
34