Skip to content

Commit

Permalink
fix logic for determining whether param requires comptime
Browse files Browse the repository at this point in the history
closes #778
closes #1213
  • Loading branch information
andrewrk committed Jul 24, 2018
1 parent 74c80d2 commit 29e19ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/analyze.cpp
Expand Up @@ -1585,10 +1585,6 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
case TypeTableEntryIdBlock:
case TypeTableEntryIdBoundFn:
case TypeTableEntryIdMetaType:
add_node_error(g, param_node->data.param_decl.type,
buf_sprintf("parameter of type '%s' must be declared comptime",
buf_ptr(&type_entry->name)));
return g->builtin_types.entry_invalid;
case TypeTableEntryIdVoid:
case TypeTableEntryIdBool:
case TypeTableEntryIdInt:
Expand All @@ -1603,6 +1599,13 @@ static TypeTableEntry *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *c
case TypeTableEntryIdUnion:
case TypeTableEntryIdFn:
case TypeTableEntryIdPromise:
type_ensure_zero_bits_known(g, type_entry);
if (type_requires_comptime(type_entry)) {
add_node_error(g, param_node->data.param_decl.type,
buf_sprintf("parameter of type '%s' must be declared comptime",
buf_ptr(&type_entry->name)));
return g->builtin_types.entry_invalid;
}
break;
}
FnTypeParamInfo *param_info = &fn_type_id.param_info[fn_type_id.next_param_index];
Expand Down Expand Up @@ -5019,9 +5022,10 @@ bool type_requires_comptime(TypeTableEntry *type_entry) {
} else {
return type_requires_comptime(type_entry->data.pointer.child_type);
}
case TypeTableEntryIdFn:
return type_entry->data.fn.is_generic;
case TypeTableEntryIdEnum:
case TypeTableEntryIdErrorSet:
case TypeTableEntryIdFn:
case TypeTableEntryIdBool:
case TypeTableEntryIdInt:
case TypeTableEntryIdFloat:
Expand Down
11 changes: 11 additions & 0 deletions test/compile_errors.zig
@@ -1,6 +1,17 @@
const tests = @import("tests.zig");

pub fn addCases(cases: *tests.CompileErrorContext) void {
cases.add(
"generic fn as parameter without comptime keyword",
\\fn f(_: fn (var) void) void {}
\\fn g(_: var) void {}
\\export fn entry() void {
\\ f(g);
\\}
,
".tmp_source.zig:1:9: error: parameter of type 'fn(var)var' must be declared comptime",
);

cases.add(
"optional pointer to void in extern struct",
\\comptime {
Expand Down

0 comments on commit 29e19ac

Please sign in to comment.