| ... | ... | @@ -1431,15 +1431,34 @@ test "struct field has a pointer to an aligned version of itself" { |
| 1431 | 1431 | try expect(&e == e.next); |
| 1432 | 1432 | } |
| 1433 | 1433 | |
| 1434 | | test "struct only referenced from optional parameter/return" { |
| 1434 | test "struct has only one reference" { |
| 1435 | 1435 | const S = struct { |
| 1436 | | fn f(_: ?struct { x: u8 }) void {} |
| 1437 | | fn g() ?struct { x: u8 } { |
| 1436 | fn optionalStructParam(_: ?struct { x: u8 }) void {} |
| 1437 | fn errorUnionStructParam(_: error{}!struct { x: u8 }) void {} |
| 1438 | fn optionalStructReturn() ?struct { x: u8 } { |
| 1438 | 1439 | return null; |
| 1439 | 1440 | } |
| 1441 | fn errorUnionStructReturn() error{Foo}!struct { x: u8 } { |
| 1442 | return error.Foo; |
| 1443 | } |
| 1444 | fn optionalComptimeIntParam(comptime x: ?comptime_int) comptime_int { |
| 1445 | return x.?; |
| 1446 | } |
| 1447 | fn errorUnionComptimeIntParam(comptime x: error{}!comptime_int) comptime_int { |
| 1448 | return x catch unreachable; |
| 1449 | } |
| 1440 | 1450 | }; |
| 1441 | 1451 | |
| 1442 | | const fp: *const anyopaque = &S.f; |
| 1443 | | const gp: *const anyopaque = &S.g; |
| 1444 | | try expect(fp != gp); |
| 1452 | const optional_struct_param: *const anyopaque = &S.optionalStructParam; |
| 1453 | const error_union_struct_param: *const anyopaque = &S.errorUnionStructParam; |
| 1454 | try expect(optional_struct_param != error_union_struct_param); |
| 1455 | |
| 1456 | const optional_struct_return: *const anyopaque = &S.optionalStructReturn; |
| 1457 | const error_union_struct_return: *const anyopaque = &S.errorUnionStructReturn; |
| 1458 | try expect(optional_struct_return != error_union_struct_return); |
| 1459 | |
| 1460 | try expectEqual(@alignOf(struct {}), S.optionalComptimeIntParam(@alignOf(struct {}))); |
| 1461 | try expectEqual(@alignOf(struct { x: u8 }), S.errorUnionComptimeIntParam(@alignOf(struct { x: u8 }))); |
| 1462 | try expectEqual(@sizeOf(struct { x: u16 }), S.optionalComptimeIntParam(@sizeOf(struct { x: u16 }))); |
| 1463 | try expectEqual(@sizeOf(struct { x: u32 }), S.errorUnionComptimeIntParam(@sizeOf(struct { x: u32 }))); |
| 1445 | 1464 | } |