| 1 | // Test for bug #17372: Uninitialized interface type field gives runtime error when accessing it |
| 2 | // Expected behavior: print "nil" instead of causing a runtime crash |
| 3 | |
| 4 | interface Boo {} |
| 5 | |
| 6 | struct Foo { |
| 7 | boo Boo |
| 8 | } |
| 9 | |
| 10 | @[noinit] |
| 11 | struct Bar { |
| 12 | boo Boo |
| 13 | } |
| 14 | |
| 15 | fn test_uninitialized_interface_field_str() { |
| 16 | // Printing uninitialized interface field should return "nil", not crash |
| 17 | s := '${Foo{}.boo}' |
| 18 | assert s == 'nil', 'Expected "nil" for uninitialized interface, got "${s}"' |
| 19 | } |
| 20 | |
| 21 | fn test_uninitialized_interface_field_in_struct_str() { |
| 22 | // Struct containing uninitialized interface should also work |
| 23 | foo := Foo{} |
| 24 | s := '${foo}' |
| 25 | assert s.contains('boo: nil'), 'Expected struct str to contain "boo: nil", got "${s}"' |
| 26 | } |
| 27 | |
| 28 | fn test_noinit_struct_with_interface_field() { |
| 29 | bar := Bar{} |
| 30 | s := '${bar.boo}' |
| 31 | assert s == 'nil', 'Expected "nil" for noinit struct interface field, got "${s}"' |
| 32 | } |
| 33 | |