authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-28 17:17:48+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2018-04-28 17:17:48+02:00
logfba0347ec43fb5c06b5ac9bec541b740d95194fe
treeebf1c2a12012ee94efb7e8363dd759b5ac7ab284
parent2fc34eaa581cc31827e978fbd973bf36d2c647e2

.ReturnType and @ArgType now emits errors on unresolved types

related: #846

2 files changed, 36 insertions(+), 0 deletions(-)

src/ir.cpp+19
...@@ -13859,6 +13859,15 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru...@@ -13859,6 +13859,15 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
13859 }13859 }
13860 } else if (child_type->id == TypeTableEntryIdFn) {13860 } else if (child_type->id == TypeTableEntryIdFn) {
13861 if (buf_eql_str(field_name, "ReturnType")) {13861 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
13862 bool ptr_is_const = true;13871 bool ptr_is_const = true;
13863 bool ptr_is_volatile = false;13872 bool ptr_is_volatile = false;
13864 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,13873 return ir_analyze_const_ptr(ira, &field_ptr_instruction->base,
...@@ -17860,6 +17869,16 @@ static TypeTableEntry *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruc...@@ -17860,6 +17869,16 @@ static TypeTableEntry *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstruc
1786017869
17861 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);17870 ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
17862 out_val->data.x_type = fn_type_id->param_info[arg_index].type;17871 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
17863 return ira->codegen->builtin_types.entry_type;17882 return ira->codegen->builtin_types.entry_type;
17864}17883}
1786517884
test/compile_errors.zig+17
...@@ -3209,4 +3209,21 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {...@@ -3209,4 +3209,21 @@ pub fn addCases(cases: &tests.CompileErrorContext) void {
3209 \\}3209 \\}
3210 ,3210 ,
3211 ".tmp_source.zig:5:42: error: zero-bit field 'val' in struct 'Empty' has no offset");3211 ".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
3212}3229}