From 57f9405a8fcaec6043d680fa47ae0e98709160c2 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Thu, 28 Jul 2022 15:00:40 +0300 Subject: Sema: validate bitcast operand type --- test/cases/compile_errors/bitCast_to_enum_type.zig | 2 +- .../intToPtr_with_misaligned_address.zig | 10 ++++++++++ .../issue_3818_bitcast_from_parray-slice_to_u16.zig | 19 +++++++++++++++++++ .../stage1/obj/intToPtr_with_misaligned_address.zig | 10 ---------- .../issue_3818_bitcast_from_parray-slice_to_u16.zig | 17 ----------------- 5 files changed, 30 insertions(+), 28 deletions(-) create mode 100644 test/cases/compile_errors/intToPtr_with_misaligned_address.zig create mode 100644 test/cases/compile_errors/issue_3818_bitcast_from_parray-slice_to_u16.zig delete mode 100644 test/cases/compile_errors/stage1/obj/intToPtr_with_misaligned_address.zig delete mode 100644 test/cases/compile_errors/stage1/obj/issue_3818_bitcast_from_parray-slice_to_u16.zig (limited to 'test/cases/compile_errors') diff --git a/test/cases/compile_errors/bitCast_to_enum_type.zig b/test/cases/compile_errors/bitCast_to_enum_type.zig index add531627f..a8fedb7d54 100644 --- a/test/cases/compile_errors/bitCast_to_enum_type.zig +++ b/test/cases/compile_errors/bitCast_to_enum_type.zig @@ -9,4 +9,4 @@ export fn entry() void { // target=native // // :3:24: error: cannot @bitCast to 'tmp.entry.E' -// :3:24: note: use @intToEnum for type coercion +// :3:24: note: use @intToEnum to cast from 'u32' diff --git a/test/cases/compile_errors/intToPtr_with_misaligned_address.zig b/test/cases/compile_errors/intToPtr_with_misaligned_address.zig new file mode 100644 index 0000000000..43f89ab3b5 --- /dev/null +++ b/test/cases/compile_errors/intToPtr_with_misaligned_address.zig @@ -0,0 +1,10 @@ +pub export fn entry() void { + var y = @intToPtr([*]align(4) u8, 5); + _ = y; +} + +// error +// backend=stage2 +// target=native +// +// :2:39: error: pointer type '[*]align(4) u8' requires aligned address diff --git a/test/cases/compile_errors/issue_3818_bitcast_from_parray-slice_to_u16.zig b/test/cases/compile_errors/issue_3818_bitcast_from_parray-slice_to_u16.zig new file mode 100644 index 0000000000..874f015ffb --- /dev/null +++ b/test/cases/compile_errors/issue_3818_bitcast_from_parray-slice_to_u16.zig @@ -0,0 +1,19 @@ +export fn foo1() void { + var bytes = [_]u8{1, 2}; + const word: u16 = @bitCast(u16, bytes[0..]); + _ = word; +} +export fn foo2() void { + var bytes: []const u8 = &[_]u8{1, 2}; + const word: u16 = @bitCast(u16, bytes); + _ = word; +} + +// error +// backend=stage2 +// target=native +// +// :3:42: error: cannot @bitCast from '*[2]u8' +// :3:42: note: use @ptrToInt to cast to 'u16' +// :8:37: error: cannot @bitCast from '[]const u8' +// :8:37: note: use @ptrToInt to cast to 'u16' diff --git a/test/cases/compile_errors/stage1/obj/intToPtr_with_misaligned_address.zig b/test/cases/compile_errors/stage1/obj/intToPtr_with_misaligned_address.zig deleted file mode 100644 index 16b6bf565e..0000000000 --- a/test/cases/compile_errors/stage1/obj/intToPtr_with_misaligned_address.zig +++ /dev/null @@ -1,10 +0,0 @@ -pub fn main() void { - var y = @intToPtr([*]align(4) u8, 5); - _ = y; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:2:13: error: pointer type '[*]align(4) u8' requires aligned address diff --git a/test/cases/compile_errors/stage1/obj/issue_3818_bitcast_from_parray-slice_to_u16.zig b/test/cases/compile_errors/stage1/obj/issue_3818_bitcast_from_parray-slice_to_u16.zig deleted file mode 100644 index cdc1def677..0000000000 --- a/test/cases/compile_errors/stage1/obj/issue_3818_bitcast_from_parray-slice_to_u16.zig +++ /dev/null @@ -1,17 +0,0 @@ -export fn foo1() void { - var bytes = [_]u8{1, 2}; - const word: u16 = @bitCast(u16, bytes[0..]); - _ = word; -} -export fn foo2() void { - var bytes: []const u8 = &[_]u8{1, 2}; - const word: u16 = @bitCast(u16, bytes); - _ = word; -} - -// error -// backend=stage1 -// target=native -// -// tmp.zig:3:42: error: unable to @bitCast from pointer type '*[2]u8' -// tmp.zig:8:32: error: destination type 'u16' has size 2 but source type '[]const u8' has size 16 -- cgit v1.2.3