From 56908dcb9dd7bbfae7c22b6312752eb576a227c2 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 9 May 2016 08:44:41 -0700 Subject: add debug safety for shortening casts closes #150 --- test/run_tests.cpp | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'test/run_tests.cpp') diff --git a/test/run_tests.cpp b/test/run_tests.cpp index f4f958b911..aee5fe17fe 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -1368,6 +1368,20 @@ fn add(x: i8w, y: i32) { } )SOURCE", 1, ".tmp_source.zig:3:17: error: incompatible types: 'i8w' and 'i32'"); + add_compile_fail_case("truncate sign mismatch", R"SOURCE( +fn f() { + const x: u32 = 10; + @truncate(i8, x); +} + )SOURCE", 1, ".tmp_source.zig:4:19: error: expected signed integer type, got 'u32'"); + + add_compile_fail_case("truncate same bit count", R"SOURCE( +fn f() { + const x: i8 = 10; + @truncate(i8, x); +} + )SOURCE", 1, ".tmp_source.zig:4:19: error: type 'i8' has same or fewer bits than destination type 'i8'"); + } ////////////////////////////////////////////////////////////////////////////// @@ -1476,6 +1490,26 @@ fn widen_slice(slice: []u8) -> []i32 { } )SOURCE"); + add_debug_safety_case("value does not fit in shortening cast", R"SOURCE( +pub fn main(args: [][]u8) -> %void { + shorten_cast(200); +} +#static_eval_enable(false) +fn shorten_cast(x: i32) -> i8 { + i8(x) +} + )SOURCE"); + + add_debug_safety_case("signed integer not fitting in cast to unsigned integer", R"SOURCE( +pub fn main(args: [][]u8) -> %void { + unsigned_cast(-10); +} +#static_eval_enable(false) +fn unsigned_cast(x: i32) -> u32 { + u32(x) +} + )SOURCE"); + } ////////////////////////////////////////////////////////////////////////////// -- cgit v1.2.3