authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-17 16:36:08+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-17 14:22:20-04:00
logc026a9f6d254cb4b6cf5d75105f17e3c912e32c2
treebe5172d482251450af6f73f2c2177963a617a035
parent8e96922f3118601dd30da23ba7db1440066784ab

fix missing compile errors on builtin cast functions


2 files changed, 85 insertions(+), 27 deletions(-)

src/ir.cpp+29-27
......@@ -25957,16 +25957,10 @@ static IrInstGen *ir_analyze_instruction_int_cast(IrAnalyze *ira, IrInstSrcIntCa
2595725957 return ira->codegen->invalid_inst_gen;
2595825958 }
2595925959
25960 if (instr_is_comptime(target)) {
25960 if (instr_is_comptime(target) || dest_type->id == ZigTypeIdComptimeInt) {
2596125961 return ir_implicit_cast2(ira, &instruction->target->base, target, dest_type);
2596225962 }
2596325963
25964 if (dest_type->id == ZigTypeIdComptimeInt) {
25965 ir_add_error(ira, &instruction->target->base, buf_sprintf("attempt to cast runtime value to '%s'",
25966 buf_ptr(&dest_type->name)));
25967 return ira->codegen->invalid_inst_gen;
25968 }
25969
2597025964 return ir_analyze_widen_or_shorten(ira, &instruction->base.base, target, dest_type);
2597125965}
2597225966
......@@ -25975,7 +25969,7 @@ static IrInstGen *ir_analyze_instruction_float_cast(IrAnalyze *ira, IrInstSrcFlo
2597525969 if (type_is_invalid(dest_type))
2597625970 return ira->codegen->invalid_inst_gen;
2597725971
25978 if (dest_type->id != ZigTypeIdFloat) {
25972 if (dest_type->id != ZigTypeIdFloat && dest_type->id != ZigTypeIdComptimeFloat) {
2597925973 ir_add_error(ira, &instruction->dest_type->base,
2598025974 buf_sprintf("expected float type, found '%s'", buf_ptr(&dest_type->name)));
2598125975 return ira->codegen->invalid_inst_gen;
......@@ -26001,6 +25995,10 @@ static IrInstGen *ir_analyze_instruction_float_cast(IrAnalyze *ira, IrInstSrcFlo
2600125995 }
2600225996 }
2600325997
25998 if (instr_is_comptime(target) || dest_type->id == ZigTypeIdComptimeFloat) {
25999 return ir_implicit_cast2(ira, &instruction->target->base, target, dest_type);
26000 }
26001
2600426002 if (target->value->type->id != ZigTypeIdFloat) {
2600526003 ir_add_error(ira, &instruction->target->base, buf_sprintf("expected float type, found '%s'",
2600626004 buf_ptr(&target->value->type->name)));
......@@ -26064,6 +26062,12 @@ static IrInstGen *ir_analyze_instruction_int_to_float(IrAnalyze *ira, IrInstSrcI
2606426062 if (type_is_invalid(dest_type))
2606526063 return ira->codegen->invalid_inst_gen;
2606626064
26065 if (dest_type->id != ZigTypeIdFloat && dest_type->id != ZigTypeIdComptimeFloat) {
26066 ir_add_error(ira, &instruction->dest_type->base,
26067 buf_sprintf("expected float type, found '%s'", buf_ptr(&dest_type->name)));
26068 return ira->codegen->invalid_inst_gen;
26069 }
26070
2606726071 IrInstGen *target = instruction->target->child;
2606826072 if (type_is_invalid(target->value->type))
2606926073 return ira->codegen->invalid_inst_gen;
......@@ -26077,33 +26081,31 @@ static IrInstGen *ir_analyze_instruction_int_to_float(IrAnalyze *ira, IrInstSrcI
2607726081 return ir_resolve_cast(ira, &instruction->base.base, target, dest_type, CastOpIntToFloat);
2607826082}
2607926083
26080static IrInstGen *ir_analyze_float_to_int(IrAnalyze *ira, IrInst* source_instr,
26081 ZigType *dest_type, IrInstGen *operand, AstNode *operand_source_node)
26082{
26083 if (operand->value->type->id == ZigTypeIdComptimeInt) {
26084 return ir_implicit_cast(ira, operand, dest_type);
26085 }
26084static IrInstGen *ir_analyze_instruction_float_to_int(IrAnalyze *ira, IrInstSrcFloatToInt *instruction) {
26085 ZigType *dest_type = ir_resolve_type(ira, instruction->dest_type->child);
26086 if (type_is_invalid(dest_type))
26087 return ira->codegen->invalid_inst_gen;
2608626088
26087 if (operand->value->type->id != ZigTypeIdFloat && operand->value->type->id != ZigTypeIdComptimeFloat) {
26088 ir_add_error_node(ira, operand_source_node, buf_sprintf("expected float type, found '%s'",
26089 buf_ptr(&operand->value->type->name)));
26089 if (dest_type->id != ZigTypeIdInt && dest_type->id != ZigTypeIdComptimeInt) {
26090 ir_add_error(ira, &instruction->dest_type->base, buf_sprintf("expected integer type, found '%s'", buf_ptr(&dest_type->name)));
2609026091 return ira->codegen->invalid_inst_gen;
2609126092 }
2609226093
26093 return ir_resolve_cast(ira, source_instr, operand, dest_type, CastOpFloatToInt);
26094}
26095
26096static IrInstGen *ir_analyze_instruction_float_to_int(IrAnalyze *ira, IrInstSrcFloatToInt *instruction) {
26097 ZigType *dest_type = ir_resolve_type(ira, instruction->dest_type->child);
26098 if (type_is_invalid(dest_type))
26094 IrInstGen *target = instruction->target->child;
26095 if (type_is_invalid(target->value->type))
2609926096 return ira->codegen->invalid_inst_gen;
2610026097
26101 IrInstGen *operand = instruction->target->child;
26102 if (type_is_invalid(operand->value->type))
26098 if (target->value->type->id == ZigTypeIdComptimeInt) {
26099 return ir_implicit_cast(ira, target, dest_type);
26100 }
26101
26102 if (target->value->type->id != ZigTypeIdFloat && target->value->type->id != ZigTypeIdComptimeFloat) {
26103 ir_add_error_node(ira, target->base.source_node, buf_sprintf("expected float type, found '%s'",
26104 buf_ptr(&target->value->type->name)));
2610326105 return ira->codegen->invalid_inst_gen;
26106 }
2610426107
26105 return ir_analyze_float_to_int(ira, &instruction->base.base, dest_type, operand,
26106 instruction->target->base.source_node);
26108 return ir_resolve_cast(ira, &instruction->base.base, target, dest_type, CastOpFloatToInt);
2610726109}
2610826110
2610926111static IrInstGen *ir_analyze_instruction_err_to_int(IrAnalyze *ira, IrInstSrcErrToInt *instruction) {
test/compile_errors.zig+56
......@@ -2,6 +2,62 @@ const tests = @import("tests.zig");
22const std = @import("std");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.addTest("invalid int casts",
6 \\export fn foo() void {
7 \\ var a: u32 = 2;
8 \\ _ = @intCast(comptime_int, a);
9 \\}
10 \\export fn bar() void {
11 \\ var a: u32 = 2;
12 \\ _ = @intToFloat(u32, a);
13 \\}
14 \\export fn baz() void {
15 \\ var a: u32 = 2;
16 \\ _ = @floatToInt(u32, a);
17 \\}
18 \\export fn qux() void {
19 \\ var a: u32 = 2;
20 \\ _ = @intCast(comptime_int, a);
21 \\}
22 , &[_][]const u8{
23 "tmp.zig:3:32: error: expected type 'comptime_int', found 'u32'",
24 "tmp.zig:3:9: note: referenced here",
25 "tmp.zig:7:21: error: expected float type, found 'u32'",
26 "tmp.zig:7:9: note: referenced here",
27 "tmp.zig:11:26: error: expected float type, found 'u32'",
28 "tmp.zig:11:9: note: referenced here",
29 "tmp.zig:15:32: error: expected type 'comptime_int', found 'u32'",
30 "tmp.zig:15:9: note: referenced here",
31 });
32
33 cases.addTest("invalid float casts",
34 \\export fn foo() void {
35 \\ var a: f32 = 2;
36 \\ _ = @floatCast(comptime_float, a);
37 \\}
38 \\export fn bar() void {
39 \\ var a: f32 = 2;
40 \\ _ = @floatToInt(f32, a);
41 \\}
42 \\export fn baz() void {
43 \\ var a: f32 = 2;
44 \\ _ = @intToFloat(f32, a);
45 \\}
46 \\export fn qux() void {
47 \\ var a: f32 = 2;
48 \\ _ = @floatCast(comptime_float, a);
49 \\}
50 , &[_][]const u8{
51 "tmp.zig:3:36: error: expected type 'comptime_float', found 'f32'",
52 "tmp.zig:3:9: note: referenced here",
53 "tmp.zig:7:21: error: expected integer type, found 'f32'",
54 "tmp.zig:7:9: note: referenced here",
55 "tmp.zig:11:26: error: expected int type, found 'f32'",
56 "tmp.zig:11:9: note: referenced here",
57 "tmp.zig:15:36: error: expected type 'comptime_float', found 'f32'",
58 "tmp.zig:15:9: note: referenced here",
59 });
60
561 cases.addTest("invalid assignments",
662 \\export fn entry1() void {
763 \\ var a: []const u8 = "foo";