Skip to content

Commit fba0347

Browse files
committed
.ReturnType and @ArgType now emits errors on unresolved types
related: #846
1 parent 2fc34ea commit fba0347

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/ir.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13859,6 +13859,15 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
1385913859
}
1386013860
} else if (child_type->id == TypeTableEntryIdFn) {
1386113861
if (buf_eql_str(field_name, "ReturnType")) {
13862+
if (child_type->data.fn.fn_type_id.return_type == nullptr) {
13863+
// Return type can only ever be null, if the function is generic
13864+
assert(child_type->data.fn.is_generic);
13865+
13866+
ir_add_error(ira, &field_ptr_instruction->base,
13867+
buf_sprintf("ReturnType has not been resolved because '%s' is generic", buf_ptr(&child_type->name)));
13868+
return ira->codegen->builtin_types.entry_invalid;
13869+
}
13870+
1386213871
bool ptr_is_const = true;
1386313872
bool ptr_is_volatile = false;
1386413873
return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
@@ -17860,6 +17869,16 @@ static TypeTableEntry *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruc
1786017869

1786117870
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
1786217871
out_val->data.x_type = fn_type_id->param_info[arg_index].type;
17872+
if (out_val->data.x_type == nullptr) {
17873+
// Args are only unresolved if our function is generic.
17874+
assert(fn_type->data.fn.is_generic);
17875+
17876+
ir_add_error(ira, arg_index_inst,
17877+
buf_sprintf("@ArgType could not resolve the type of arg %" ZIG_PRI_usize " because '%s' is generic",
17878+
arg_index, buf_ptr(&fn_type->name)));
17879+
return ira->codegen->builtin_types.entry_invalid;
17880+
}
17881+
1786317882
return ira->codegen->builtin_types.entry_type;
1786417883
}
1786517884

test/compile_errors.zig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,4 +3209,21 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
32093209
\\}
32103210
,
32113211
".tmp_source.zig:5:42: error: zero-bit field 'val' in struct 'Empty' has no offset");
3212+
3213+
cases.add("getting return type of generic function",
3214+
\\fn generic(a: var) void {}
3215+
\\comptime {
3216+
\\ _ = @typeOf(generic).ReturnType;
3217+
\\}
3218+
,
3219+
".tmp_source.zig:3:25: error: ReturnType has not been resolved because 'fn(var)var' is generic");
3220+
3221+
cases.add("getting @ArgType of generic function",
3222+
\\fn generic(a: var) void {}
3223+
\\comptime {
3224+
\\ _ = @ArgType(@typeOf(generic), 0);
3225+
\\}
3226+
,
3227+
".tmp_source.zig:3:36: error: @ArgType could not resolve the type of arg 0 because 'fn(var)var' is generic");
3228+
32123229
}

0 commit comments

Comments
 (0)