authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-18 18:15:11-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-02-18 18:15:11-05:00
logccca4b5a5e98dd96aaeb365af6c1cfbe87dca181
tree9fef52c01f3f77fe25896196cc6c6b03cd5be832
parent7f92d0d4a44258a661a5dabf9800210f5ee31484
parent6b74fd2e12b117e90197db5f4fa1ea51aa246248
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #4474 from LemonBoy/saukerkraut

Patches

4 files changed, 69 insertions(+), 39 deletions(-)

lib/std/json.zig+1-3
...@@ -1026,10 +1026,8 @@ pub const TokenStream = struct {...@@ -1026,10 +1026,8 @@ pub const TokenStream = struct {
10261026
1027 pub fn next(self: *TokenStream) Error!?Token {1027 pub fn next(self: *TokenStream) Error!?Token {
1028 if (self.token) |token| {1028 if (self.token) |token| {
1029 // TODO: Audit this pattern once #2915 is closed
1030 const copy = token;
1031 self.token = null;1029 self.token = null;
1032 return copy;1030 return token;
1033 }1031 }
10341032
1035 var t1: ?Token = undefined;1033 var t1: ?Token = undefined;
lib/std/zig/tokenizer.zig+2-6
...@@ -414,10 +414,8 @@ pub const Tokenizer = struct {...@@ -414,10 +414,8 @@ pub const Tokenizer = struct {
414414
415 pub fn next(self: *Tokenizer) Token {415 pub fn next(self: *Tokenizer) Token {
416 if (self.pending_invalid_token) |token| {416 if (self.pending_invalid_token) |token| {
417 // TODO: Audit this pattern once #2915 is closed
418 const copy = token;
419 self.pending_invalid_token = null;417 self.pending_invalid_token = null;
420 return copy;418 return token;
421 }419 }
422 const start_index = self.index;420 const start_index = self.index;
423 var state = State.Start;421 var state = State.Start;
...@@ -1270,10 +1268,8 @@ pub const Tokenizer = struct {...@@ -1270,10 +1268,8 @@ pub const Tokenizer = struct {
12701268
1271 if (result.id == Token.Id.Eof) {1269 if (result.id == Token.Id.Eof) {
1272 if (self.pending_invalid_token) |token| {1270 if (self.pending_invalid_token) |token| {
1273 // TODO: Audit this pattern once #2915 is closed
1274 const copy = token;
1275 self.pending_invalid_token = null;1271 self.pending_invalid_token = null;
1276 return copy;1272 return token;
1277 }1273 }
1278 }1274 }
12791275
src/ir.cpp+57-30
...@@ -20642,12 +20642,12 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP...@@ -20642,12 +20642,12 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
20642 if (type_is_invalid(array_ptr->value->type))20642 if (type_is_invalid(array_ptr->value->type))
20643 return ira->codegen->invalid_inst_gen;20643 return ira->codegen->invalid_inst_gen;
2064420644
20645 ZigValue *orig_array_ptr_val = array_ptr->value;
20646
20647 IrInstGen *elem_index = elem_ptr_instruction->elem_index->child;20645 IrInstGen *elem_index = elem_ptr_instruction->elem_index->child;
20648 if (type_is_invalid(elem_index->value->type))20646 if (type_is_invalid(elem_index->value->type))
20649 return ira->codegen->invalid_inst_gen;20647 return ira->codegen->invalid_inst_gen;
2065020648
20649 ZigValue *orig_array_ptr_val = array_ptr->value;
20650
20651 ZigType *ptr_type = orig_array_ptr_val->type;20651 ZigType *ptr_type = orig_array_ptr_val->type;
20652 assert(ptr_type->id == ZigTypeIdPointer);20652 assert(ptr_type->id == ZigTypeIdPointer);
2065320653
...@@ -20657,23 +20657,25 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP...@@ -20657,23 +20657,25 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
20657 // We will adjust return_type's alignment before returning it.20657 // We will adjust return_type's alignment before returning it.
20658 ZigType *return_type;20658 ZigType *return_type;
2065920659
20660 if (type_is_invalid(array_type)) {20660 if (type_is_invalid(array_type))
20661 return ira->codegen->invalid_inst_gen;20661 return ira->codegen->invalid_inst_gen;
20662 } else if (array_type->id == ZigTypeIdArray ||20662
20663 (array_type->id == ZigTypeIdPointer &&20663 if (array_type->id == ZigTypeIdPointer &&
20664 array_type->data.pointer.ptr_len == PtrLenSingle &&20664 array_type->data.pointer.ptr_len == PtrLenSingle &&
20665 array_type->data.pointer.child_type->id == ZigTypeIdArray))20665 array_type->data.pointer.child_type->id == ZigTypeIdArray)
20666 {20666 {
20667 if (array_type->id == ZigTypeIdPointer) {20667 IrInstGen *ptr_value = ir_get_deref(ira, &elem_ptr_instruction->base.base,
20668 array_type = array_type->data.pointer.child_type;20668 array_ptr, nullptr);
20669 ptr_type = ptr_type->data.pointer.child_type;20669 if (type_is_invalid(ptr_value->value->type))
20670 if (orig_array_ptr_val->special != ConstValSpecialRuntime) {20670 return ira->codegen->invalid_inst_gen;
20671 orig_array_ptr_val = const_ptr_pointee(ira, ira->codegen, orig_array_ptr_val,20671
20672 elem_ptr_instruction->base.base.source_node);20672 array_type = array_type->data.pointer.child_type;
20673 if (orig_array_ptr_val == nullptr)20673 ptr_type = ptr_type->data.pointer.child_type;
20674 return ira->codegen->invalid_inst_gen;20674
20675 }20675 orig_array_ptr_val = ptr_value->value;
20676 }20676 }
20677
20678 if (array_type->id == ZigTypeIdArray) {
20677 if (array_type->data.array.len == 0) {20679 if (array_type->data.array.len == 0) {
20678 ir_add_error_node(ira, elem_ptr_instruction->base.base.source_node,20680 ir_add_error_node(ira, elem_ptr_instruction->base.base.source_node,
20679 buf_sprintf("index 0 outside array of size 0"));20681 buf_sprintf("index 0 outside array of size 0"));
...@@ -20811,8 +20813,14 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP...@@ -20811,8 +20813,14 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
20811 orig_array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&20813 orig_array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
20812 (orig_array_ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar || array_type->id == ZigTypeIdArray))20814 (orig_array_ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar || array_type->id == ZigTypeIdArray))
20813 {20815 {
20816 if ((err = ir_resolve_const_val(ira->codegen, ira->new_irb.exec,
20817 elem_ptr_instruction->base.base.source_node, orig_array_ptr_val, UndefBad)))
20818 {
20819 return ira->codegen->invalid_inst_gen;
20820 }
20821
20814 ZigValue *array_ptr_val = const_ptr_pointee(ira, ira->codegen, orig_array_ptr_val,20822 ZigValue *array_ptr_val = const_ptr_pointee(ira, ira->codegen, orig_array_ptr_val,
20815 elem_ptr_instruction->base.base.source_node);20823 elem_ptr_instruction->base.base.source_node);
20816 if (array_ptr_val == nullptr)20824 if (array_ptr_val == nullptr)
20817 return ira->codegen->invalid_inst_gen;20825 return ira->codegen->invalid_inst_gen;
2081820826
...@@ -23674,14 +23682,13 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa...@@ -23674,14 +23682,13 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
23674 if ((err = type_resolve(ira->codegen, type_info_fn_decl_inline_type, ResolveStatusSizeKnown)))23682 if ((err = type_resolve(ira->codegen, type_info_fn_decl_inline_type, ResolveStatusSizeKnown)))
23675 return err;23683 return err;
2367623684
23677 // Loop through our declarations once to figure out how many declarations we will generate info for.23685 // The unresolved declarations are collected in a separate queue to avoid
23686 // modifying decl_table while iterating over it
23687 ZigList<Tld*> resolve_decl_queue{};
23688
23678 auto decl_it = decls_scope->decl_table.entry_iterator();23689 auto decl_it = decls_scope->decl_table.entry_iterator();
23679 decltype(decls_scope->decl_table)::Entry *curr_entry = nullptr;23690 decltype(decls_scope->decl_table)::Entry *curr_entry = nullptr;
23680 int declaration_count = 0;
23681
23682 while ((curr_entry = decl_it.next()) != nullptr) {23691 while ((curr_entry = decl_it.next()) != nullptr) {
23683 // If the declaration is unresolved, force it to be resolved again.
23684 resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node, false);
23685 if (curr_entry->value->resolution == TldResolutionInvalid) {23692 if (curr_entry->value->resolution == TldResolutionInvalid) {
23686 return ErrorSemanticAnalyzeFail;23693 return ErrorSemanticAnalyzeFail;
23687 }23694 }
...@@ -23691,16 +23698,36 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa...@@ -23691,16 +23698,36 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
23691 return ErrorSemanticAnalyzeFail;23698 return ErrorSemanticAnalyzeFail;
23692 }23699 }
2369323700
23701 // If the declaration is unresolved, force it to be resolved again.
23702 if (curr_entry->value->resolution == TldResolutionUnresolved)
23703 resolve_decl_queue.append(curr_entry->value);
23704 }
23705
23706 for (size_t i = 0; i < resolve_decl_queue.length; i++) {
23707 Tld *decl = resolve_decl_queue.at(i);
23708 resolve_top_level_decl(ira->codegen, decl, decl->source_node, false);
23709 if (decl->resolution == TldResolutionInvalid) {
23710 return ErrorSemanticAnalyzeFail;
23711 }
23712 }
23713
23714 resolve_decl_queue.deinit();
23715
23716 // Loop through our declarations once to figure out how many declarations we will generate info for.
23717 int declaration_count = 0;
23718 decl_it = decls_scope->decl_table.entry_iterator();
23719 while ((curr_entry = decl_it.next()) != nullptr) {
23694 // Skip comptime blocks and test functions.23720 // Skip comptime blocks and test functions.
23695 if (curr_entry->value->id != TldIdCompTime) {23721 if (curr_entry->value->id == TldIdCompTime)
23696 if (curr_entry->value->id == TldIdFn) {23722 continue;
23697 ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
23698 if (fn_entry->is_test)
23699 continue;
23700 }
2370123723
23702 declaration_count += 1;23724 if (curr_entry->value->id == TldIdFn) {
23725 ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
23726 if (fn_entry->is_test)
23727 continue;
23703 }23728 }
23729
23730 declaration_count += 1;
23704 }23731 }
2370523732
23706 ZigValue *declaration_array = ira->codegen->pass1_arena->create<ZigValue>();23733 ZigValue *declaration_array = ira->codegen->pass1_arena->create<ZigValue>();
test/compile_errors.zig+9
...@@ -3,6 +3,15 @@ const builtin = @import("builtin");...@@ -3,6 +3,15 @@ const builtin = @import("builtin");
3const Target = @import("std").Target;3const Target = @import("std").Target;
44
5pub fn addCases(cases: *tests.CompileErrorContext) void {5pub fn addCases(cases: *tests.CompileErrorContext) void {
6 cases.addTest("",
7 \\const A = B;
8 \\test "Crash" {
9 \\ _ = @typeInfo(@This()).Struct.decls;
10 \\}
11 , &[_][]const u8{
12 "tmp.zig:1:11: error: use of undeclared identifier 'B'",
13 });
14
6 cases.addTest("duplicate field in anonymous struct literal",15 cases.addTest("duplicate field in anonymous struct literal",
7 \\export fn entry() void {16 \\export fn entry() void {
8 \\ const anon = .{17 \\ const anon = .{