aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ir.cpp6
-rw-r--r--test/compile_errors.zig12
2 files changed, 18 insertions, 0 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index d027badac1..88e111ccb5 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -26739,6 +26739,12 @@ static IrInstGen *ir_analyze_instruction_float_cast(IrAnalyze *ira, IrInstSrcFlo
}
}
+ if (target->value->type->id != ZigTypeIdFloat) {
+ ir_add_error(ira, &instruction->target->base, buf_sprintf("expected float type, found '%s'",
+ buf_ptr(&target->value->type->name)));
+ return ira->codegen->invalid_inst_gen;
+ }
+
if (instr_is_comptime(target) || dest_type->id == ZigTypeIdComptimeFloat) {
ZigValue *val = ir_resolve_const(ira, target, UndefBad);
if (val == nullptr)
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index 5de56f8ca7..a1a261f887 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -122,6 +122,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ var a: u32 = 2;
\\ _ = @floatToInt(u32, a);
\\}
+ \\export fn qux() void {
+ \\ var a: f32 = 2;
+ \\ _ = @intCast(u32, a);
+ \\}
, &[_][]const u8{
"tmp.zig:3:32: error: unable to evaluate constant expression",
"tmp.zig:3:9: note: referenced here",
@@ -129,6 +133,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:7:9: note: referenced here",
"tmp.zig:11:26: error: expected float type, found 'u32'",
"tmp.zig:11:9: note: referenced here",
+ "tmp.zig:15:23: error: expected integer type, found 'f32'",
+ "tmp.zig:15:9: note: referenced here",
});
cases.addTest("invalid float casts",
@@ -144,6 +150,10 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ var a: f32 = 2;
\\ _ = @intToFloat(f32, a);
\\}
+ \\export fn qux() void {
+ \\ var a: u32 = 2;
+ \\ _ = @floatCast(f32, a);
+ \\}
, &[_][]const u8{
"tmp.zig:3:36: error: unable to evaluate constant expression",
"tmp.zig:3:9: note: referenced here",
@@ -151,6 +161,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:7:9: note: referenced here",
"tmp.zig:11:26: error: expected int type, found 'f32'",
"tmp.zig:11:9: note: referenced here",
+ "tmp.zig:15:25: error: expected float type, found 'u32'",
+ "tmp.zig:15:9: note: referenced here",
});
cases.addTest("invalid assignments",