aboutsummaryrefslogtreecommitdiff
path: root/test/compile_errors.zig
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-11-18 19:36:27 -0500
committerAndrew Kelley <superjoe30@gmail.com>2018-11-18 19:37:59 -0500
commitf8a782fb2ec63b89fadb409f7378066a26945d3b (patch)
tree363d3428b7b38777b1f328fe0196987b7d986f9f /test/compile_errors.zig
parente9b47d960b81dfc1fd70c5ae663b4b692ab0b19d (diff)
downloadzig-f8a782fb2ec63b89fadb409f7378066a26945d3b.tar.gz
zig-f8a782fb2ec63b89fadb409f7378066a26945d3b.zip
all numbers with comptime known values implicitly cast
to all number types. If the value does not fit, a compile error is emitted. closes #422 closes #1712
Diffstat (limited to 'test/compile_errors.zig')
-rw-r--r--test/compile_errors.zig87
1 files changed, 56 insertions, 31 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index 7339be7fad..45bd6cb40b 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -2,6 +2,61 @@ const tests = @import("tests.zig");
pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add(
+ "cast negative value to unsigned integer",
+ \\comptime {
+ \\ const value: i32 = -1;
+ \\ const unsigned = @intCast(u32, value);
+ \\}
+ \\export fn entry1() void {
+ \\ const value: i32 = -1;
+ \\ const unsigned: u32 = value;
+ \\}
+ ,
+ ".tmp_source.zig:3:36: error: cannot cast negative value -1 to unsigned integer type 'u32'",
+ ".tmp_source.zig:7:27: error: cannot cast negative value -1 to unsigned integer type 'u32'",
+ );
+
+ cases.add(
+ "integer cast truncates bits",
+ \\export fn entry1() void {
+ \\ const spartan_count: u16 = 300;
+ \\ const byte = @intCast(u8, spartan_count);
+ \\}
+ \\export fn entry2() void {
+ \\ const spartan_count: u16 = 300;
+ \\ const byte: u8 = spartan_count;
+ \\}
+ \\export fn entry3() void {
+ \\ var spartan_count: u16 = 300;
+ \\ var byte: u8 = spartan_count;
+ \\}
+ ,
+ ".tmp_source.zig:3:31: error: integer value 300 cannot be implicitly casted to type 'u8'",
+ ".tmp_source.zig:7:22: error: integer value 300 cannot be implicitly casted to type 'u8'",
+ ".tmp_source.zig:11:20: error: expected type 'u8', found 'u16'",
+ );
+
+ cases.add(
+ "comptime implicit cast f64 to f32",
+ \\export fn entry() void {
+ \\ const x: f64 = 16777217;
+ \\ const y: f32 = x;
+ \\}
+ ,
+ ".tmp_source.zig:3:20: error: cast of value 16777217.000000 to type 'f32' loses information",
+ );
+
+ cases.add(
+ "implicit cast from f64 to f32",
+ \\var x: f64 = 1.0;
+ \\var y: f32 = x;
+ \\
+ \\export fn entry() usize { return @sizeOf(@typeOf(y)); }
+ ,
+ ".tmp_source.zig:2:14: error: expected type 'f32', found 'f64'",
+ );
+
+ cases.add(
"exceeded maximum bit width of integer",
\\export fn entry1() void {
\\ const T = @IntType(false, 65536);
@@ -1819,7 +1874,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ if (0) {}
\\}
,
- ".tmp_source.zig:2:9: error: integer value 0 cannot be implicitly casted to type 'bool'",
+ ".tmp_source.zig:2:9: error: expected type 'bool', found 'comptime_int'",
);
cases.add(
@@ -2423,16 +2478,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
);
cases.add(
- "implicit cast from f64 to f32",
- \\const x : f64 = 1.0;
- \\const y : f32 = x;
- \\
- \\export fn entry() usize { return @sizeOf(@typeOf(y)); }
- ,
- ".tmp_source.zig:2:17: error: expected type 'f32', found 'f64'",
- );
-
- cases.add(
"colliding invalid top level functions",
\\fn func() bogus {}
\\fn func() bogus {}
@@ -4050,16 +4095,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
);
cases.add(
- "cast negative value to unsigned integer",
- \\comptime {
- \\ const value: i32 = -1;
- \\ const unsigned = @intCast(u32, value);
- \\}
- ,
- ".tmp_source.zig:3:22: error: attempt to cast negative value to unsigned integer",
- );
-
- cases.add(
"compile-time division by zero",
\\comptime {
\\ const a: i32 = 1;
@@ -4082,16 +4117,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
);
cases.add(
- "compile-time integer cast truncates bits",
- \\comptime {
- \\ const spartan_count: u16 = 300;
- \\ const byte = @intCast(u8, spartan_count);
- \\}
- ,
- ".tmp_source.zig:3:18: error: cast from 'u16' to 'u8' truncates bits",
- );
-
- cases.add(
"@setRuntimeSafety twice for same scope",
\\export fn foo() void {
\\ @setRuntimeSafety(false);