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...@@ -9713,6 +9713,9 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
9713 bool const_val_is_float = (const_val->type->id == ZigTypeIdFloat || const_val->type->id == ZigTypeIdComptimeFloat);9713 bool const_val_is_float = (const_val->type->id == ZigTypeIdFloat || const_val->type->id == ZigTypeIdComptimeFloat);
9714 assert(const_val_is_int || const_val_is_float);9714 assert(const_val_is_int || const_val_is_float);
97159715
9716 if (const_val_is_int && other_type->id == ZigTypeIdComptimeFloat) {
9717 return true;
9718 }
9716 if (other_type->id == ZigTypeIdFloat) {9719 if (other_type->id == ZigTypeIdFloat) {
9717 if (const_val->type->id == ZigTypeIdComptimeInt || const_val->type->id == ZigTypeIdComptimeFloat) {9720 if (const_val->type->id == ZigTypeIdComptimeInt || const_val->type->id == ZigTypeIdComptimeFloat) {
9718 return true;9721 return true;
std/math.zig+7
...@@ -305,6 +305,13 @@ test "math.min" {...@@ -305,6 +305,13 @@ test "math.min" {
305 testing.expect(@typeOf(result) == i16);305 testing.expect(@typeOf(result) == i16);
306 testing.expect(result == -200);306 testing.expect(result == -200);
307 }307 }
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 }
308}315}
309316
310pub fn max(x: var, y: var) @typeOf(x + y) {317pub 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 {...@@ -3225,14 +3225,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
3225 "tmp.zig:3:17: note: value 8 cannot fit into type u3",3225 "tmp.zig:3:17: note: value 8 cannot fit into type u3",
3226 );3226 );
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
3236 cases.add(3228 cases.add(
3237 "missing function call param",3229 "missing function call param",
3238 \\const Foo = struct {3230 \\const Foo = struct {
test/stage1/behavior/cast.zig+6-1
...@@ -508,7 +508,7 @@ test "peer type resolution: unreachable, null, slice" {...@@ -508,7 +508,7 @@ test "peer type resolution: unreachable, null, slice" {
508}508}
509509
510test "peer type resolution: unreachable, error set, unreachable" {510test "peer type resolution: unreachable, error set, unreachable" {
511 const Error = error {511 const Error = error{
512 FileDescriptorAlreadyPresentInSet,512 FileDescriptorAlreadyPresentInSet,
513 OperationCausesCircularLoop,513 OperationCausesCircularLoop,
514 FileDescriptorNotRegistered,514 FileDescriptorNotRegistered,
...@@ -529,3 +529,8 @@ test "peer type resolution: unreachable, error set, unreachable" {...@@ -529,3 +529,8 @@ test "peer type resolution: unreachable, error set, unreachable" {
529 };529 };
530 expect(transformed_err == error.SystemResources);530 expect(transformed_err == error.SystemResources);
531}531}
532
533test "implicit cast comptime_int to comptime_float" {
534 comptime expect(comptime_float(10) == f32(10));
535 expect(2 == 2.0);
536}