| author | |
| committer | |
| log | 02c49f2911674a93be7591f3d9b47bd087c0586d |
| tree | 16aef28ebecc94db79b2a2216916cd0666c67368 |
| parent | 94bdde8327608af23771fbca8486c2e5c189fa1e |
Fixes: https://codeberg.org/ziglang/zig/issues/32147
Co-authored-by: rue04 <rue04@rue04.de>
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/35663
Reviewed-by: mlugg <mlugg@mlugg.co.uk>3 files changed, 16 insertions(+), 6 deletions(-)
src/Sema.zig+8-2| ... | ... | @@ -21184,7 +21184,10 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 21184 | 21184 | const dest_scalar_ty = dest_ty.scalarType(zcu); |
| 21185 | 21185 | const operand_scalar_ty = operand_ty.scalarType(zcu); |
| 21186 | 21186 | |
| 21187 | _ = try sema.checkIntType(block, src, dest_scalar_ty); | |
| 21187 | switch (dest_scalar_ty.zigTypeTag(zcu)) { | |
| 21188 | .comptime_int, .int => {}, | |
| 21189 | else => return sema.fail(block, src, "expected integer result type, found '{f}'", .{dest_scalar_ty.fmt(pt)}), | |
| 21190 | } | |
| 21188 | 21191 | try sema.checkFloatType(block, operand_src, operand_scalar_ty); |
| 21189 | 21192 | |
| 21190 | 21193 | if (sema.resolveValue(operand)) |operand_val| { |
| ... | ... | @@ -21360,7 +21363,10 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 21360 | 21363 | const dest_scalar_ty = dest_ty.scalarType(zcu); |
| 21361 | 21364 | const operand_scalar_ty = operand_ty.scalarType(zcu); |
| 21362 | 21365 | |
| 21363 | try sema.checkFloatType(block, src, dest_scalar_ty); | |
| 21366 | switch (dest_scalar_ty.zigTypeTag(zcu)) { | |
| 21367 | .comptime_float, .float => {}, | |
| 21368 | else => return sema.fail(block, src, "expected float result type, found '{f}'", .{dest_scalar_ty.fmt(pt)}), | |
| 21369 | } | |
| 21364 | 21370 | _ = try sema.checkIntType(block, operand_src, operand_scalar_ty); |
| 21365 | 21371 | |
| 21366 | 21372 | if (sema.resolveValue(operand)) |operand_val| { |
test/cases/compile_errors/invalid_float_casts.zig+1-1| ... | ... | @@ -22,6 +22,6 @@ export fn qux() void { |
| 22 | 22 | // error |
| 23 | 23 | // |
| 24 | 24 | // :4:40: error: unable to cast runtime value to 'comptime_float' |
| 25 | // :9:18: error: expected integer type, found 'f32' | |
| 25 | // :9:18: error: expected integer result type, found 'f32' | |
| 26 | 26 | // :14:32: error: expected integer type, found 'f32' |
| 27 | 27 | // :19:29: error: expected float or vector type, found 'u32' |
test/cases/compile_errors/invalid_int_casts.zig+7-3| ... | ... | @@ -8,6 +8,9 @@ export fn bar() void { |
| 8 | 8 | _ = &a; |
| 9 | 9 | _ = @as(u32, @floatFromInt(a)); |
| 10 | 10 | } |
| 11 | export fn bar2() void { | |
| 12 | _ = @as(comptime_int, @floatFromInt(2)); | |
| 13 | } | |
| 11 | 14 | export fn baz() void { |
| 12 | 15 | var a: u32 = 2; |
| 13 | 16 | _ = &a; |
| ... | ... | @@ -22,6 +25,7 @@ export fn qux() void { |
| 22 | 25 | // error |
| 23 | 26 | // |
| 24 | 27 | // :4:36: error: unable to cast runtime value to 'comptime_int' |
| 25 | // :9:18: error: expected float type, found 'u32' | |
| 26 | // :14:32: error: expected float type, found 'u32' | |
| 27 | // :19:27: error: expected integer or vector, found 'f32' | |
| 28 | // :9:18: error: expected float result type, found 'u32' | |
| 29 | // :12:27: error: expected float result type, found 'comptime_int' | |
| 30 | // :17:32: error: expected float type, found 'u32' | |
| 31 | // :22:27: error: expected integer or vector, found 'f32' |