diff --git a/src/Sema.zig b/src/Sema.zig index 35f35d8e581d87e16335725a025a636e32ddffdf..1c2c3056a5b10dd3bb5282c7ad74886d070e3efa 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -21184,7 +21184,10 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro const dest_scalar_ty = dest_ty.scalarType(zcu); const operand_scalar_ty = operand_ty.scalarType(zcu); - _ = try sema.checkIntType(block, src, dest_scalar_ty); + switch (dest_scalar_ty.zigTypeTag(zcu)) { + .comptime_int, .int => {}, + else => return sema.fail(block, src, "expected integer result type, found '{f}'", .{dest_scalar_ty.fmt(pt)}), + } try sema.checkFloatType(block, operand_src, operand_scalar_ty); if (sema.resolveValue(operand)) |operand_val| { @@ -21360,7 +21363,10 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro const dest_scalar_ty = dest_ty.scalarType(zcu); const operand_scalar_ty = operand_ty.scalarType(zcu); - try sema.checkFloatType(block, src, dest_scalar_ty); + switch (dest_scalar_ty.zigTypeTag(zcu)) { + .comptime_float, .float => {}, + else => return sema.fail(block, src, "expected float result type, found '{f}'", .{dest_scalar_ty.fmt(pt)}), + } _ = try sema.checkIntType(block, operand_src, operand_scalar_ty); if (sema.resolveValue(operand)) |operand_val| { diff --git a/test/cases/compile_errors/invalid_float_casts.zig b/test/cases/compile_errors/invalid_float_casts.zig index f6d077d8a0d370618f41e04c6e63c51f8c11908c..cc2039cea3b05d2c72d9bd09807bb75ab4b79701 100644 --- a/test/cases/compile_errors/invalid_float_casts.zig +++ b/test/cases/compile_errors/invalid_float_casts.zig @@ -22,6 +22,6 @@ export fn qux() void { // error // // :4:40: error: unable to cast runtime value to 'comptime_float' -// :9:18: error: expected integer type, found 'f32' +// :9:18: error: expected integer result type, found 'f32' // :14:32: error: expected integer type, found 'f32' // :19:29: error: expected float or vector type, found 'u32' diff --git a/test/cases/compile_errors/invalid_int_casts.zig b/test/cases/compile_errors/invalid_int_casts.zig index b2c542d0f7697ac0543b4a59ff05bacfd5ca5693..479390c5c3fb9303b5b76968d5b9b8d6d1367f96 100644 --- a/test/cases/compile_errors/invalid_int_casts.zig +++ b/test/cases/compile_errors/invalid_int_casts.zig @@ -8,6 +8,9 @@ export fn bar() void { _ = &a; _ = @as(u32, @floatFromInt(a)); } +export fn bar2() void { + _ = @as(comptime_int, @floatFromInt(2)); +} export fn baz() void { var a: u32 = 2; _ = &a; @@ -22,6 +25,7 @@ export fn qux() void { // error // // :4:36: error: unable to cast runtime value to 'comptime_int' -// :9:18: error: expected float type, found 'u32' -// :14:32: error: expected float type, found 'u32' -// :19:27: error: expected integer or vector, found 'f32' +// :9:18: error: expected float result type, found 'u32' +// :12:27: error: expected float result type, found 'comptime_int' +// :17:32: error: expected float type, found 'u32' +// :22:27: error: expected integer or vector, found 'f32'