| 1 | struct Base { |
| 2 | embedded_field int |
| 3 | } |
| 4 | |
| 5 | struct Data { |
| 6 | Base |
| 7 | field int |
| 8 | } |
| 9 | |
| 10 | type AliasWithShared = shared Data |
| 11 | |
| 12 | struct Holder { |
| 13 | data AliasWithShared |
| 14 | } |
| 15 | |
| 16 | fn field_from_alias(data AliasWithShared) int { |
| 17 | return data.field |
| 18 | } |
| 19 | |
| 20 | fn field_from_alias_pointer(data &AliasWithShared) int { |
| 21 | return data.field |
| 22 | } |
| 23 | |
| 24 | fn embedded_field_from_alias_pointer(data &AliasWithShared) int { |
| 25 | return data.embedded_field |
| 26 | } |
| 27 | |
| 28 | fn option_alias_with_shared() ?AliasWithShared { |
| 29 | return &Data{ |
| 30 | field: 5 |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | fn result_alias_with_shared() !AliasWithShared { |
| 35 | return &Data{ |
| 36 | field: 6 |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | fn test_alias_to_shared_field_access() { |
| 41 | data_with_shared := AliasWithShared(&Data{ |
| 42 | field: 1 |
| 43 | }) |
| 44 | assert data_with_shared.field == 1 |
| 45 | } |
| 46 | |
| 47 | fn test_alias_to_shared_call_arg() { |
| 48 | assert field_from_alias(&Data{ |
| 49 | field: 2 |
| 50 | }) == 2 |
| 51 | } |
| 52 | |
| 53 | fn test_alias_to_shared_struct_init_field() { |
| 54 | holder := Holder{ |
| 55 | data: &Data{ |
| 56 | field: 3 |
| 57 | } |
| 58 | } |
| 59 | assert holder.data.field == 3 |
| 60 | } |
| 61 | |
| 62 | fn test_alias_to_shared_pointer_field_access() { |
| 63 | data := AliasWithShared(&Data{ |
| 64 | field: 4 |
| 65 | }) |
| 66 | assert field_from_alias_pointer(&data) == 4 |
| 67 | } |
| 68 | |
| 69 | fn test_alias_to_shared_pointer_embed_field_access() { |
| 70 | data := AliasWithShared(&Data{ |
| 71 | embedded_field: 7 |
| 72 | }) |
| 73 | assert embedded_field_from_alias_pointer(&data) == 7 |
| 74 | } |
| 75 | |
| 76 | fn test_option_alias_to_shared_cast() { |
| 77 | opt := ?AliasWithShared(&Data{ |
| 78 | field: 5 |
| 79 | }) |
| 80 | data := opt or { |
| 81 | assert false |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | assert data.field == 5 |
| 86 | } |
| 87 | |
| 88 | fn test_option_alias_to_shared_return() { |
| 89 | data := option_alias_with_shared() or { |
| 90 | assert false |
| 91 | return |
| 92 | } |
| 93 | assert data.field == 5 |
| 94 | } |
| 95 | |
| 96 | fn test_result_alias_to_shared_return() { |
| 97 | data := result_alias_with_shared() or { |
| 98 | assert false |
| 99 | return |
| 100 | } |
| 101 | assert data.field == 6 |
| 102 | } |
| 103 | |