| 1 | struct Bar { |
| 2 | anon struct { |
| 3 | foofoo Foo = Foo{'foofoo'} |
| 4 | } |
| 5 | } |
| 6 | |
| 7 | struct Foo { |
| 8 | name string |
| 9 | } |
| 10 | |
| 11 | fn 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. |
| 20 | const hello = 'hello' |
| 21 | |
| 22 | // vfmt off |
| 23 | fn 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 | |
| 31 | fn test_anon_struct_with_const_default_expr() { |
| 32 | assert method_passed_an_anon_struct_arg(name: 'world') == 'hello world!' |
| 33 | } |
| 34 | |