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
This commit is contained in:
Andrew Kelley
2018-11-18 19:36:27 -05:00
parent e9b47d960b
commit f8a782fb2e
4 changed files with 346 additions and 161 deletions

View File

@@ -1,6 +1,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 {
@@ -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(
@@ -2422,16 +2477,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
".tmp_source.zig:1:36: error: expected type 'fn(i32) i32', found 'extern fn(i32) i32'",
);
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 {}
@@ -4049,16 +4094,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
".tmp_source.zig:2:14: error: remainder division with 'i32' and 'i32': signed integers and floats must use @rem or @mod",
);
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 {
@@ -4081,16 +4116,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
".tmp_source.zig:4:17: error: division by zero",
);
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 {