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) = .{},...@@ -36,7 +36,7 @@ compile_errors: ArrayListUnmanaged(Zir.Inst.CompileErrors.Item) = .{},
36fn_block: ?*GenZir = null,36fn_block: ?*GenZir = null,
37/// Maps string table indexes to the first `@import` ZIR instruction37/// Maps string table indexes to the first `@import` ZIR instruction
38/// that uses this string as the operand.38/// that uses this string as the operand.
39imports: std.AutoArrayHashMapUnmanaged(u32, Zir.Inst.Index) = .{},39imports: std.AutoArrayHashMapUnmanaged(u32, ast.TokenIndex) = .{},
4040
41const InnerError = error{ OutOfMemory, AnalysisFail };41const InnerError = error{ OutOfMemory, AnalysisFail };
4242
...@@ -132,8 +132,7 @@ pub fn generate(gpa: *Allocator, tree: ast.Tree) Allocator.Error!Zir {...@@ -132,8 +132,7 @@ pub fn generate(gpa: *Allocator, tree: ast.Tree) Allocator.Error!Zir {
132 if (astgen.compile_errors.items.len == 0) {132 if (astgen.compile_errors.items.len == 0) {
133 astgen.extra.items[err_index] = 0;133 astgen.extra.items[err_index] = 0;
134 } else {134 } else {
135 try astgen.extra.ensureCapacity(gpa, astgen.extra.items.len +135 try astgen.extra.ensureUnusedCapacity(gpa, 1 + astgen.compile_errors.items.len *
136 1 + astgen.compile_errors.items.len *
137 @typeInfo(Zir.Inst.CompileErrors.Item).Struct.fields.len);136 @typeInfo(Zir.Inst.CompileErrors.Item).Struct.fields.len);
138137
139 astgen.extra.items[err_index] = astgen.addExtraAssumeCapacity(Zir.Inst.CompileErrors{138 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 {...@@ -149,13 +148,20 @@ pub fn generate(gpa: *Allocator, tree: ast.Tree) Allocator.Error!Zir {
149 if (astgen.imports.count() == 0) {148 if (astgen.imports.count() == 0) {
150 astgen.extra.items[imports_index] = 0;149 astgen.extra.items[imports_index] = 0;
151 } else {150 } else {
152 try astgen.extra.ensureCapacity(gpa, astgen.extra.items.len +151 try astgen.extra.ensureUnusedCapacity(gpa, @typeInfo(Zir.Inst.Imports).Struct.fields.len +
153 @typeInfo(Zir.Inst.Imports).Struct.fields.len + astgen.imports.count());152 astgen.imports.count() * @typeInfo(Zir.Inst.Imports.Item).Struct.fields.len);
154153
155 astgen.extra.items[imports_index] = astgen.addExtraAssumeCapacity(Zir.Inst.Imports{154 astgen.extra.items[imports_index] = astgen.addExtraAssumeCapacity(Zir.Inst.Imports{
156 .imports_len = @intCast(u32, astgen.imports.count()),155 .imports_len = @intCast(u32, astgen.imports.count()),
157 });156 });
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 }
159 }165 }
160166
161 return Zir{167 return Zir{
...@@ -6986,12 +6992,10 @@ fn builtinCall(...@@ -6986,12 +6992,10 @@ fn builtinCall(
6986 const str_lit_token = main_tokens[operand_node];6992 const str_lit_token = main_tokens[operand_node];
6987 const str = try astgen.strLitAsString(str_lit_token);6993 const str = try astgen.strLitAsString(str_lit_token);
6988 const result = try gz.addStrTok(.import, str.index, str_lit_token);6994 const result = try gz.addStrTok(.import, str.index, str_lit_token);
6989 if (gz.refToIndex(result)) |import_inst_index| {
6990 const gop = try astgen.imports.getOrPut(astgen.gpa, str.index);6995 const gop = try astgen.imports.getOrPut(astgen.gpa, str.index);
6991 if (!gop.found_existing) {6996 if (!gop.found_existing) {
6992 gop.value_ptr.* = import_inst_index;6997 gop.value_ptr.* = str_lit_token;
6993 }6998 }
6994 }
6995 return rvalue(gz, rl, result, node);6999 return rvalue(gz, rl, result, node);
6996 },7000 },
6997 .compile_log => {7001 .compile_log => {
src/Compilation.zig+12-9
...@@ -2315,7 +2315,7 @@ const AstGenSrc = union(enum) {...@@ -2315,7 +2315,7 @@ const AstGenSrc = union(enum) {
2315 root,2315 root,
2316 import: struct {2316 import: struct {
2317 importing_file: *Module.Scope.File,2317 importing_file: *Module.Scope.File,
2318 import_inst: Zir.Inst.Index,2318 import_tok: std.zig.ast.TokenIndex,
2319 },2319 },
2320};2320};
23212321
...@@ -2352,11 +2352,15 @@ fn workerAstGenFile(...@@ -2352,11 +2352,15 @@ fn workerAstGenFile(
2352 assert(file.zir_loaded);2352 assert(file.zir_loaded);
2353 const imports_index = file.zir.extra[@enumToInt(Zir.ExtraIndex.imports)];2353 const imports_index = file.zir.extra[@enumToInt(Zir.ExtraIndex.imports)];
2354 if (imports_index != 0) {2354 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| {2359 while (import_i < extra.data.imports_len) : (import_i += 1) {
2358 const inst_data = file.zir.instructions.items(.data)[import_inst].str_tok;2360 const item = file.zir.extraData(Zir.Inst.Imports.Item, extra_index);
2359 const import_path = inst_data.get(file.zir);2361 extra_index = item.end;
2362
2363 const import_path = file.zir.nullTerminatedString(item.data.name);
23602364
2361 const import_result = blk: {2365 const import_result = blk: {
2362 const lock = comp.mutex.acquire();2366 const lock = comp.mutex.acquire();
...@@ -2370,7 +2374,7 @@ fn workerAstGenFile(...@@ -2370,7 +2374,7 @@ fn workerAstGenFile(
2370 });2374 });
2371 const sub_src: AstGenSrc = .{ .import = .{2375 const sub_src: AstGenSrc = .{ .import = .{
2372 .importing_file = file,2376 .importing_file = file,
2373 .import_inst = import_inst,2377 .import_tok = item.data.token,
2374 } };2378 } };
2375 wg.start();2379 wg.start();
2376 comp.thread_pool.spawn(workerAstGenFile, .{2380 comp.thread_pool.spawn(workerAstGenFile, .{
...@@ -2602,12 +2606,11 @@ fn reportRetryableAstGenError(...@@ -2602,12 +2606,11 @@ fn reportRetryableAstGenError(
2602 },2606 },
2603 .import => |info| blk: {2607 .import => |info| blk: {
2604 const importing_file = info.importing_file;2608 const importing_file = info.importing_file;
2605 const import_inst = info.import_inst;2609
2606 const inst_data = importing_file.zir.instructions.items(.data)[import_inst].str_tok;
2607 break :blk .{2610 break :blk .{
2608 .file_scope = importing_file,2611 .file_scope = importing_file,
2609 .parent_decl_node = 0,2612 .parent_decl_node = 0,
2610 .lazy = .{ .token_offset = inst_data.src_tok },2613 .lazy = .{ .token_abs = info.import_tok },
2611 };2614 };
2612 },2615 },
2613 };2616 };
src/Zir.zig+19-7
...@@ -138,11 +138,17 @@ pub fn renderAsTextToFile(...@@ -138,11 +138,17 @@ pub fn renderAsTextToFile(
138 const imports_index = scope_file.zir.extra[@enumToInt(ExtraIndex.imports)];138 const imports_index = scope_file.zir.extra[@enumToInt(ExtraIndex.imports)];
139 if (imports_index != 0) {139 if (imports_index != 0) {
140 try fs_file.writeAll("Imports:\n");140 try fs_file.writeAll("Imports:\n");
141 const imports_len = scope_file.zir.extra[imports_index];141
142 for (scope_file.zir.extra[imports_index + 1 ..][0..imports_len]) |import_inst| {142 const extra = scope_file.zir.extraData(Inst.Imports, imports_index);
143 const inst_data = writer.code.instructions.items(.data)[import_inst].str_tok;143 var import_i: u32 = 0;
144 const src = inst_data.src();144 var extra_index = extra.end;
145 const import_path = inst_data.get(writer.code);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);
146 try fs_file.writer().print(" @import(\"{}\") ", .{152 try fs_file.writer().print(" @import(\"{}\") ", .{
147 std.zig.fmtEscapes(import_path),153 std.zig.fmtEscapes(import_path),
148 });154 });
...@@ -2780,10 +2786,16 @@ pub const Inst = struct {...@@ -2780,10 +2786,16 @@ pub const Inst = struct {
2780 };2786 };
2781 };2787 };
27822788
2783 /// Trailing: for each `imports_len` there is an instruction index2789 /// Trailing: for each `imports_len` there is an Item
2784 /// to an import instruction.
2785 pub const Imports = struct {2790 pub const Imports = struct {
2786 imports_len: Zir.Inst.Index,2791 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 };
2787 };2799 };
2788};2800};
27892801