diff options
| author | Veikka Tuominen <git@vexu.eu> | 2022-09-02 20:26:33 +0300 |
|---|---|---|
| committer | Veikka Tuominen <git@vexu.eu> | 2022-09-03 01:04:46 +0300 |
| commit | b83c037f9ffd7a4285de41c95827615fcbdbbf2f (patch) | |
| tree | 25b6f3ce249c9b77603a47647fc9d658980da08a /test | |
| parent | 6aee07c1446f3ce98d326998c887fbca3b7fd945 (diff) | |
| download | zig-b83c037f9ffd7a4285de41c95827615fcbdbbf2f.tar.gz zig-b83c037f9ffd7a4285de41c95827615fcbdbbf2f.zip | |
Sema: only ABI sized packed structs are extern compatible
Diffstat (limited to 'test')
5 files changed, 42 insertions, 75 deletions
diff --git a/test/c_abi/cfuncs.c b/test/c_abi/cfuncs.c index 391e87fc67..439de8bbae 100644 --- a/test/c_abi/cfuncs.c +++ b/test/c_abi/cfuncs.c @@ -86,24 +86,8 @@ struct MedStructMixed { void zig_med_struct_mixed(struct MedStructMixed); struct MedStructMixed zig_ret_med_struct_mixed(); -struct SmallPackedStruct { - uint8_t a: 2; - uint8_t b: 2; - uint8_t c: 2; - uint8_t d: 2; - uint8_t e: 1; -}; - -struct BigPackedStruct { - uint64_t a: 64; - uint64_t b: 64; - uint64_t c: 64; - uint64_t d: 64; - uint8_t e: 8; -}; - -//void zig_small_packed_struct(struct SmallPackedStruct); // #1481 -void zig_big_packed_struct(struct BigPackedStruct); +void zig_small_packed_struct(uint8_t); +void zig_big_packed_struct(__int128); struct SplitStructInts { uint64_t a; @@ -176,13 +160,19 @@ void run_c_tests(void) { } { - struct BigPackedStruct s = {1, 2, 3, 4, 5}; + __int128 s = 0; + s |= 1 << 0; + s |= (__int128)2 << 64; zig_big_packed_struct(s); } { - struct SmallPackedStruct s = {0, 1, 2, 3, 1}; - //zig_small_packed_struct(s); + uint8_t s = 0; + s |= 0 << 0; + s |= 1 << 2; + s |= 2 << 4; + s |= 3 << 6; + zig_small_packed_struct(s); } { @@ -378,42 +368,32 @@ void c_split_struct_mixed(struct SplitStructMixed x) { assert_or_panic(y.c == 1337.0f); } -struct SmallPackedStruct c_ret_small_packed_struct() { - struct SmallPackedStruct s = { - .a = 0, - .b = 1, - .c = 2, - .d = 3, - .e = 1, - }; +uint8_t c_ret_small_packed_struct() { + uint8_t s = 0; + s |= 0 << 0; + s |= 1 << 2; + s |= 2 << 4; + s |= 3 << 6; return s; } -void c_small_packed_struct(struct SmallPackedStruct x) { - assert_or_panic(x.a == 0); - assert_or_panic(x.a == 1); - assert_or_panic(x.a == 2); - assert_or_panic(x.a == 3); - assert_or_panic(x.e == 1); +void c_small_packed_struct(uint8_t x) { + assert_or_panic(((x >> 0) & 0x3) == 0); + assert_or_panic(((x >> 2) & 0x3) == 1); + assert_or_panic(((x >> 4) & 0x3) == 2); + assert_or_panic(((x >> 6) & 0x3) == 3); } -struct BigPackedStruct c_ret_big_packed_struct() { - struct BigPackedStruct s = { - .a = 1, - .b = 2, - .c = 3, - .d = 4, - .e = 5, - }; +__int128 c_ret_big_packed_struct() { + __int128 s = 0; + s |= 1 << 0; + s |= (__int128)2 << 64; return s; } -void c_big_packed_struct(struct BigPackedStruct x) { - assert_or_panic(x.a == 1); - assert_or_panic(x.b == 2); - assert_or_panic(x.c == 3); - assert_or_panic(x.d == 4); - assert_or_panic(x.e == 5); +void c_big_packed_struct(__int128 x) { + assert_or_panic(((x >> 0) & 0xFFFFFFFFFFFFFFFF) == 1); + assert_or_panic(((x >> 64) & 0xFFFFFFFFFFFFFFFF) == 2); } struct SplitStructMixed c_ret_split_struct_mixed() { diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig index 145bbc384a..a34e4eda8f 100644 --- a/test/c_abi/main.zig +++ b/test/c_abi/main.zig @@ -263,37 +263,30 @@ const SmallPackedStruct = packed struct { b: u2, c: u2, d: u2, - e: bool, }; -const c_small_packed_struct: fn (SmallPackedStruct) callconv(.C) void = @compileError("TODO: #1481"); +extern fn c_small_packed_struct(SmallPackedStruct) void; extern fn c_ret_small_packed_struct() SmallPackedStruct; -// waiting on #1481 -//export fn zig_small_packed_struct(x: SmallPackedStruct) void { -// expect(x.a == 0) catch @panic("test failure"); -// expect(x.b == 1) catch @panic("test failure"); -// expect(x.c == 2) catch @panic("test failure"); -// expect(x.d == 3) catch @panic("test failure"); -// expect(x.e) catch @panic("test failure"); -//} +export fn zig_small_packed_struct(x: SmallPackedStruct) void { + expect(x.a == 0) catch @panic("test failure"); + expect(x.b == 1) catch @panic("test failure"); + expect(x.c == 2) catch @panic("test failure"); + expect(x.d == 3) catch @panic("test failure"); +} test "C ABI small packed struct" { - var s = SmallPackedStruct{ .a = 0, .b = 1, .c = 2, .d = 3, .e = true }; - _ = s; //c_small_packed_struct(s); // waiting on #1481 + var s = SmallPackedStruct{ .a = 0, .b = 1, .c = 2, .d = 3 }; + c_small_packed_struct(s); var s2 = c_ret_small_packed_struct(); try expect(s2.a == 0); try expect(s2.b == 1); try expect(s2.c == 2); try expect(s2.d == 3); - try expect(s2.e); } const BigPackedStruct = packed struct { a: u64, b: u64, - c: u64, - d: u64, - e: u8, }; extern fn c_big_packed_struct(BigPackedStruct) void; extern fn c_ret_big_packed_struct() BigPackedStruct; @@ -301,20 +294,14 @@ extern fn c_ret_big_packed_struct() BigPackedStruct; export fn zig_big_packed_struct(x: BigPackedStruct) void { expect(x.a == 1) catch @panic("test failure"); expect(x.b == 2) catch @panic("test failure"); - expect(x.c == 3) catch @panic("test failure"); - expect(x.d == 4) catch @panic("test failure"); - expect(x.e == 5) catch @panic("test failure"); } test "C ABI big packed struct" { - var s = BigPackedStruct{ .a = 1, .b = 2, .c = 3, .d = 4, .e = 5 }; + var s = BigPackedStruct{ .a = 1, .b = 2 }; c_big_packed_struct(s); var s2 = c_ret_big_packed_struct(); try expect(s2.a == 1); try expect(s2.b == 2); - try expect(s2.c == 3); - try expect(s2.d == 4); - try expect(s2.e == 5); } const SplitStructInt = extern struct { diff --git a/test/cases/compile_errors/C_pointer_pointing_to_non_C_ABI_compatible_type_or_has_align_attr.zig b/test/cases/compile_errors/C_pointer_pointing_to_non_C_ABI_compatible_type_or_has_align_attr.zig index 1b9118aa64..1472c7d5ba 100644 --- a/test/cases/compile_errors/C_pointer_pointing_to_non_C_ABI_compatible_type_or_has_align_attr.zig +++ b/test/cases/compile_errors/C_pointer_pointing_to_non_C_ABI_compatible_type_or_has_align_attr.zig @@ -10,5 +10,5 @@ export fn a() void { // target=native // // :3:19: error: C pointers cannot point to non-C-ABI-compatible type 'tmp.Foo' -// :3:19: note: only structs with packed or extern layout are extern compatible +// :3:19: note: only extern structs and ABI sized packed structs are extern compatible // :1:13: note: struct declared here diff --git a/test/cases/compile_errors/function_with_non-extern_non-packed_struct_parameter.zig b/test/cases/compile_errors/function_with_non-extern_non-packed_struct_parameter.zig index 0007a2014e..55ee277641 100644 --- a/test/cases/compile_errors/function_with_non-extern_non-packed_struct_parameter.zig +++ b/test/cases/compile_errors/function_with_non-extern_non-packed_struct_parameter.zig @@ -10,5 +10,5 @@ export fn entry(foo: Foo) void { _ = foo; } // target=native // // :6:17: error: parameter of type 'tmp.Foo' not allowed in function with calling convention 'C' -// :6:17: note: only structs with packed or extern layout are extern compatible +// :6:17: note: only extern structs and ABI sized packed structs are extern compatible // :1:13: note: struct declared here diff --git a/test/cases/compile_errors/function_with_non-extern_non-packed_union_parameter.zig b/test/cases/compile_errors/function_with_non-extern_non-packed_union_parameter.zig index 001d235e18..f848392c90 100644 --- a/test/cases/compile_errors/function_with_non-extern_non-packed_union_parameter.zig +++ b/test/cases/compile_errors/function_with_non-extern_non-packed_union_parameter.zig @@ -10,5 +10,5 @@ export fn entry(foo: Foo) void { _ = foo; } // target=native // // :6:17: error: parameter of type 'tmp.Foo' not allowed in function with calling convention 'C' -// :6:17: note: only unions with packed or extern layout are extern compatible +// :6:17: note: only extern unions and ABI sized packed unions are extern compatible // :1:13: note: union declared here |
