authorgravatar for frmdstryr@protonmail.comfrmdstryr <frmdstryr@protonmail.com> 2020-10-16 13:16:09-04:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-11-18 13:33:45+02:00
loga39d3155b45cf2a1d5c91c7c82089c6f4ed7213c
tree440d851281a606c20294409a26ddbd18692b306e
parent129ccad43443f26f00584816b18425c9a3a7b080

Change error when runtime value passed to comptime arg


2 files changed, 13 insertions(+), 8 deletions(-)

src/stage1/ir.cpp+12-7
...@@ -6362,7 +6362,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod...@@ -6362,7 +6362,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
6362 BuiltinFnEntry *builtin_fn = entry->value;6362 BuiltinFnEntry *builtin_fn = entry->value;
6363 size_t actual_param_count = node->data.fn_call_expr.params.length;6363 size_t actual_param_count = node->data.fn_call_expr.params.length;
63646364
6365 if (builtin_fn->param_count != SIZE_MAX && builtin_fn->param_count != actual_param_count) { 6365 if (builtin_fn->param_count != SIZE_MAX && builtin_fn->param_count != actual_param_count) {
6366 add_node_error(irb->codegen, node,6366 add_node_error(irb->codegen, node,
6367 buf_sprintf("expected %" ZIG_PRI_usize " argument(s), found %" ZIG_PRI_usize,6367 buf_sprintf("expected %" ZIG_PRI_usize " argument(s), found %" ZIG_PRI_usize,
6368 builtin_fn->param_count, actual_param_count));6368 builtin_fn->param_count, actual_param_count));
...@@ -8576,7 +8576,7 @@ static IrInstSrc *ir_gen_for_expr(IrBuilderSrc *irb, Scope *parent_scope, AstNod...@@ -8576,7 +8576,7 @@ static IrInstSrc *ir_gen_for_expr(IrBuilderSrc *irb, Scope *parent_scope, AstNod
85768576
8577 if (is_duplicate_label(irb->codegen, child_scope, node, node->data.for_expr.name))8577 if (is_duplicate_label(irb->codegen, child_scope, node, node->data.for_expr.name))
8578 return irb->codegen->invalid_inst_src;8578 return irb->codegen->invalid_inst_src;
8579 8579
8580 ZigList<IrInstSrc *> incoming_values = {0};8580 ZigList<IrInstSrc *> incoming_values = {0};
8581 ZigList<IrBasicBlockSrc *> incoming_blocks = {0};8581 ZigList<IrBasicBlockSrc *> incoming_blocks = {0};
8582 ScopeLoop *loop_scope = create_loop_scope(irb->codegen, node, child_scope);8582 ScopeLoop *loop_scope = create_loop_scope(irb->codegen, node, child_scope);
...@@ -20068,6 +20068,11 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod...@@ -20068,6 +20068,11 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
2006820068
20069 ZigValue *arg_val;20069 ZigValue *arg_val;
2007020070
20071 if (comptime_arg && !instr_is_comptime(casted_arg)) {
20072 ir_add_error(ira, &casted_arg->base,
20073 buf_sprintf("runtime value cannot be passed to comptime arg"));
20074 return false;
20075 }
20071 if (comptime_arg) {20076 if (comptime_arg) {
20072 arg_part_of_generic_id = true;20077 arg_part_of_generic_id = true;
20073 arg_val = ir_resolve_const(ira, casted_arg, UndefBad);20078 arg_val = ir_resolve_const(ira, casted_arg, UndefBad);
...@@ -20854,7 +20859,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -20854,7 +20859,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
20854 expected_return_type->id != ZigTypeIdErrorUnion && expected_return_type->id != ZigTypeIdErrorSet)20859 expected_return_type->id != ZigTypeIdErrorUnion && expected_return_type->id != ZigTypeIdErrorSet)
20855 {20860 {
20856 if (call_result_loc->id == ResultLocIdReturn) {20861 if (call_result_loc->id == ResultLocIdReturn) {
20857 add_error_note(ira->codegen, ira->new_irb.exec->first_err_trace_msg, 20862 add_error_note(ira->codegen, ira->new_irb.exec->first_err_trace_msg,
20858 ira->explicit_return_type_source_node, buf_sprintf("function cannot return an error"));20863 ira->explicit_return_type_source_node, buf_sprintf("function cannot return an error"));
20859 } else {20864 } else {
20860 add_error_note(ira->codegen, ira->new_irb.exec->first_err_trace_msg, result_loc->base.source_node,20865 add_error_note(ira->codegen, ira->new_irb.exec->first_err_trace_msg, result_loc->base.source_node,
...@@ -29490,7 +29495,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,...@@ -29490,7 +29495,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
29490 }29495 }
29491 }29496 }
29492 } else if(!switch_type->data.enumeration.non_exhaustive && switch_type->data.enumeration.src_field_count == instruction->range_count) {29497 } else if(!switch_type->data.enumeration.non_exhaustive && switch_type->data.enumeration.src_field_count == instruction->range_count) {
29493 ir_add_error_node(ira, instruction->else_prong, 29498 ir_add_error_node(ira, instruction->else_prong,
29494 buf_sprintf("unreachable else prong, all cases already handled"));29499 buf_sprintf("unreachable else prong, all cases already handled"));
29495 return ira->codegen->invalid_inst_gen;29500 return ira->codegen->invalid_inst_gen;
29496 }29501 }
...@@ -29611,7 +29616,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,...@@ -29611,7 +29616,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
29611 ir_add_error(ira, &instruction->base.base, buf_sprintf("switch must handle all possibilities"));29616 ir_add_error(ira, &instruction->base.base, buf_sprintf("switch must handle all possibilities"));
29612 return ira->codegen->invalid_inst_gen;29617 return ira->codegen->invalid_inst_gen;
29613 } else if(handles_all_cases && instruction->else_prong != nullptr) {29618 } else if(handles_all_cases && instruction->else_prong != nullptr) {
29614 ir_add_error_node(ira, instruction->else_prong, 29619 ir_add_error_node(ira, instruction->else_prong,
29615 buf_sprintf("unreachable else prong, all cases already handled"));29620 buf_sprintf("unreachable else prong, all cases already handled"));
29616 return ira->codegen->invalid_inst_gen;29621 return ira->codegen->invalid_inst_gen;
29617 }29622 }
...@@ -29650,7 +29655,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,...@@ -29650,7 +29655,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
29650 }29655 }
2965129656
29652 if(seenTrue == 1 && seenFalse == 1 && instruction->else_prong != nullptr) {29657 if(seenTrue == 1 && seenFalse == 1 && instruction->else_prong != nullptr) {
29653 ir_add_error_node(ira, instruction->else_prong, 29658 ir_add_error_node(ira, instruction->else_prong,
29654 buf_sprintf("unreachable else prong, all cases already handled"));29659 buf_sprintf("unreachable else prong, all cases already handled"));
29655 return ira->codegen->invalid_inst_gen;29660 return ira->codegen->invalid_inst_gen;
29656 }29661 }
...@@ -29688,7 +29693,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,...@@ -29688,7 +29693,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
29688 }29693 }
29689 }29694 }
29690 prevs.deinit();29695 prevs.deinit();
29691 } 29696 }
29692 return ir_const_void(ira, &instruction->base.base);29697 return ir_const_void(ira, &instruction->base.base);
29693}29698}
2969429699
test/compile_errors.zig+1-1
...@@ -5314,7 +5314,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5314,7 +5314,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5314 \\5314 \\
5315 \\export fn entry() usize { return @sizeOf(@TypeOf(test1)); }5315 \\export fn entry() usize { return @sizeOf(@TypeOf(test1)); }
5316 , &[_][]const u8{5316 , &[_][]const u8{
5317 "tmp.zig:3:16: error: unable to evaluate constant expression",5317 "tmp.zig:3:16: error: runtime value cannot be passed to comptime arg",
5318 });5318 });
53195319
5320 cases.add("assign null to non-optional pointer",5320 cases.add("assign null to non-optional pointer",