authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-09 13:30:53-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-12-09 13:30:53-05:00
log5d82744f1cb3ea66c20fc377d0237a50ccbcf81e
treea5da5c96fe71492cbf5d29a2e96b6cfe45a3793e
parent640e09183d3100c477a26c6cdc26f1eae31472a1
signature Commit is signed but in an unrecognized format.

ability to give comptime and non-comptime types to same parameter


3 files changed, 30 insertions(+), 13 deletions(-)

lib/std/fmt.zig+1
...@@ -469,6 +469,7 @@ pub fn formatType(...@@ -469,6 +469,7 @@ pub fn formatType(
469 .Fn => {469 .Fn => {
470 return format(context, Errors, output, "{}@{x}", .{ @typeName(T), @ptrToInt(value) });470 return format(context, Errors, output, "{}@{x}", .{ @typeName(T), @ptrToInt(value) });
471 },471 },
472 .Type => return output(context, @typeName(T)),
472 else => @compileError("Unable to format type '" ++ @typeName(T) ++ "'"),473 else => @compileError("Unable to format type '" ++ @typeName(T) ++ "'"),
473 }474 }
474}475}
src/ir.cpp+12-13
...@@ -17359,8 +17359,18 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod...@@ -17359,8 +17359,18 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
17359 }17359 }
17360 }17360 }
1736117361
17362 bool comptime_arg = param_decl_node->data.param_decl.is_comptime ||17362 bool comptime_arg = param_decl_node->data.param_decl.is_comptime;
17363 casted_arg->value->type->id == ZigTypeIdComptimeInt || casted_arg->value->type->id == ZigTypeIdComptimeFloat;17363 if (!comptime_arg) {
17364 switch (type_requires_comptime(ira->codegen, casted_arg->value->type)) {
17365 case ReqCompTimeInvalid:
17366 return false;
17367 case ReqCompTimeYes:
17368 comptime_arg = true;
17369 break;
17370 case ReqCompTimeNo:
17371 break;
17372 }
17373 }
1736417374
17365 ZigValue *arg_val;17375 ZigValue *arg_val;
1736617376
...@@ -17395,17 +17405,6 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod...@@ -17395,17 +17405,6 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
17395 }17405 }
1739617406
17397 if (!comptime_arg) {17407 if (!comptime_arg) {
17398 switch (type_requires_comptime(ira->codegen, casted_arg->value->type)) {
17399 case ReqCompTimeYes:
17400 ir_add_error(ira, casted_arg,
17401 buf_sprintf("parameter of type '%s' requires comptime", buf_ptr(&casted_arg->value->type->name)));
17402 return false;
17403 case ReqCompTimeInvalid:
17404 return false;
17405 case ReqCompTimeNo:
17406 break;
17407 }
17408
17409 casted_args[fn_type_id->param_count] = casted_arg;17408 casted_args[fn_type_id->param_count] = casted_arg;
17410 FnTypeParamInfo *param_info = &fn_type_id->param_info[fn_type_id->param_count];17409 FnTypeParamInfo *param_info = &fn_type_id->param_info[fn_type_id->param_count];
17411 param_info->type = casted_arg->value->type;17410 param_info->type = casted_arg->value->type;
test/stage1/behavior/fn.zig+17
...@@ -255,3 +255,20 @@ test "function call with anon list literal" {...@@ -255,3 +255,20 @@ test "function call with anon list literal" {
255 S.doTheTest();255 S.doTheTest();
256 comptime S.doTheTest();256 comptime S.doTheTest();
257}257}
258
259test "ability to give comptime types and non comptime types to same parameter" {
260 const S = struct {
261 fn doTheTest() void {
262 var x: i32 = 1;
263 expect(foo(x) == 10);
264 expect(foo(i32) == 20);
265 }
266
267 fn foo(arg: var) i32 {
268 if (@typeInfo(@typeOf(arg)) == .Type and arg == i32) return 20;
269 return 9 + arg;
270 }
271 };
272 S.doTheTest();
273 comptime S.doTheTest();
274}