commit e48e707c32121a73f1fd2862197c8f47dbceea5e (tree)
parent a7f77d7c6a4326de4c4cd356cd88e48854817e6f
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Wed, 11 Apr 2018 14:44:32 -0400
allow integer and float literals to be passed to var params
closes #623
Diffstat:
3 files changed, 19 insertions(+), 2 deletions(-)
diff --git 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
@@ -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
@@ -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 {