authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2021-07-19 23:21:24+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-19 23:23:42-04:00
loge80702067959f99944adeb79a59c31699d7e278a
treec3d96f7aaaf583078420217face77b34cba022ac
parent00e944f7186810ac518d47b62a3e3cc8aa081cdf

Fixed wrong "unable to load" error for non-existing import files

- Changed ZIR encoding of `import` metadata from having instruction indexes to storing token indexes.

3 files changed, 44 insertions(+), 25 deletions(-)

src/AstGen.zig+13-9
......@@ -36,7 +36,7 @@ compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{},
3636fn_block: ?*GenZir = null,
3737/// Maps string table indexes to the first `@import` ZIR instruction
3838/// that uses this string as the operand.
39imports: std.AutoArrayHashMapUnmanaged(u32, Zir.Inst.Index) = .{},
39imports: std.AutoArrayHashMapUnmanaged(u32, ast.TokenIndex) = .{},
4040
4141const InnerError = error{ OutOfMemory, AnalysisFail };
4242
......@@ -132,8 +132,7 @@ pub fn generate(gpa: *Allocator, tree: ast.Tree) Allocator.Error!Zir {
132132 if (astgen.compile_errors.items.len == 0) {
133133 astgen.extra.items[err_index] = 0;
134134 } else {
135 try astgen.extra.ensureCapacity(gpa, astgen.extra.items.len +
136 1 + astgen.compile_errors.items.len *
135 try astgen.extra.ensureUnusedCapacity(gpa, 1 + astgen.compile_errors.items.len *
137136 @typeInfo(Zir.Inst.CompileErrors.Item).Struct.fields.len);
138137
139138 astgen.extra.items[err_index] = astgen.addExtraAssumeCapacity(Zir.Inst.CompileErrors{
......@@ -149,13 +148,20 @@ pub fn generate(gpa: *Allocator, tree: ast.Tree) Allocator.Error!Zir {
149148 if (astgen.imports.count() == 0) {
150149 astgen.extra.items[imports_index] = 0;
151150 } else {
152 try astgen.extra.ensureCapacity(gpa, astgen.extra.items.len +
153 @typeInfo(Zir.Inst.Imports).Struct.fields.len + astgen.imports.count());
151 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.Imports).Struct.fields.len +
152 astgen.imports.count() * @typeInfo(Zir.Inst.Imports.Item).Struct.fields.len);
154153
155154 astgen.extra.items[imports_index] = astgen.addExtraAssumeCapacity(Zir.Inst.Imports{
156155 .imports_len = @intCast(u32, astgen.imports.count()),
157156 });
158 astgen.extra.appendSliceAssumeCapacity(astgen.imports.values());
157
158 var it = astgen.imports.iterator();
159 while (it.next()) |entry| {
160 _ = astgen.addExtraAssumeCapacity(Zir.Inst.Imports.Item{
161 .name = entry.key_ptr.*,
162 .token = entry.value_ptr.*,
163 });
164 }
159165 }
160166
161167 return Zir{
......@@ -6986,12 +6992,10 @@ fn builtinCall(
69866992 const str_lit_token = main_tokens[operand_node];
69876993 const str = try astgen.strLitAsString(str_lit_token);
69886994 const result = try gz.addStrTok(.import, str.index, str_lit_token);
6989 if (gz.refToIndex(result)) |import_inst_index| {
69906995 const gop = try astgen.imports.getOrPut(astgen.gpa, str.index);
69916996 if (!gop.found_existing) {
6992 gop.value_ptr.* = import_inst_index;
6997 gop.value_ptr.* = str_lit_token;
69936998 }
6994 }
69956999 return rvalue(gz, rl, result, node);
69967000 },
69977001 .compile_log => {
src/Compilation.zig+12-9
......@@ -2315,7 +2315,7 @@ const AstGenSrc = union(enum) {
23152315 root,
23162316 import: struct {
23172317 importing_file: *Module.Scope.File,
2318 import_inst: Zir.Inst.Index,
2318 import_tok: std.zig.ast.TokenIndex,
23192319 },
23202320};
23212321
......@@ -2352,11 +2352,15 @@ fn workerAstGenFile(
23522352 assert(file.zir_loaded);
23532353 const imports_index = file.zir.extra[@enumToInt(Zir.ExtraIndex.imports)];
23542354 if (imports_index != 0) {
2355 const imports_len = file.zir.extra[imports_index];
2355 const extra = file.zir.extraData(Zir.Inst.Imports, imports_index);
2356 var import_i: u32 = 0;
2357 var extra_index = extra.end;
23562358
2357 for (file.zir.extra[imports_index + 1 ..][0..imports_len]) |import_inst| {
2358 const inst_data = file.zir.instructions.items(.data)[import_inst].str_tok;
2359 const import_path = inst_data.get(file.zir);
2359 while (import_i < extra.data.imports_len) : (import_i += 1) {
2360 const item = file.zir.extraData(Zir.Inst.Imports.Item, extra_index);
2361 extra_index = item.end;
2362
2363 const import_path = file.zir.nullTerminatedString(item.data.name);
23602364
23612365 const import_result = blk: {
23622366 const lock = comp.mutex.acquire();
......@@ -2370,7 +2374,7 @@ fn workerAstGenFile(
23702374 });
23712375 const sub_src: AstGenSrc = .{ .import = .{
23722376 .importing_file = file,
2373 .import_inst = import_inst,
2377 .import_tok = item.data.token,
23742378 } };
23752379 wg.start();
23762380 comp.thread_pool.spawn(workerAstGenFile, .{
......@@ -2602,12 +2606,11 @@ fn reportRetryableAstGenError(
26022606 },
26032607 .import => |info| blk: {
26042608 const importing_file = info.importing_file;
2605 const import_inst = info.import_inst;
2606 const inst_data = importing_file.zir.instructions.items(.data)[import_inst].str_tok;
2609
26072610 break :blk .{
26082611 .file_scope = importing_file,
26092612 .parent_decl_node = 0,
2610 .lazy = .{ .token_offset = inst_data.src_tok },
2613 .lazy = .{ .token_abs = info.import_tok },
26112614 };
26122615 },
26132616 };
src/Zir.zig+19-7
......@@ -138,11 +138,17 @@ pub fn renderAsTextToFile(
138138 const imports_index = scope_file.zir.extra[@enumToInt(ExtraIndex.imports)];
139139 if (imports_index != 0) {
140140 try fs_file.writeAll("Imports:\n");
141 const imports_len = scope_file.zir.extra[imports_index];
142 for (scope_file.zir.extra[imports_index + 1 ..][0..imports_len]) |import_inst| {
143 const inst_data = writer.code.instructions.items(.data)[import_inst].str_tok;
144 const src = inst_data.src();
145 const import_path = inst_data.get(writer.code);
141
142 const extra = scope_file.zir.extraData(Inst.Imports, imports_index);
143 var import_i: u32 = 0;
144 var extra_index = extra.end;
145
146 while (import_i < extra.data.imports_len) : (import_i += 1) {
147 const item = scope_file.zir.extraData(Inst.Imports.Item, extra_index);
148 extra_index = item.end;
149
150 const src: LazySrcLoc = .{ .token_abs = item.data.token };
151 const import_path = scope_file.zir.nullTerminatedString(item.data.name);
146152 try fs_file.writer().print(" @import(\"{}\") ", .{
147153 std.zig.fmtEscapes(import_path),
148154 });
......@@ -2780,10 +2786,16 @@ pub const Inst = struct {
27802786 };
27812787 };
27822788
2783 /// Trailing: for each `imports_len` there is an instruction index
2784 /// to an import instruction.
2789 /// Trailing: for each `imports_len` there is an Item
27852790 pub const Imports = struct {
27862791 imports_len: Zir.Inst.Index,
2792
2793 pub const Item = struct {
2794 /// null terminated string index
2795 name: u32,
2796 /// points to the import name
2797 token: ast.TokenIndex,
2798 };
27872799 };
27882800};
27892801