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