Skip to content

Commit e48e707

Browse files
committed
allow integer and float literals to be passed to var params
closes #623
1 parent a7f77d7 commit e48e707

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

src/ir.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -11804,7 +11804,8 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1180411804
}
1180511805
}
1180611806

11807-
bool comptime_arg = param_decl_node->data.param_decl.is_inline;
11807+
bool comptime_arg = param_decl_node->data.param_decl.is_inline ||
11808+
casted_arg->value.type->id == TypeTableEntryIdNumLitInt || casted_arg->value.type->id == TypeTableEntryIdNumLitFloat;
1180811809

1180911810
ConstExprValue *arg_val;
1181011811

@@ -11829,6 +11830,12 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1182911830
var->shadowable = !comptime_arg;
1183011831

1183111832
*next_proto_i += 1;
11833+
} else if (casted_arg->value.type->id == TypeTableEntryIdNumLitInt ||
11834+
casted_arg->value.type->id == TypeTableEntryIdNumLitFloat)
11835+
{
11836+
ir_add_error(ira, casted_arg,
11837+
buf_sprintf("compiler bug: integer and float literals in var args function must be casted. https://github.com/zig-lang/zig/issues/557"));
11838+
return false;
1183211839
}
1183311840

1183411841
if (!comptime_arg) {

test/cases/fn.zig

+10
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,13 @@ test "inline function call" {
9494
}
9595

9696
fn add(a: i32, b: i32) i32 { return a + b; }
97+
98+
99+
test "number literal as an argument" {
100+
numberLiteralArg(3);
101+
comptime numberLiteralArg(3);
102+
}
103+
104+
fn numberLiteralArg(a: var) void {
105+
assert(a == 3);
106+
}

test/compile_errors.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,7 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
17231723
\\}
17241724
\\
17251725
\\export fn entry() usize { return @sizeOf(@typeOf(bar)); }
1726-
, ".tmp_source.zig:10:16: error: parameter of type '(integer literal)' requires comptime");
1726+
, ".tmp_source.zig:10:16: error: compiler bug: integer and float literals in var args function must be casted");
17271727

17281728
cases.add("assign too big number to u16",
17291729
\\export fn foo() void {

0 commit comments

Comments
 (0)