Skip to content

Commit

Permalink
stage1: remove outdated error message regarding ziglang#447
Browse files Browse the repository at this point in the history
  • Loading branch information
g-w1 committed May 6, 2021
1 parent 530e67c commit 742b2ca
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 85 deletions.
2 changes: 1 addition & 1 deletion lib/std/zig/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ const Parser = struct {
};
}

/// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? CallConv? EXCLAMATIONMARK? (Keyword_anytype / TypeExpr)
/// FnProto <- KEYWORD_fn IDENTIFIER? LPAREN ParamDeclList RPAREN ByteAlign? LinkSection? CallConv? EXCLAMATIONMARK? TypeExpr
fn parseFnProto(p: *Parser) !Node.Index {
const fn_token = p.eatToken(.keyword_fn) orelse return null_node;

Expand Down
1 change: 0 additions & 1 deletion src/stage1/all_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,6 @@ struct AstNodeFnProto {
Buf *name;
ZigList<AstNode *> params;
AstNode *return_type;
Token *return_anytype_token;
AstNode *fn_def_node;
// populated if this is an extern declaration
Buf *lib_name;
Expand Down
13 changes: 0 additions & 13 deletions src/stage1/analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2125,18 +2125,6 @@ static ZigType *analyze_fn_type(CodeGen *g, AstNode *proto_node, Scope *child_sc
return g->builtin_types.entry_invalid;
}

if (fn_proto->return_anytype_token != nullptr) {
if (!calling_convention_allows_zig_types(fn_type_id.cc)) {
add_node_error(g, fn_proto->return_type,
buf_sprintf("return type 'anytype' not allowed in function with calling convention '%s'",
calling_convention_name(fn_type_id.cc)));
return g->builtin_types.entry_invalid;
}
add_node_error(g, proto_node,
buf_sprintf("TODO implement inferred return types https://github.com/ziglang/zig/issues/447"));
return g->builtin_types.entry_invalid;
}

ZigType *specified_return_type = analyze_type_expr(g, child_scope, fn_proto->return_type);
if (type_is_invalid(specified_return_type)) {
fn_type_id.return_type = g->builtin_types.entry_invalid;
Expand Down Expand Up @@ -10220,4 +10208,3 @@ const char *float_op_to_name(BuiltinFnId op) {
zig_unreachable();
}
}

16 changes: 6 additions & 10 deletions src/stage1/ast_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,17 +490,13 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
fprintf(ar->f, ")");
}

if (node->data.fn_proto.return_anytype_token != nullptr) {
fprintf(ar->f, "anytype");
} else {
AstNode *return_type_node = node->data.fn_proto.return_type;
assert(return_type_node != nullptr);
fprintf(ar->f, " ");
if (node->data.fn_proto.auto_err_set) {
fprintf(ar->f, "!");
}
render_node_grouped(ar, return_type_node);
AstNode *return_type_node = node->data.fn_proto.return_type;
assert(return_type_node != nullptr);
fprintf(ar->f, " ");
if (node->data.fn_proto.auto_err_set) {
fprintf(ar->f, "!");
}
render_node_grouped(ar, return_type_node);
break;
}
case NodeTypeFnDef:
Expand Down
83 changes: 37 additions & 46 deletions src/stage1/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10104,19 +10104,12 @@ static IrInstSrc *ir_gen_fn_proto(IrBuilderSrc *irb, Scope *parent_scope, AstNod
}

IrInstSrc *return_type;
if (node->data.fn_proto.return_anytype_token == nullptr) {
if (node->data.fn_proto.return_type == nullptr) {
return_type = ir_build_const_type(irb, parent_scope, node, irb->codegen->builtin_types.entry_void);
} else {
return_type = ir_gen_node(irb, node->data.fn_proto.return_type, parent_scope);
if (return_type == irb->codegen->invalid_inst_src)
return irb->codegen->invalid_inst_src;
}
if (node->data.fn_proto.return_type == nullptr) {
return_type = ir_build_const_type(irb, parent_scope, node, irb->codegen->builtin_types.entry_void);
} else {
add_node_error(irb->codegen, node,
buf_sprintf("TODO implement inferred return types https://github.com/ziglang/zig/issues/447"));
return irb->codegen->invalid_inst_src;
//return_type = nullptr;
return_type = ir_gen_node(irb, node->data.fn_proto.return_type, parent_scope);
if (return_type == irb->codegen->invalid_inst_src)
return irb->codegen->invalid_inst_src;
}

return ir_build_fn_proto(irb, parent_scope, node, param_types, align_value, callconv_value, return_type, is_var_args);
Expand Down Expand Up @@ -14978,7 +14971,7 @@ static IrInstGen *ir_analyze_struct_literal_to_array(IrAnalyze *ira, IrInst* sou

if ((err = type_resolve(ira->codegen, wanted_type, ResolveStatusSizeKnown)))
return ira->codegen->invalid_inst_gen;

size_t array_len = wanted_type->data.array.len;
size_t instr_field_count = actual_type->data.structure.src_field_count;
assert(array_len == instr_field_count);
Expand Down Expand Up @@ -20953,44 +20946,42 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
inst_fn_type_id.alignment = align_bytes;
}

if (fn_proto_node->data.fn_proto.return_anytype_token == nullptr) {
AstNode *return_type_node = fn_proto_node->data.fn_proto.return_type;
ZigType *specified_return_type = ir_analyze_type_expr(ira, impl_fn->child_scope, return_type_node);
if (type_is_invalid(specified_return_type))
return ira->codegen->invalid_inst_gen;

if(!is_valid_return_type(specified_return_type)){
ErrorMsg *msg = ir_add_error(ira, source_instr,
buf_sprintf("call to generic function with %s return type '%s' not allowed", type_id_name(specified_return_type->id), buf_ptr(&specified_return_type->name)));
add_error_note(ira->codegen, msg, fn_proto_node, buf_sprintf("function declared here"));
AstNode *return_type_node = fn_proto_node->data.fn_proto.return_type;
ZigType *specified_return_type = ir_analyze_type_expr(ira, impl_fn->child_scope, return_type_node);
if (type_is_invalid(specified_return_type))
return ira->codegen->invalid_inst_gen;

Tld *tld = find_decl(ira->codegen, &fn_entry->fndef_scope->base, &specified_return_type->name);
if (tld != nullptr) {
add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("type declared here"));
}
return ira->codegen->invalid_inst_gen;
}
if(!is_valid_return_type(specified_return_type)){
ErrorMsg *msg = ir_add_error(ira, source_instr,
buf_sprintf("call to generic function with %s return type '%s' not allowed", type_id_name(specified_return_type->id), buf_ptr(&specified_return_type->name)));
add_error_note(ira->codegen, msg, fn_proto_node, buf_sprintf("function declared here"));

if (fn_proto_node->data.fn_proto.auto_err_set) {
ZigType *inferred_err_set_type = get_auto_err_set_type(ira->codegen, impl_fn);
if ((err = type_resolve(ira->codegen, specified_return_type, ResolveStatusSizeKnown)))
return ira->codegen->invalid_inst_gen;
inst_fn_type_id.return_type = get_error_union_type(ira->codegen, inferred_err_set_type, specified_return_type);
} else {
inst_fn_type_id.return_type = specified_return_type;
Tld *tld = find_decl(ira->codegen, &fn_entry->fndef_scope->base, &specified_return_type->name);
if (tld != nullptr) {
add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("type declared here"));
}
return ira->codegen->invalid_inst_gen;
}

switch (type_requires_comptime(ira->codegen, specified_return_type)) {
case ReqCompTimeYes:
// Throw out our work and call the function as if it were comptime.
return ir_analyze_fn_call(ira, source_instr, fn_entry, fn_type, fn_ref, first_arg_ptr,
first_arg_ptr_src, CallModifierCompileTime, new_stack, new_stack_src, is_async_call_builtin,
args_ptr, args_len, ret_ptr, call_result_loc);
case ReqCompTimeInvalid:
if (fn_proto_node->data.fn_proto.auto_err_set) {
ZigType *inferred_err_set_type = get_auto_err_set_type(ira->codegen, impl_fn);
if ((err = type_resolve(ira->codegen, specified_return_type, ResolveStatusSizeKnown)))
return ira->codegen->invalid_inst_gen;
case ReqCompTimeNo:
break;
}
inst_fn_type_id.return_type = get_error_union_type(ira->codegen, inferred_err_set_type, specified_return_type);
} else {
inst_fn_type_id.return_type = specified_return_type;
}

switch (type_requires_comptime(ira->codegen, specified_return_type)) {
case ReqCompTimeYes:
// Throw out our work and call the function as if it were comptime.
return ir_analyze_fn_call(ira, source_instr, fn_entry, fn_type, fn_ref, first_arg_ptr,
first_arg_ptr_src, CallModifierCompileTime, new_stack, new_stack_src, is_async_call_builtin,
args_ptr, args_len, ret_ptr, call_result_loc);
case ReqCompTimeInvalid:
return ira->codegen->invalid_inst_gen;
case ReqCompTimeNo:
break;
}

auto existing_entry = ira->codegen->generic_table.put_unique(generic_id, impl_fn);
Expand Down
25 changes: 11 additions & 14 deletions src/stage1/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,21 +820,19 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc) {
AstNode *align_expr = ast_parse_byte_align(pc);
AstNode *section_expr = ast_parse_link_section(pc);
AstNode *callconv_expr = ast_parse_callconv(pc);
Token *anytype = eat_token_if(pc, TokenIdKeywordAnyType);
Token *exmark = nullptr;
AstNode *return_type = nullptr;
if (anytype == nullptr) {
exmark = eat_token_if(pc, TokenIdBang);
return_type = ast_parse_type_expr(pc);
if (return_type == nullptr) {
Token *next = peek_token(pc);
ast_error(
pc,
next,
"expected return type (use 'void' to return nothing), found: '%s'",
token_name(next->id)
);
}

exmark = eat_token_if(pc, TokenIdBang);
return_type = ast_parse_type_expr(pc);
if (return_type == nullptr) {
Token *next = peek_token(pc);
ast_error(
pc,
next,
"expected return type (use 'void' to return nothing), found: '%s'",
token_name(next->id)
);
}

AstNode *res = ast_create_node(pc, NodeTypeFnProto, first);
Expand All @@ -844,7 +842,6 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc) {
res->data.fn_proto.align_expr = align_expr;
res->data.fn_proto.section_expr = section_expr;
res->data.fn_proto.callconv_expr = callconv_expr;
res->data.fn_proto.return_anytype_token = anytype;
res->data.fn_proto.auto_err_set = exmark != nullptr;
res->data.fn_proto.return_type = return_type;

Expand Down

0 comments on commit 742b2ca

Please sign in to comment.