authorgravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2019-04-08 05:30:32+02:00
committergravatar for jhc@dismail.deJimmi Holst Christensen <jhc@dismail.de> 2019-04-08 05:30:32+02:00
log43e219b0d960890d376240af1e0c135b6c70b353
tree7f1da89035be8b63bb4e2b718385a4556a9641ff
parentf86ea797ba1387c0cf06987f42a82b0afdff20b4
parent6a78b315b2112e372b48ba7399ea9cddadbe65b6

Merge branch 'master' of github.com:ziglang/zig


3 files changed, 49 insertions(+), 8 deletions(-)

src/ir.cpp+27-5
...@@ -356,6 +356,28 @@ static void ir_ref_var(ZigVar *var) {...@@ -356,6 +356,28 @@ static void ir_ref_var(ZigVar *var) {
356 var->ref_count += 1;356 var->ref_count += 1;
357}357}
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
359static IrBasicBlock *ir_create_basic_block(IrBuilder *irb, Scope *scope, const char *name_hint) {381static IrBasicBlock *ir_create_basic_block(IrBuilder *irb, Scope *scope, const char *name_hint) {
360 IrBasicBlock *result = allocate<IrBasicBlock>(1);382 IrBasicBlock *result = allocate<IrBasicBlock>(1);
361 result->scope = scope;383 result->scope = scope;
...@@ -13875,7 +13897,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node...@@ -13875,7 +13897,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node
13875 IrInstruction *casted_arg;13897 IrInstruction *casted_arg;
13876 if (param_decl_node->data.param_decl.var_token == nullptr) {13898 if (param_decl_node->data.param_decl.var_token == nullptr) {
13877 AstNode *param_type_node = param_decl_node->data.param_decl.type;13899 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);
13879 if (type_is_invalid(param_type))13901 if (type_is_invalid(param_type))
13880 return false;13902 return false;
1388113903
...@@ -13915,7 +13937,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod...@@ -13915,7 +13937,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
13915 } else {13937 } else {
13916 if (param_decl_node->data.param_decl.var_token == nullptr) {13938 if (param_decl_node->data.param_decl.var_token == nullptr) {
13917 AstNode *param_type_node = param_decl_node->data.param_decl.type;13939 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);
13919 if (type_is_invalid(param_type))13941 if (type_is_invalid(param_type))
13920 return false;13942 return false;
1392113943
...@@ -14296,7 +14318,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call...@@ -14296,7 +14318,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
14296 }14318 }
1429714319
14298 AstNode *return_type_node = fn_proto_node->data.fn_proto.return_type;14320 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);
14300 if (type_is_invalid(specified_return_type))14322 if (type_is_invalid(specified_return_type))
14301 return ira->codegen->invalid_instruction;14323 return ira->codegen->invalid_instruction;
14302 ZigType *return_type;14324 ZigType *return_type;
...@@ -14532,7 +14554,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call...@@ -14532,7 +14554,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
1453214554
14533 if (fn_proto_node->data.fn_proto.return_var_token == nullptr) {14555 if (fn_proto_node->data.fn_proto.return_var_token == nullptr) {
14534 AstNode *return_type_node = fn_proto_node->data.fn_proto.return_type;14556 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);
14536 if (type_is_invalid(specified_return_type))14558 if (type_is_invalid(specified_return_type))
14537 return ira->codegen->invalid_instruction;14559 return ira->codegen->invalid_instruction;
14538 if (fn_proto_node->data.fn_proto.auto_err_set) {14560 if (fn_proto_node->data.fn_proto.auto_err_set) {
...@@ -14559,7 +14581,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call...@@ -14559,7 +14581,7 @@ static IrInstruction *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *call
14559 if (call_instruction->is_async) {14581 if (call_instruction->is_async) {
14560 AstNode *async_allocator_type_node = fn_proto_node->data.fn_proto.async_allocator_type;14582 AstNode *async_allocator_type_node = fn_proto_node->data.fn_proto.async_allocator_type;
14561 if (async_allocator_type_node != nullptr) {14583 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);
14563 if (type_is_invalid(async_allocator_type))14585 if (type_is_invalid(async_allocator_type))
14564 return ira->codegen->invalid_instruction;14586 return ira->codegen->invalid_instruction;
14565 inst_fn_type_id.async_allocator_type = async_allocator_type;14587 inst_fn_type_id.async_allocator_type = async_allocator_type;
std/hash_map.zig+7-3
...@@ -175,7 +175,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3...@@ -175,7 +175,7 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
175 return hm.get(key) != null;175 return hm.get(key) != null;
176 }176 }
177177
178 pub fn remove(hm: *Self, key: K) ?*KV {178 pub fn remove(hm: *Self, key: K) ?KV {
179 if (hm.entries.len == 0) return null;179 if (hm.entries.len == 0) return null;
180 hm.incrementModificationCount();180 hm.incrementModificationCount();
181 const start_index = hm.keyToIndex(key);181 const start_index = hm.keyToIndex(key);
...@@ -189,13 +189,14 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3...@@ -189,13 +189,14 @@ pub fn HashMap(comptime K: type, comptime V: type, comptime hash: fn (key: K) u3
189189
190 if (!eql(entry.kv.key, key)) continue;190 if (!eql(entry.kv.key, key)) continue;
191191
192 const removed_kv = entry.kv;
192 while (roll_over < hm.entries.len) : (roll_over += 1) {193 while (roll_over < hm.entries.len) : (roll_over += 1) {
193 const next_index = (start_index + roll_over + 1) % hm.entries.len;194 const next_index = (start_index + roll_over + 1) % hm.entries.len;
194 const next_entry = &hm.entries[next_index];195 const next_entry = &hm.entries[next_index];
195 if (!next_entry.used or next_entry.distance_from_start_index == 0) {196 if (!next_entry.used or next_entry.distance_from_start_index == 0) {
196 entry.used = false;197 entry.used = false;
197 hm.size -= 1;198 hm.size -= 1;
198 return &entry.kv;199 return removed_kv;
199 }200 }
200 entry.* = next_entry.*;201 entry.* = next_entry.*;
201 entry.distance_from_start_index -= 1;202 entry.distance_from_start_index -= 1;
...@@ -371,7 +372,10 @@ test "basic hash map usage" {...@@ -371,7 +372,10 @@ test "basic hash map usage" {
371372
372 testing.expect(map.contains(2));373 testing.expect(map.contains(2));
373 testing.expect(map.get(2).?.value == 22);374 testing.expect(map.get(2).?.value == 22);
374 _ = map.remove(2);375
376 const rmv1 = map.remove(2);
377 testing.expect(rmv1.?.key == 2);
378 testing.expect(rmv1.?.value == 22);
375 testing.expect(map.remove(2) == null);379 testing.expect(map.remove(2) == null);
376 testing.expect(map.get(2) == null);380 testing.expect(map.get(2) == null);
377}381}
test/compile_errors.zig+15
...@@ -2,6 +2,21 @@ const tests = @import("tests.zig");...@@ -2,6 +2,21 @@ const tests = @import("tests.zig");
2const builtin = @import("builtin");2const builtin = @import("builtin");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub 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
5 cases.add(20 cases.add(
6 "@ptrToInt 0 to non optional pointer",21 "@ptrToInt 0 to non optional pointer",
7 \\export fn entry() void {22 \\export fn entry() void {