From e48e707c32121a73f1fd2862197c8f47dbceea5e Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 11 Apr 2018 14:44:32 -0400 Subject: [PATCH] allow integer and float literals to be passed to var params closes #623 --- src/ir.cpp | 9 ++++++++- test/cases/fn.zig | 10 ++++++++++ test/compile_errors.zig | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 0b072cc696..7d8088d5ed 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -11804,7 +11804,8 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod } } - bool comptime_arg = param_decl_node->data.param_decl.is_inline; + bool comptime_arg = param_decl_node->data.param_decl.is_inline || + casted_arg->value.type->id == TypeTableEntryIdNumLitInt || casted_arg->value.type->id == TypeTableEntryIdNumLitFloat; ConstExprValue *arg_val; @@ -11829,6 +11830,12 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod var->shadowable = !comptime_arg; *next_proto_i += 1; + } else if (casted_arg->value.type->id == TypeTableEntryIdNumLitInt || + casted_arg->value.type->id == TypeTableEntryIdNumLitFloat) + { + ir_add_error(ira, casted_arg, + buf_sprintf("compiler bug: integer and float literals in var args function must be casted. https://github.com/zig-lang/zig/issues/557")); + return false; } if (!comptime_arg) { diff --git a/test/cases/fn.zig b/test/cases/fn.zig index e492f6036c..c125d98d8c 100644 --- a/test/cases/fn.zig +++ b/test/cases/fn.zig @@ -94,3 +94,13 @@ test "inline function call" { } fn add(a: i32, b: i32) i32 { return a + b; } + + +test "number literal as an argument" { + numberLiteralArg(3); + comptime numberLiteralArg(3); +} + +fn numberLiteralArg(a: var) void { + assert(a == 3); +} diff --git a/test/compile_errors.zig b/test/compile_errors.zig index bed5aa1b63..21e384e389 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -1723,7 +1723,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void { \\} \\ \\export fn entry() usize { return @sizeOf(@typeOf(bar)); } - , ".tmp_source.zig:10:16: error: parameter of type '(integer literal)' requires comptime"); + , ".tmp_source.zig:10:16: error: compiler bug: integer and float literals in var args function must be casted"); cases.add("assign too big number to u16", \\export fn foo() void {