zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 2fcac141e75598dc0edd357b55c35d468b506672 (tree)
parent 2e0524954405e05d8b2bfa4a15c293f26a4a080d
Author: Matthew Lugg <mlugg@mlugg.co.uk>
Date:   Wed, 17 Jun 2026 12:11:35 +0100

tests: update for new `@bitCast` semantics

Diffstat:
Mdoc/langref/test_packed_structs.zig | 12++----------
Mdoc/langref/test_pointer_casting.zig | 20++++++++++++--------
Mtest/behavior/asm.zig | 36------------------------------------
Mtest/behavior/atomics.zig | 1-
Mtest/behavior/bitcast.zig | 308++++++++++++++++++++++++++++++++++++-------------------------------------------
Mtest/behavior/comptime_memory.zig | 16++++++++++++++++
Mtest/behavior/sizeof_and_typeof.zig | 11-----------
Mtest/behavior/vector.zig | 4++--
Mtest/c_abi/main.zig | 3++-
Mtest/cases/compile_errors/asm_output_type_no_guaranteed_in_memory_layout.zig | 32++++++++++++++++++++++++++++----
Atest/cases/compile_errors/bitCast_extern_struct.zig | 10++++++++++
Dtest/cases/compile_errors/bitCast_to_enum_type.zig | 10----------
Atest/cases/compile_errors/bitCast_vector_of_pointer.zig | 9+++++++++
Mtest/cases/compile_errors/bitCast_with_invalid_array_element_type.zig | 7++-----
Atest/cases/compile_errors/error_in_nested_declaration.zig | 29+++++++++++++++++++++++++++++
Dtest/cases/error_in_nested_declaration.zig | 30------------------------------
16 files changed, 253 insertions(+), 285 deletions(-)

