From a39d3155b45cf2a1d5c91c7c82089c6f4ed7213c Mon Sep 17 00:00:00 2001 From: frmdstryr Date: Fri, 16 Oct 2020 13:16:09 -0400 Subject: [PATCH] Change error when runtime value passed to comptime arg --- src/stage1/ir.cpp | 19 ++++++++++++------- test/compile_errors.zig | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index 1be9507480..738ef3c65d 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -6362,7 +6362,7 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod BuiltinFnEntry *builtin_fn = entry->value; size_t actual_param_count = node->data.fn_call_expr.params.length; - if (builtin_fn->param_count != SIZE_MAX && builtin_fn->param_count != actual_param_count) { + if (builtin_fn->param_count != SIZE_MAX && builtin_fn->param_count != actual_param_count) { add_node_error(irb->codegen, node, buf_sprintf("expected %" ZIG_PRI_usize " argument(s), found %" ZIG_PRI_usize, builtin_fn->param_count, actual_param_count)); @@ -8576,7 +8576,7 @@ static IrInstSrc *ir_gen_for_expr(IrBuilderSrc *irb, Scope *parent_scope, AstNod if (is_duplicate_label(irb->codegen, child_scope, node, node->data.for_expr.name)) return irb->codegen->invalid_inst_src; - + ZigList incoming_values = {0}; ZigList incoming_blocks = {0}; 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 ZigValue *arg_val; + if (comptime_arg && !instr_is_comptime(casted_arg)) { + ir_add_error(ira, &casted_arg->base, + buf_sprintf("runtime value cannot be passed to comptime arg")); + return false; + } if (comptime_arg) { arg_part_of_generic_id = true; arg_val = ir_resolve_const(ira, casted_arg, UndefBad); @@ -20854,7 +20859,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr, expected_return_type->id != ZigTypeIdErrorUnion && expected_return_type->id != ZigTypeIdErrorSet) { if (call_result_loc->id == ResultLocIdReturn) { - add_error_note(ira->codegen, ira->new_irb.exec->first_err_trace_msg, + add_error_note(ira->codegen, ira->new_irb.exec->first_err_trace_msg, ira->explicit_return_type_source_node, buf_sprintf("function cannot return an error")); } else { 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, } } } else if(!switch_type->data.enumeration.non_exhaustive && switch_type->data.enumeration.src_field_count == instruction->range_count) { - ir_add_error_node(ira, instruction->else_prong, + ir_add_error_node(ira, instruction->else_prong, buf_sprintf("unreachable else prong, all cases already handled")); return ira->codegen->invalid_inst_gen; } @@ -29611,7 +29616,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira, ir_add_error(ira, &instruction->base.base, buf_sprintf("switch must handle all possibilities")); return ira->codegen->invalid_inst_gen; } else if(handles_all_cases && instruction->else_prong != nullptr) { - ir_add_error_node(ira, instruction->else_prong, + ir_add_error_node(ira, instruction->else_prong, buf_sprintf("unreachable else prong, all cases already handled")); return ira->codegen->invalid_inst_gen; } @@ -29650,7 +29655,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira, } if(seenTrue == 1 && seenFalse == 1 && instruction->else_prong != nullptr) { - ir_add_error_node(ira, instruction->else_prong, + ir_add_error_node(ira, instruction->else_prong, buf_sprintf("unreachable else prong, all cases already handled")); return ira->codegen->invalid_inst_gen; } @@ -29688,7 +29693,7 @@ static IrInstGen *ir_analyze_instruction_check_switch_prongs(IrAnalyze *ira, } } prevs.deinit(); - } + } return ir_const_void(ira, &instruction->base.base); } diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 1d16cd6706..4d1f86b5a8 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -5314,7 +5314,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ \\export fn entry() usize { return @sizeOf(@TypeOf(test1)); } , &[_][]const u8{ - "tmp.zig:3:16: error: unable to evaluate constant expression", + "tmp.zig:3:16: error: runtime value cannot be passed to comptime arg", }); cases.add("assign null to non-optional pointer",