aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2019-08-11 12:00:32 -0400
committerAndrew Kelley <andrew@ziglang.org>2019-08-11 12:01:02 -0400
commit1b83ee78a48a64bef28f12b7b2e263074f88b6b6 (patch)
tree14e7cfb300a16966bc562892c5051ea48020ab64
parent4bd4c5e06d13c35a70d0207b730fb67b83ff6363 (diff)
downloadzig-1b83ee78a48a64bef28f12b7b2e263074f88b6b6.tar.gz
zig-1b83ee78a48a64bef28f12b7b2e263074f88b6b6.zip
allow comptime_int to implicit cast to comptime_float
-rw-r--r--src/ir.cpp3
-rw-r--r--std/math.zig7
-rw-r--r--test/compile_errors.zig8
-rw-r--r--test/stage1/behavior/cast.zig7
4 files changed, 16 insertions, 9 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index 2b096a3383..13348d28c4 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -9713,6 +9713,9 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
bool const_val_is_float = (const_val->type->id == ZigTypeIdFloat || const_val->type->id == ZigTypeIdComptimeFloat);
assert(const_val_is_int || const_val_is_float);
+ if (const_val_is_int && other_type->id == ZigTypeIdComptimeFloat) {
+ return true;
+ }
if (other_type->id == ZigTypeIdFloat) {
if (const_val->type->id == ZigTypeIdComptimeInt || const_val->type->id == ZigTypeIdComptimeFloat) {
return true;
diff --git a/std/math.zig b/std/math.zig
index e10c9329d9..e47021512e 100644
--- a/std/math.zig
+++ b/std/math.zig
@@ -305,6 +305,13 @@ test "math.min" {
testing.expect(@typeOf(result) == i16);
testing.expect(result == -200);
}
+ {
+ const a = 10.34;
+ var b: f32 = 999.12;
+ var result = min(a, b);
+ testing.expect(@typeOf(result) == f32);
+ testing.expect(result == 10.34);
+ }
}
pub fn max(x: var, y: var) @typeOf(x + y) {
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index a4bc2a66f0..437e40900d 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -3226,14 +3226,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
);
cases.add(
- "incompatible number literals",
- \\const x = 2 == 2.0;
- \\export fn entry() usize { return @sizeOf(@typeOf(x)); }
- ,
- "tmp.zig:1:11: error: integer value 2 cannot be implicitly casted to type 'comptime_float'",
- );
-
- cases.add(
"missing function call param",
\\const Foo = struct {
\\ a: i32,
diff --git a/test/stage1/behavior/cast.zig b/test/stage1/behavior/cast.zig
index c243f18088..04c7fa606f 100644
--- a/test/stage1/behavior/cast.zig
+++ b/test/stage1/behavior/cast.zig
@@ -508,7 +508,7 @@ test "peer type resolution: unreachable, null, slice" {
}
test "peer type resolution: unreachable, error set, unreachable" {
- const Error = error {
+ const Error = error{
FileDescriptorAlreadyPresentInSet,
OperationCausesCircularLoop,
FileDescriptorNotRegistered,
@@ -529,3 +529,8 @@ test "peer type resolution: unreachable, error set, unreachable" {
};
expect(transformed_err == error.SystemResources);
}
+
+test "implicit cast comptime_int to comptime_float" {
+ comptime expect(comptime_float(10) == f32(10));
+ expect(2 == 2.0);
+}