diff --git a/doc/langref/test_packed_structs.zig b/doc/langref/test_packed_structs.zig @@ -26,16 +26,8 @@ fn doTheTest() !void { try expectEqual(0x1, divided.quarter4); const ordered: [2]u8 = @bitCast(full); - switch (native_endian) { - .big => { - try expectEqual(0x12, ordered[0]); - try expectEqual(0x34, ordered[1]); - }, - .little => { - try expectEqual(0x34, ordered[0]); - try expectEqual(0x12, ordered[1]); - }, - } + try expectEqual(0x34, ordered[0]); + try expectEqual(0x12, ordered[1]); } // test diff --git a/doc/langref/test_pointer_casting.zig b/doc/langref/test_pointer_casting.zig @@ -1,18 +1,22 @@ const std = @import("std"); +const native_endian = @import("builtin").target.cpu.arch.endian(); const expectEqual = std.testing.expectEqual; test "pointer casting" { - const bytes align(@alignOf(u32)) = [_]u8{ 0x12, 0x12, 0x12, 0x12 }; + const bytes: [4]u8 align(@alignOf(u32)) = .{ 0x10, 0x20, 0x30, 0x40 }; const u32_ptr: *const u32 = @ptrCast(&bytes); - try expectEqual(0x12121212, u32_ptr.*); - // Even this example is contrived - there are better ways to do the above than - // pointer casting. For example, using a slice narrowing cast: - const u32_value = std.mem.bytesAsSlice(u32, bytes[0..])[0]; - try expectEqual(0x12121212, u32_value); + // Because we directly reinterpreted bytes of memory, the `u32` value we + // load from `u32_ptr` depends on the target endian: + switch (native_endian) { + .little => try expectEqual(0x40302010, u32_ptr.*), + .big => try expectEqual(0x10203040, u32_ptr.*), + } - // And even another way, the most straightforward way to do it: - try expectEqual(0x12121212, @as(u32, @bitCast(bytes))); + // To instead reinterpret the logical bit representation of `bytes` with no + // dependency on the target endian, use `@bitCast`, which always places + // earlier array elements into less-significant bits: + try expectEqual(0x40302010, @as(u32, @bitCast(bytes))); } test "pointer child type" { diff --git a/test/behavior/asm.zig b/test/behavior/asm.zig @@ -209,42 +209,6 @@ test "packed output types (x86_64)" { } } -test "extern output types (x86_64)" { - if (builtin.target.cpu.arch != .x86_64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_c and builtin.os.tag == .windows) return error.SkipZigTest; // MSVC doesn't support inline assembly - if (builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; // https://codeberg.org/ziglang/zig/issues/31531 - - const S = extern struct { x: u32 }; - { - const s: S = asm volatile ("mov $123, %[ret]" - : [ret] "=r" (-> S), - ); - try expect(s.x == 123); - } - { - var s: S = undefined; - asm volatile ("mov $123, %[ret]" - : [ret] "=r" (s), - ); - try expect(s.x == 123); - } - - const U = extern union { x: u32 }; - { - const u: U = asm volatile ("mov $123, %[ret]" - : [ret] "=r" (-> U), - ); - try expect(u.x == 123); - } - { - var u: U = undefined; - asm volatile ("mov $123, %[ret]" - : [ret] "=r" (u), - ); - try expect(u.x == 123); - } -} - test "abi register aliases as clobbers (RISC-V)" { if (!builtin.target.cpu.arch.isRISCV()) return error.SkipZigTest; if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; diff --git a/test/behavior/atomics.zig b/test/behavior/atomics.zig @@ -358,7 +358,6 @@ test "atomics with different types" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - if (builtin.target.cpu.arch.endian() == .big) return error.SkipZigTest; // #24282 try testAtomicsWithType(bool, true, false); diff --git a/test/behavior/bitcast.zig b/test/behavior/bitcast.zig @@ -74,55 +74,6 @@ fn conv_uN(comptime N: usize, x: @Int(.unsigned, N)) @Int(.signed, N) { return @as(@Int(.signed, N), @bitCast(x)); } -test "bitcast uX to bytes" { - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - - const bit_values = [_]usize{ 1, 48, 27, 512, 493, 293, 125, 204, 112 }; - inline for (bit_values) |bits| { - try testBitCast(bits); - try comptime testBitCast(bits); - } -} - -fn testBitCastuXToBytes(comptime N: usize) !void { - - // The location of padding bits in these layouts are technically not defined - // by LLVM, but we currently allow exotic integers to be cast (at comptime) - // to types that expose their padding bits anyway. - // - // This test at least makes sure those bits are matched by the runtime behavior - // on the platforms we target. If the above behavior is restricted after all, - // this test should be deleted. - - const T = @Int(.unsigned, N); - for ([_]T{ 0, ~@as(T, 0) }) |init_value| { - var x: T = init_value; - const bytes = std.mem.asBytes(&x); - - const byte_count = (N + 7) / 8; - switch (native_endian) { - .little => { - var byte_i = 0; - while (byte_i < (byte_count - 1)) : (byte_i += 1) { - try expect(bytes[byte_i] == 0xff); - } - try expect(((bytes[byte_i] ^ 0xff) << -%@as(u3, @truncate(N))) == 0); - }, - .big => { - var byte_i = byte_count - 1; - while (byte_i > 0) : (byte_i -= 1) { - try expect(bytes[byte_i] == 0xff); - } - try expect(((bytes[byte_i] ^ 0xff) << -%@as(u3, @truncate(N))) == 0); - }, - } - } -} - test "nested bitcast" { const S = struct { fn moo(x: isize) !void { @@ -183,39 +134,6 @@ test "@bitCast packed structs at runtime and comptime" { try comptime S.doTheTest(); } -test "@bitCast extern structs at runtime and comptime" { - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; - - const Full = extern struct { - number: u16, - }; - const TwoHalves = extern struct { - half1: u8, - half2: u8, - }; - const S = struct { - fn doTheTest() !void { - var full = Full{ .number = 0x1234 }; - _ = &full; - const two_halves: TwoHalves = @bitCast(full); - switch (native_endian) { - .big => { - try expect(two_halves.half1 == 0x12); - try expect(two_halves.half2 == 0x34); - }, - .little => { - try expect(two_halves.half1 == 0x34); - try expect(two_halves.half2 == 0x12); - }, - } - } - }; - try S.doTheTest(); - try comptime S.doTheTest(); -} - test "bitcast packed struct to integer and back" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; @@ -363,32 +281,12 @@ test "comptime @bitCast packed struct to int and back" { } } -test "comptime bitcast with fields following f80" { - if (true) { - // https://github.com/ziglang/zig/issues/19387 - return error.SkipZigTest; - } - - if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - - const FloatT = extern struct { f: f80, x: u128 align(16) }; - const x: FloatT = .{ .f = 0.5, .x = 123 }; - var x_as_uint: u256 = comptime @as(u256, @bitCast(x)); - _ = &x_as_uint; - - try expect(x.f == @as(FloatT, @bitCast(x_as_uint)).f); - try expect(x.x == @as(FloatT, @bitCast(x_as_uint)).x); -} - test "bitcast vector to integer and back" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - if (builtin.cpu.arch.endian() == .big and builtin.zig_backend == .stage2_llvm) return error.SkipZigTest; var vec: @Vector(16, bool) = @splat(true); vec[1] = false; @@ -524,87 +422,163 @@ test "@bitCast of packed struct of bools all false" { try expect(@as(u8, @as(u4, @bitCast(p))) == 0); } -test "@bitCast of extern struct containing pointer" { - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; // TODO - - const S = struct { - const A = extern struct { - ptr: *const u32, - }; - - const B = extern struct { - ptr: *const i32, - }; +test "@bitCast of packed struct with void field to integer" { + const S = packed struct(u8) { + v: void, + x: u8, - fn doTheTest() !void { - const x: u32 = 123; - var a: A = undefined; - a = .{ .ptr = &x }; - const b: B = @bitCast(a); - try expect(b.ptr.* == 123); + fn doTheTest(x: u8) !void { + // Intentionally using `@as` to avoid RLS which masks the bug + const foo = @as(@This(), .{ .v = {}, .x = x }); + const as_int: u8 = @bitCast(foo); + try expect(as_int == x); } }; - - try S.doTheTest(); - try comptime S.doTheTest(); + try S.doTheTest(123); + try comptime S.doTheTest(123); } -test "@bitCast of extern struct to float" { - if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; - - const S = struct { - const S = extern struct { - x: u16, - y: u16, - }; - fn doTheTest() !void { - var s: S = .{ .x = 0, .y = 0 }; - _ = &s; - const a: f32 = @bitCast(s); - try expect(a == 0); +test "@bitCast vector to array with different element size" { + const static = struct { + fn doTheTest(v: @Vector(4, u5)) !void { + const result: [5]u4 = @bitCast(v); + // See the definition of `v` in the test proper for these values. + try expect(result[0] == 0b0010); + try expect(result[1] == 0b1110); + try expect(result[2] == 0b0101); + try expect(result[3] == 0b0110); + try expect(result[4] == 0b0000); + } + }; + // The strange digit groupings here are to indicate how this maps to `expected` above. + const v: @Vector(4, u5) = .{ + 0b0_0010, + 0b01_111, + 0b110_01, + 0b0000_0, + }; + try static.doTheTest(v); + try comptime static.doTheTest(v); +} + +test "@bitCast packed struct to array of bits" { + const S = packed struct(u16) { + foo: u5, + bar: i7, + baz: u3, + qux: bool, + fn doTheTest(val: @This(), comptime Bits: type) !void { + const bits: Bits = @bitCast(val); + + // foo + try expect(bits[0] == 1); + try expect(bits[1] == 0); + try expect(bits[2] == 0); + try expect(bits[3] == 1); + try expect(bits[4] == 0); + // bar + try expect(bits[5] == 0); + try expect(bits[6] == 1); + try expect(bits[7] == 1); + try expect(bits[8] == 1); + try expect(bits[9] == 1); + try expect(bits[10] == 1); + try expect(bits[11] == 1); + // baz + try expect(bits[12] == 0); + try expect(bits[13] == 1); + try expect(bits[14] == 0); + // qux + try expect(bits[15] == 1); } }; - try S.doTheTest(); - try comptime S.doTheTest(); + const val: S = .{ + .foo = 0b01001, + .bar = -2, + .baz = 0b010, + .qux = true, + }; + + try val.doTheTest(@Vector(16, u1)); + try val.doTheTest([16]u1); + + try comptime val.doTheTest(@Vector(16, u1)); + try comptime val.doTheTest([16]u1); } -test "@bitCast of float to extern struct" { - if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; +test "@bitCast nested arrays of vectors" { + const Src = [2][2]@Vector(4, u5); + const Dest = [5]@Vector(2, u8); - const S = struct { - const S = extern struct { - x: u32, - }; - fn doTheTest() !void { - var a: f32 = -0.0; - _ = &a; - const s: S = @bitCast(a); - try expect(s.x == 0x80000000); + // The strange digit groupings here are to indicate how this maps to the output. + const src: Src = .{ .{ + .{ 0b00011, 0b00_100, 0b11100, 0b0010_1 }, + .{ 0b1_0110, 0b11011, 0b101_10, 0b10101 }, + }, .{ + .{ 0b10101, 0b00_001, 0b01011, 0b0001_0 }, + .{ 0b0_0001, 0b01111, 0b111_10, 0b00001 }, + } }; + + const expected: Dest = .{ + .{ 0b10000011, 0b11110000 }, + .{ 0b01100010, 0b10110111 }, + .{ 0b10101101, 0b00110101 }, + .{ 0b00101100, 0b00010001 }, + .{ 0b10011110, 0b00001111 }, + }; + + const static = struct { + fn doTheTest(src_arg: Src) !void { + const actual: Dest = @bitCast(src_arg); + for (actual, expected) |actual_vec, expected_vec| { + try expect(actual_vec[0] == expected_vec[0]); + try expect(actual_vec[1] == expected_vec[1]); + } } }; - try S.doTheTest(); - try comptime S.doTheTest(); + try static.doTheTest(src); + try comptime static.doTheTest(src); } -test "@bitCast of packed struct with void field to integer" { - const S = packed struct(u8) { - v: void, - x: u8, - - fn doTheTest(x: u8) !void { - // Intentionally using `@as` to avoid RLS which masks the bug - const foo = @as(@This(), .{ .v = {}, .x = x }); - const as_int: u8 = @bitCast(foo); - try expect(as_int == x); +test "@bitCast nested arrays of bool to scalar" { + const static = struct { + fn doTheTest(src: [4][4]bool) !void { + const result: u16 = @bitCast(src); + try expect(result == 0b1100_0101_1010_0011); } }; - try S.doTheTest(123); - try comptime S.doTheTest(123); + const src: [4][4]bool = .{ + .{ true, true, false, false }, // 0b0011 + .{ false, true, false, true }, // 0b1010 + .{ true, false, true, false }, // 0b0101 + .{ false, false, true, true }, // 0b1100 + }; + try static.doTheTest(src); + try comptime static.doTheTest(src); +} + +test "@bitCast deeply nested arrays to scalar" { + const static = struct { + fn doTheTest(src: [2][1][3][5]u4) !void { + const signed: i120 = @bitCast(src); + try expect(signed < 0); // top nibble is 0x8 so sign bit is 1 + const unsigned: u120 = @bitCast(src); + try expect(unsigned == 0x8873B_5BF6F_F4020_0E7AC_1EFED_40F51); + try expect(@as(i120, @bitCast(unsigned)) == signed); + try expect(@as(u120, @bitCast(signed)) == unsigned); + } + }; + const src: [2][1][3][5]u4 = .{ .{.{ + .{ 0x1, 0x5, 0xF, 0x0, 0x4 }, + .{ 0xD, 0xE, 0xF, 0xE, 0x1 }, + .{ 0xC, 0xA, 0x7, 0xE, 0x0 }, + }}, .{.{ + .{ 0x0, 0x2, 0x0, 0x4, 0xF }, + .{ 0xF, 0x6, 0xF, 0xB, 0x5 }, + .{ 0xB, 0x3, 0x7, 0x8, 0x8 }, + }} }; + try static.doTheTest(src); + try comptime static.doTheTest(src); } diff --git a/test/behavior/comptime_memory.zig b/test/behavior/comptime_memory.zig @@ -583,3 +583,19 @@ test "comptime store to extern struct reinterpreted as byte array" { comptime std.debug.assert(val.x == 0); } + +test "reinterpret sentinel-terminated array as packed struct" { + const S = packed struct(u16) { lo: u8, hi: u8 }; + const data: [2:0]u8 = .{ 0x12, 0x34 }; + const ptr: *align(1) const S = @ptrCast(&data); + switch (endian) { + .little => { + try testing.expect(ptr.lo == 0x12); + try testing.expect(ptr.hi == 0x34); + }, + .big => { + try testing.expect(ptr.lo == 0x34); + try testing.expect(ptr.hi == 0x12); + }, + } +} diff --git a/test/behavior/sizeof_and_typeof.zig b/test/behavior/sizeof_and_typeof.zig @@ -151,9 +151,6 @@ test "branching logic inside @TypeOf" { test "@bitSizeOf" { try expect(@bitSizeOf(u2) == 2); try expect(@bitSizeOf(u8) == @sizeOf(u8) * 8); - try expect(@bitSizeOf(struct { - a: u2, - }) == 8); try expect(@bitSizeOf(packed struct { a: u2, }) == 2); @@ -281,14 +278,6 @@ test "@offsetOf zero-bit field" { try expect(@offsetOf(S, "b") == @offsetOf(S, "c")); } -test "@bitSizeOf on array of structs" { - const S = struct { - foo: u64, - }; - - try expectEqual(128, @bitSizeOf([2]S)); -} - test "lazy abi size used in comparison" { const S = struct { a: usize }; var rhs: i32 = 100; diff --git a/test/behavior/vector.zig b/test/behavior/vector.zig @@ -1536,12 +1536,12 @@ test "vector pointer is indexable" { const V = @Vector(2, u32); const x: V = .{ 123, 456 }; - comptime assert(@TypeOf(&(&x)[0]) == *const u32); // validate constness + comptime assert(@typeInfo(@TypeOf(&(&x)[0])).pointer.attrs.@"const"); try expectEqual(@as(u32, 123), (&x)[0]); try expectEqual(@as(u32, 456), (&x)[1]); var y: V = .{ 123, 456 }; - comptime assert(@TypeOf(&(&y)[0]) == *u32); // validate constness + comptime assert(!@typeInfo(@TypeOf(&(&y)[0])).pointer.attrs.@"const"); try expectEqual(@as(u32, 123), (&y)[0]); try expectEqual(@as(u32, 456), (&y)[1]); diff --git a/test/c_abi/main.zig b/test/c_abi/main.zig @@ -17109,7 +17109,8 @@ const byval_tail_callsite_attr = struct { } fn cast(self: MyRect) struct_Rect { - return @bitCast(self); + const ptr: *const struct_Rect = @ptrCast(&self); + return ptr.*; } extern fn c_byval_tail_callsite_attr(struct_Rect) f64; diff --git a/test/cases/compile_errors/asm_output_type_no_guaranteed_in_memory_layout.zig b/test/cases/compile_errors/asm_output_type_no_guaranteed_in_memory_layout.zig @@ -25,14 +25,38 @@ export fn entry4() void { : [_] "=r" (u), ); } +const ES = extern struct { x: u32 }; +export fn entry5() void { + var es: ES = undefined; + asm volatile ("" + : [_] "=r" (es), + ); +} +const EU = extern union { x: u32 }; +export fn entry6() void { + var eu: EU = undefined; + asm volatile ("" + : [_] "=r" (eu), + ); +} // error // -// :4:24: error: invalid inline assembly output type; 'tmp.S' does not have a guaranteed in-memory layout +// :4:24: error: invalid inline assembly output type 'tmp.S' +// :4:24: note: struct types cannot be passed to inline assembly // :1:11: note: struct declared here -// :11:21: error: invalid inline assembly output type; 'tmp.S' does not have a guaranteed in-memory layout +// :11:21: error: invalid inline assembly output type 'tmp.S' +// :11:21: note: struct types cannot be passed to inline assembly // :1:11: note: struct declared here -// :18:24: error: invalid inline assembly output type; 'tmp.U' does not have a guaranteed in-memory layout +// :18:24: error: invalid inline assembly output type 'tmp.U' +// :18:24: note: union types cannot be passed to inline assembly // :15:11: note: union declared here -// :25:21: error: invalid inline assembly output type; 'tmp.U' does not have a guaranteed in-memory layout +// :25:21: error: invalid inline assembly output type 'tmp.U' +// :25:21: note: union types cannot be passed to inline assembly // :15:11: note: union declared here +// :32:21: error: invalid inline assembly output type 'tmp.ES' +// :32:21: note: struct types cannot be passed to inline assembly +// :28:19: note: struct declared here +// :39:21: error: invalid inline assembly output type 'tmp.EU' +// :39:21: note: union types cannot be passed to inline assembly +// :35:19: note: union declared here diff --git a/test/cases/compile_errors/bitCast_extern_struct.zig b/test/cases/compile_errors/bitCast_extern_struct.zig @@ -0,0 +1,10 @@ +const S = extern struct { x: u32 }; +export fn foo(s: S) void { + const as_int: u32 = @bitCast(s); + _ = as_int; +} + +// error +// +// :3:34: error: cannot @bitCast from 'tmp.S' +// :1:18: note: struct declared here diff --git a/test/cases/compile_errors/bitCast_to_enum_type.zig b/test/cases/compile_errors/bitCast_to_enum_type.zig @@ -1,10 +0,0 @@ -export fn entry() void { - const E = enum(u32) { a, b }; - const y: E = @bitCast(@as(u32, 3)); - _ = y; -} - -// error -// -// :3:18: error: cannot @bitCast to 'tmp.entry.E' -// :3:18: note: use @enumFromInt to cast from 'u32' diff --git a/test/cases/compile_errors/bitCast_vector_of_pointer.zig b/test/cases/compile_errors/bitCast_vector_of_pointer.zig @@ -0,0 +1,9 @@ +export fn foo(p: *u32) void { + const vec: @Vector(2, *u32) = .{ p, p }; + const raw: [2]usize = @bitCast(vec); + _ = raw; +} + +// error +// +// :3:36: error: cannot @bitCast from '@Vector(2, *u32)' diff --git a/test/cases/compile_errors/bitCast_with_invalid_array_element_type.zig b/test/cases/compile_errors/bitCast_with_invalid_array_element_type.zig @@ -18,9 +18,6 @@ export fn baz() void { // error // -// :5:29: error: cannot @bitCast from '[1]tmp.foo.S' -// :5:29: note: array element type 'tmp.foo.S' does not have a guaranteed in-memory layout +// :5:42: error: cannot @bitCast from '[1]tmp.foo.S' // :12:19: error: cannot @bitCast to '[1]tmp.bar.S' -// :12:19: note: array element type 'tmp.bar.S' does not have a guaranteed in-memory layout -// :16:21: error: cannot @bitCast from '[1]comptime_int' -// :16:21: note: array element type 'comptime_int' does not have a guaranteed in-memory layout +// :16:45: error: cannot @bitCast from '[1]comptime_int' diff --git a/test/cases/compile_errors/error_in_nested_declaration.zig b/test/cases/compile_errors/error_in_nested_declaration.zig @@ -0,0 +1,29 @@ +const S = struct { + b: u32, + c: i32, + a: struct { + pub fn str(_: @This(), extra: []u32) []i32 { + return @bitCast(extra); + } + }, +}; + +pub export fn entry() void { + var s: S = undefined; + _ = s.a.str(undefined); +} + +const S2 = struct { + a: [*c]anyopaque, +}; + +pub export fn entry2() void { + var s: S2 = undefined; + _ = &s; +} + +// error +// +// :6:20: error: cannot @bitCast to '[]i32' +// :6:20: note: use @ptrCast to cast from '[]u32' +// :17:12: error: indexable pointer to opaque type 'anyopaque' not allowed diff --git a/test/cases/error_in_nested_declaration.zig b/test/cases/error_in_nested_declaration.zig @@ -1,30 +0,0 @@ -const S = struct { - b: u32, - c: i32, - a: struct { - pub fn str(_: @This(), extra: []u32) []i32 { - return @bitCast(extra); - } - }, -}; - -pub export fn entry() void { - var s: S = undefined; - _ = s.a.str(undefined); -} - -const S2 = struct { - a: [*c]anyopaque, -}; - -pub export fn entry2() void { - var s: S2 = undefined; - _ = &s; -} - -// error -// backend=selfhosted,llvm -// -// :6:20: error: cannot @bitCast to '[]i32' -// :6:20: note: use @ptrCast to cast from '[]u32' -// :17:12: error: indexable pointer to opaque type 'anyopaque' not allowed