authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-11 12:00:32-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-11 12:01:02-04:00
log1b83ee78a48a64bef28f12b7b2e263074f88b6b6
tree14e7cfb300a16966bc562892c5051ea48020ab64
parent4bd4c5e06d13c35a70d0207b730fb67b83ff6363
signaturelock-open Commit is signed but in an unrecognized format.

allow comptime_int to implicit cast to comptime_float


4 files changed, 16 insertions(+), 9 deletions(-)

src/ir.cpp+3
......@@ -9713,6 +9713,9 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
97139713 bool const_val_is_float = (const_val->type->id == ZigTypeIdFloat || const_val->type->id == ZigTypeIdComptimeFloat);
97149714 assert(const_val_is_int || const_val_is_float);
97159715
9716 if (const_val_is_int && other_type->id == ZigTypeIdComptimeFloat) {
9717 return true;
9718 }
97169719 if (other_type->id == ZigTypeIdFloat) {
97179720 if (const_val->type->id == ZigTypeIdComptimeInt || const_val->type->id == ZigTypeIdComptimeFloat) {
97189721 return true;
std/math.zig+7
......@@ -305,6 +305,13 @@ test "math.min" {
305305 testing.expect(@typeOf(result) == i16);
306306 testing.expect(result == -200);
307307 }
308 {
309 const a = 10.34;
310 var b: f32 = 999.12;
311 var result = min(a, b);
312 testing.expect(@typeOf(result) == f32);
313 testing.expect(result == 10.34);
314 }
308315}
309316
310317pub fn max(x: var, y: var) @typeOf(x + y) {
test/compile_errors.zig-8
......@@ -3225,14 +3225,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
32253225 "tmp.zig:3:17: note: value 8 cannot fit into type u3",
32263226 );
32273227
3228 cases.add(
3229 "incompatible number literals",
3230 \\const x = 2 == 2.0;
3231 \\export fn entry() usize { return @sizeOf(@typeOf(x)); }
3232 ,
3233 "tmp.zig:1:11: error: integer value 2 cannot be implicitly casted to type 'comptime_float'",
3234 );
3235
32363228 cases.add(
32373229 "missing function call param",
32383230 \\const Foo = struct {
test/stage1/behavior/cast.zig+6-1
......@@ -508,7 +508,7 @@ test "peer type resolution: unreachable, null, slice" {
508508}
509509
510510test "peer type resolution: unreachable, error set, unreachable" {
511 const Error = error {
511 const Error = error{
512512 FileDescriptorAlreadyPresentInSet,
513513 OperationCausesCircularLoop,
514514 FileDescriptorNotRegistered,
......@@ -529,3 +529,8 @@ test "peer type resolution: unreachable, error set, unreachable" {
529529 };
530530 expect(transformed_err == error.SystemResources);
531531}
532
533test "implicit cast comptime_int to comptime_float" {
534 comptime expect(comptime_float(10) == f32(10));
535 expect(2 == 2.0);
536}