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
signature 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 {
10261026
10271027 pub fn next(self: *TokenStream) Error!?Token {
10281028 if (self.token) |token| {
1029 // TODO: Audit this pattern once #2915 is closed
1030 const copy = token;
10311029 self.token = null;
1032 return copy;
1030 return token;
10331031 }
10341032
10351033 var t1: ?Token = undefined;
lib/std/zig/tokenizer.zig+2-6
......@@ -414,10 +414,8 @@ pub const Tokenizer = struct {
414414
415415 pub fn next(self: *Tokenizer) Token {
416416 if (self.pending_invalid_token) |token| {
417 // TODO: Audit this pattern once #2915 is closed
418 const copy = token;
419417 self.pending_invalid_token = null;
420 return copy;
418 return token;
421419 }
422420 const start_index = self.index;
423421 var state = State.Start;
......@@ -1270,10 +1268,8 @@ pub const Tokenizer = struct {
12701268
12711269 if (result.id == Token.Id.Eof) {
12721270 if (self.pending_invalid_token) |token| {
1273 // TODO: Audit this pattern once #2915 is closed
1274 const copy = token;
12751271 self.pending_invalid_token = null;
1276 return copy;
1272 return token;
12771273 }
12781274 }
12791275
src/ir.cpp+57-30
......@@ -20642,12 +20642,12 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
2064220642 if (type_is_invalid(array_ptr->value->type))
2064320643 return ira->codegen->invalid_inst_gen;
2064420644
20645 ZigValue *orig_array_ptr_val = array_ptr->value;
20646
2064720645 IrInstGen *elem_index = elem_ptr_instruction->elem_index->child;
2064820646 if (type_is_invalid(elem_index->value->type))
2064920647 return ira->codegen->invalid_inst_gen;
2065020648
20649 ZigValue *orig_array_ptr_val = array_ptr->value;
20650
2065120651 ZigType *ptr_type = orig_array_ptr_val->type;
2065220652 assert(ptr_type->id == ZigTypeIdPointer);
2065320653
......@@ -20657,23 +20657,25 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
2065720657 // We will adjust return_type's alignment before returning it.
2065820658 ZigType *return_type;
2065920659
20660 if (type_is_invalid(array_type)) {
20660 if (type_is_invalid(array_type))
2066120661 return ira->codegen->invalid_inst_gen;
20662 } else if (array_type->id == ZigTypeIdArray ||
20663 (array_type->id == ZigTypeIdPointer &&
20664 array_type->data.pointer.ptr_len == PtrLenSingle &&
20665 array_type->data.pointer.child_type->id == ZigTypeIdArray))
20662
20663 if (array_type->id == ZigTypeIdPointer &&
20664 array_type->data.pointer.ptr_len == PtrLenSingle &&
20665 array_type->data.pointer.child_type->id == ZigTypeIdArray)
2066620666 {
20667 if (array_type->id == ZigTypeIdPointer) {
20668 array_type = array_type->data.pointer.child_type;
20669 ptr_type = ptr_type->data.pointer.child_type;
20670 if (orig_array_ptr_val->special != ConstValSpecialRuntime) {
20671 orig_array_ptr_val = const_ptr_pointee(ira, ira->codegen, orig_array_ptr_val,
20672 elem_ptr_instruction->base.base.source_node);
20673 if (orig_array_ptr_val == nullptr)
20674 return ira->codegen->invalid_inst_gen;
20675 }
20676 }
20667 IrInstGen *ptr_value = ir_get_deref(ira, &elem_ptr_instruction->base.base,
20668 array_ptr, nullptr);
20669 if (type_is_invalid(ptr_value->value->type))
20670 return ira->codegen->invalid_inst_gen;
20671
20672 array_type = array_type->data.pointer.child_type;
20673 ptr_type = ptr_type->data.pointer.child_type;
20674
20675 orig_array_ptr_val = ptr_value->value;
20676 }
20677
20678 if (array_type->id == ZigTypeIdArray) {
2067720679 if (array_type->data.array.len == 0) {
2067820680 ir_add_error_node(ira, elem_ptr_instruction->base.base.source_node,
2067920681 buf_sprintf("index 0 outside array of size 0"));
......@@ -20811,8 +20813,14 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
2081120813 orig_array_ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr &&
2081220814 (orig_array_ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar || array_type->id == ZigTypeIdArray))
2081320815 {
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
2081420822 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);
2081620824 if (array_ptr_val == nullptr)
2081720825 return ira->codegen->invalid_inst_gen;
2081820826
......@@ -23674,14 +23682,13 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
2367423682 if ((err = type_resolve(ira->codegen, type_info_fn_decl_inline_type, ResolveStatusSizeKnown)))
2367523683 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
2367823689 auto decl_it = decls_scope->decl_table.entry_iterator();
2367923690 decltype(decls_scope->decl_table)::Entry *curr_entry = nullptr;
23680 int declaration_count = 0;
23681
2368223691 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);
2368523692 if (curr_entry->value->resolution == TldResolutionInvalid) {
2368623693 return ErrorSemanticAnalyzeFail;
2368723694 }
......@@ -23691,16 +23698,36 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
2369123698 return ErrorSemanticAnalyzeFail;
2369223699 }
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) {
2369423720 // Skip comptime blocks and test functions.
23695 if (curr_entry->value->id != TldIdCompTime) {
23696 if (curr_entry->value->id == TldIdFn) {
23697 ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
23698 if (fn_entry->is_test)
23699 continue;
23700 }
23721 if (curr_entry->value->id == TldIdCompTime)
23722 continue;
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;
2370323728 }
23729
23730 declaration_count += 1;
2370423731 }
2370523732
2370623733 ZigValue *declaration_array = ira->codegen->pass1_arena->create<ZigValue>();
test/compile_errors.zig+9
......@@ -3,6 +3,15 @@ const builtin = @import("builtin");
33const Target = @import("std").Target;
44
55pub 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
615 cases.addTest("duplicate field in anonymous struct literal",
716 \\export fn entry() void {
817 \\ const anon = .{