Skip to content

Commit 3d0291a

Browse files
committed
ir: Avoid invalidating the decl_table iterator
Collect the declarations to resolve first and run resolve_top_level_decl on them later. Closes #4310
1 parent 59a243c commit 3d0291a

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

src/ir.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23654,13 +23654,15 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
2365423654
return err;
2365523655

2365623656
// Loop through our declarations once to figure out how many declarations we will generate info for.
23657+
// The unresolved declarations are collected in a separate queue to avoid
23658+
// modifying decl_table while iterating over it
23659+
ZigList<Tld*> resolve_decl_queue{};
23660+
2365723661
auto decl_it = decls_scope->decl_table.entry_iterator();
2365823662
decltype(decls_scope->decl_table)::Entry *curr_entry = nullptr;
23659-
int declaration_count = 0;
2366023663

23664+
int declaration_count = 0;
2366123665
while ((curr_entry = decl_it.next()) != nullptr) {
23662-
// If the declaration is unresolved, force it to be resolved again.
23663-
resolve_top_level_decl(ira->codegen, curr_entry->value, curr_entry->value->source_node, false);
2366423666
if (curr_entry->value->resolution == TldResolutionInvalid) {
2366523667
return ErrorSemanticAnalyzeFail;
2366623668
}
@@ -23671,17 +23673,32 @@ static Error ir_make_type_info_decls(IrAnalyze *ira, IrInst* source_instr, ZigVa
2367123673
}
2367223674

2367323675
// Skip comptime blocks and test functions.
23674-
if (curr_entry->value->id != TldIdCompTime) {
23675-
if (curr_entry->value->id == TldIdFn) {
23676-
ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
23677-
if (fn_entry->is_test)
23678-
continue;
23679-
}
23676+
if (curr_entry->value->id == TldIdCompTime)
23677+
continue;
23678+
23679+
if (curr_entry->value->id == TldIdFn) {
23680+
ZigFn *fn_entry = ((TldFn *)curr_entry->value)->fn_entry;
23681+
if (fn_entry->is_test)
23682+
continue;
23683+
}
2368023684

23681-
declaration_count += 1;
23685+
declaration_count += 1;
23686+
23687+
// If the declaration is unresolved, force it to be resolved again.
23688+
if (curr_entry->value->resolution == TldResolutionUnresolved)
23689+
resolve_decl_queue.append(curr_entry->value);
23690+
}
23691+
23692+
for (size_t i = 0; i < resolve_decl_queue.length; i++) {
23693+
Tld *decl = resolve_decl_queue.at(i);
23694+
resolve_top_level_decl(ira->codegen, decl, decl->source_node, false);
23695+
if (decl->resolution == TldResolutionInvalid) {
23696+
return ErrorSemanticAnalyzeFail;
2368223697
}
2368323698
}
2368423699

23700+
resolve_decl_queue.deinit();
23701+
2368523702
ZigValue *declaration_array = ira->codegen->pass1_arena->create<ZigValue>();
2368623703
declaration_array->special = ConstValSpecialStatic;
2368723704
declaration_array->type = get_array_type(ira->codegen, type_info_declaration_type, declaration_count, nullptr);

test/compile_errors.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ const builtin = @import("builtin");
33
const Target = @import("std").Target;
44

55
pub fn addCases(cases: *tests.CompileErrorContext) void {
6-
cases.addTest("access of undefined pointer to array",
7-
\\const ram_u32: *[4096]u32 = undefined;
8-
\\export fn entry() void {
9-
\\ @ptrCast(*u32, &(ram_u32[0])) = &(ram_u32[0]);
6+
cases.addTest("",
7+
\\const A = B;
8+
\\test "Crash" {
9+
\\ _ = @typeInfo(@This()).Struct.decls;
1010
\\}
1111
, &[_][]const u8{
12-
"tmp.zig:3:29: error: use of undefined value here causes undefined behavior",
12+
"tmp.zig:1:11: error: use of undeclared identifier 'B'",
1313
});
1414

1515
cases.addTest("duplicate field in anonymous struct literal",

0 commit comments

Comments
 (0)