ability to give comptime and non-comptime types to same parameter
This commit is contained in:
parent
640e09183d
commit
5d82744f1c
@ -469,6 +469,7 @@ pub fn formatType(
|
|||||||
.Fn => {
|
.Fn => {
|
||||||
return format(context, Errors, output, "{}@{x}", .{ @typeName(T), @ptrToInt(value) });
|
return format(context, Errors, output, "{}@{x}", .{ @typeName(T), @ptrToInt(value) });
|
||||||
},
|
},
|
||||||
|
.Type => return output(context, @typeName(T)),
|
||||||
else => @compileError("Unable to format type '" ++ @typeName(T) ++ "'"),
|
else => @compileError("Unable to format type '" ++ @typeName(T) ++ "'"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
25
src/ir.cpp
25
src/ir.cpp
@ -17359,8 +17359,18 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool comptime_arg = param_decl_node->data.param_decl.is_comptime ||
|
bool comptime_arg = param_decl_node->data.param_decl.is_comptime;
|
||||||
casted_arg->value->type->id == ZigTypeIdComptimeInt || casted_arg->value->type->id == ZigTypeIdComptimeFloat;
|
if (!comptime_arg) {
|
||||||
|
switch (type_requires_comptime(ira->codegen, casted_arg->value->type)) {
|
||||||
|
case ReqCompTimeInvalid:
|
||||||
|
return false;
|
||||||
|
case ReqCompTimeYes:
|
||||||
|
comptime_arg = true;
|
||||||
|
break;
|
||||||
|
case ReqCompTimeNo:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ZigValue *arg_val;
|
ZigValue *arg_val;
|
||||||
|
|
||||||
@ -17395,17 +17405,6 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!comptime_arg) {
|
if (!comptime_arg) {
|
||||||
switch (type_requires_comptime(ira->codegen, casted_arg->value->type)) {
|
|
||||||
case ReqCompTimeYes:
|
|
||||||
ir_add_error(ira, casted_arg,
|
|
||||||
buf_sprintf("parameter of type '%s' requires comptime", buf_ptr(&casted_arg->value->type->name)));
|
|
||||||
return false;
|
|
||||||
case ReqCompTimeInvalid:
|
|
||||||
return false;
|
|
||||||
case ReqCompTimeNo:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
casted_args[fn_type_id->param_count] = casted_arg;
|
casted_args[fn_type_id->param_count] = casted_arg;
|
||||||
FnTypeParamInfo *param_info = &fn_type_id->param_info[fn_type_id->param_count];
|
FnTypeParamInfo *param_info = &fn_type_id->param_info[fn_type_id->param_count];
|
||||||
param_info->type = casted_arg->value->type;
|
param_info->type = casted_arg->value->type;
|
||||||
|
@ -255,3 +255,20 @@ test "function call with anon list literal" {
|
|||||||
S.doTheTest();
|
S.doTheTest();
|
||||||
comptime S.doTheTest();
|
comptime S.doTheTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "ability to give comptime types and non comptime types to same parameter" {
|
||||||
|
const S = struct {
|
||||||
|
fn doTheTest() void {
|
||||||
|
var x: i32 = 1;
|
||||||
|
expect(foo(x) == 10);
|
||||||
|
expect(foo(i32) == 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo(arg: var) i32 {
|
||||||
|
if (@typeInfo(@typeOf(arg)) == .Type and arg == i32) return 20;
|
||||||
|
return 9 + arg;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
S.doTheTest();
|
||||||
|
comptime S.doTheTest();
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user