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
63626362 BuiltinFnEntry *builtin_fn = entry->value;
63636363 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) {
63666366 add_node_error(irb->codegen, node,
63676367 buf_sprintf("expected %" ZIG_PRI_usize " argument(s), found %" ZIG_PRI_usize,
63686368 builtin_fn->param_count, actual_param_count));
......@@ -8576,7 +8576,7 @@ static IrInstSrc *ir_gen_for_expr(IrBuilderSrc *irb, Scope *parent_scope, AstNod
85768576
85778577 if (is_duplicate_label(irb->codegen, child_scope, node, node->data.for_expr.name))
85788578 return irb->codegen->invalid_inst_src;
8579
8579
85808580 ZigList<IrInstSrc *> incoming_values = {0};
85818581 ZigList<IrBasicBlockSrc *> incoming_blocks = {0};
85828582 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
2006820068
2006920069 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 }
2007120076 if (comptime_arg) {
2007220077 arg_part_of_generic_id = true;
2007320078 arg_val = ir_resolve_const(ira, casted_arg, UndefBad);
......@@ -20854,7 +20859,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
2085420859 expected_return_type->id != ZigTypeIdErrorUnion && expected_return_type->id != ZigTypeIdErrorSet)
2085520860 {
2085620861 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,
2085820863 ira->explicit_return_type_source_node, buf_sprintf("function cannot return an error"));
2085920864 } else {
2086020865 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,
2949029495 }
2949129496 }
2949229497 } 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,
2949429499 buf_sprintf("unreachable else prong, all cases already handled"));
2949529500 return ira->codegen->invalid_inst_gen;
2949629501 }
......@@ -29611,7 +29616,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
2961129616 ir_add_error(ira, &instruction->base.base, buf_sprintf("switch must handle all possibilities"));
2961229617 return ira->codegen->invalid_inst_gen;
2961329618 } 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,
2961529620 buf_sprintf("unreachable else prong, all cases already handled"));
2961629621 return ira->codegen->invalid_inst_gen;
2961729622 }
......@@ -29650,7 +29655,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
2965029655 }
2965129656
2965229657 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,
2965429659 buf_sprintf("unreachable else prong, all cases already handled"));
2965529660 return ira->codegen->invalid_inst_gen;
2965629661 }
......@@ -29688,7 +29693,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira,
2968829693 }
2968929694 }
2969029695 prevs.deinit();
29691 }
29696 }
2969229697 return ir_const_void(ira, &instruction->base.base);
2969329698}
2969429699
test/compile_errors.zig+1-1
......@@ -5314,7 +5314,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
53145314 \\
53155315 \\export fn entry() usize { return @sizeOf(@TypeOf(test1)); }
53165316 , &[_][]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",
53185318 });
53195319
53205320 cases.add("assign null to non-optional pointer",