authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-07 15:32:02-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-04-07 15:32:02-04:00
log6715c54cc641e61405bb72d286fe2cf560447b56
tree6b62ce5b82c65595a13f6cf93335a4bfaa73dc8e
parent7c38651a65b48b8cc683bc31a40952d9b469ec4d
parentfae0c35195076c8bce9da3528ce8d1959902ca06
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #2205 from kristate/zig-backport-issue532

stage1: create ir.cpp specific `analyze_type_expr`

2 files changed, 42 insertions(+), 5 deletions(-)

src/ir.cpp+27-5
......@@ -356,6 +356,28 @@ static void ir_ref_var(ZigVar *var) {
356356 var->ref_count += 1;
357357}
358358
359ZigType *ir_analyze_type_expr(IrAnalyze *ira, Scope *scope, AstNode *node) {
360 ConstExprValue *result = ir_eval_const_value( ira->codegen
361 , scope
362 , node
363 , ira->codegen->builtin_types.entry_type
364 , ira->new_irb.exec->backward_branch_count
365 , ira->new_irb.exec->backward_branch_quota
366 , nullptr
367 , nullptr
368 , node
369 , nullptr
370 , ira->new_irb.exec
371 , nullptr
372 );
373
374 if (type_is_invalid(result->type))
375 return ira->codegen->builtin_types.entry_invalid;
376
377 assert(result->special != ConstValSpecialRuntime);
378 return result->data.x_type;
379}
380
359381static IrBasicBlock *ir_create_basic_block(IrBuilder *irb, Scope *scope, const char *name_hint) {
360382 IrBasicBlock *result = allocate<IrBasicBlock>(1);
361383 result->scope = scope;
......@@ -13875,7 +13897,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node
1387513897 IrInstruction *casted_arg;
1387613898 if (param_decl_node->data.param_decl.var_token == nullptr) {
1387713899 AstNode *param_type_node = param_decl_node->data.param_decl.type;
13878 ZigType *param_type = analyze_type_expr(ira->codegen, *exec_scope, param_type_node);
13900 ZigType *param_type = ir_analyze_type_expr(ira, *exec_scope, param_type_node);
1387913901 if (type_is_invalid(param_type))
1388013902 return false;
1388113903
......@@ -13915,7 +13937,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
1391513937 } else {
1391613938 if (param_decl_node->data.param_decl.var_token == nullptr) {
1391713939 AstNode *param_type_node = param_decl_node->data.param_decl.type;
13918 ZigType *param_type = analyze_type_expr(ira->codegen, *child_scope, param_type_node);
13940 ZigType *param_type = ir_analyze_type_expr(ira, *child_scope, param_type_node);
1391913941 if (type_is_invalid(param_type))
1392013942 return false;
1392113943
......@@ -14296,7 +14318,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1429614318 }
1429714319
1429814320 AstNode *return_type_node = fn_proto_node->data.fn_proto.return_type;
14299 ZigType *specified_return_type = analyze_type_expr(ira->codegen, exec_scope, return_type_node);
14321 ZigType *specified_return_type = ir_analyze_type_expr(ira, exec_scope, return_type_node);
1430014322 if (type_is_invalid(specified_return_type))
1430114323 return ira->codegen->invalid_instruction;
1430214324 ZigType *return_type;
......@@ -14532,7 +14554,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1453214554
1453314555 if (fn_proto_node->data.fn_proto.return_var_token == nullptr) {
1453414556 AstNode *return_type_node = fn_proto_node->data.fn_proto.return_type;
14535 ZigType *specified_return_type = analyze_type_expr(ira->codegen, impl_fn->child_scope, return_type_node);
14557 ZigType *specified_return_type = ir_analyze_type_expr(ira, impl_fn->child_scope, return_type_node);
1453614558 if (type_is_invalid(specified_return_type))
1453714559 return ira->codegen->invalid_instruction;
1453814560 if (fn_proto_node->data.fn_proto.auto_err_set) {
......@@ -14559,7 +14581,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1455914581 if (call_instruction->is_async) {
1456014582 AstNode *async_allocator_type_node = fn_proto_node->data.fn_proto.async_allocator_type;
1456114583 if (async_allocator_type_node != nullptr) {
14562 ZigType *async_allocator_type = analyze_type_expr(ira->codegen, impl_fn->child_scope, async_allocator_type_node);
14584 ZigType *async_allocator_type = ir_analyze_type_expr(ira, impl_fn->child_scope, async_allocator_type_node);
1456314585 if (type_is_invalid(async_allocator_type))
1456414586 return ira->codegen->invalid_instruction;
1456514587 inst_fn_type_id.async_allocator_type = async_allocator_type;
test/compile_errors.zig+15
......@@ -2,6 +2,21 @@ const tests = @import("tests.zig");
22const builtin = @import("builtin");
33
44pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add(
6 "Generic function where return type is self-referenced",
7 \\fn Foo(comptime T: type) Foo(T) {
8 \\ return struct{ x: T };
9 \\}
10 \\export fn entry() void {
11 \\ const t = Foo(u32) {
12 \\ .x = 1
13 \\ };
14 \\}
15 ,
16 "tmp.zig:1:29: error: evaluation exceeded 1000 backwards branches",
17 "tmp.zig:1:29: note: called from here",
18 );
19
520 cases.add(
621 "@ptrToInt 0 to non optional pointer",
722 \\export fn entry() void {