authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-24 19:09:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-11-24 19:10:58-07:00
loge266ede6e310b64adcf3912b8ef2b9e2397fde7b
tree45dc81753836fccb9343b8031214dea137bbe63a
parent0581756453a98d4e1100c684896783606dc86374

stage2: fix cleanup code for `@import` errors

When adding test coverage, I noticed an inconsistency in which source location the compile error was pointing to for `@embedFile` errors vs `@import` errors. They now both point to the same place, the string operand. closes #9404 closes #9939

3 files changed, 21 insertions(+), 11 deletions(-)

src/Module.zig+5-3
......@@ -3541,11 +3541,11 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
35413541 defer if (!keep_resolved_path) gpa.free(resolved_path);
35423542
35433543 const gop = try mod.import_table.getOrPut(gpa, resolved_path);
3544 errdefer _ = mod.import_table.pop();
35443545 if (gop.found_existing) return ImportFileResult{
35453546 .file = gop.value_ptr.*,
35463547 .is_new = false,
35473548 };
3548 keep_resolved_path = true; // It's now owned by import_table.
35493549
35503550 const sub_file_path = try gpa.dupe(u8, pkg.root_src_path);
35513551 errdefer gpa.free(sub_file_path);
......@@ -3553,6 +3553,7 @@ pub fn importPkg(mod: *Module, pkg: *Package) !ImportFileResult {
35533553 const new_file = try gpa.create(File);
35543554 errdefer gpa.destroy(new_file);
35553555
3556 keep_resolved_path = true; // It's now owned by import_table.
35563557 gop.value_ptr.* = new_file;
35573558 new_file.* = .{
35583559 .sub_file_path = sub_file_path,
......@@ -3599,11 +3600,11 @@ pub fn importFile(
35993600 defer if (!keep_resolved_path) gpa.free(resolved_path);
36003601
36013602 const gop = try mod.import_table.getOrPut(gpa, resolved_path);
3603 errdefer _ = mod.import_table.pop();
36023604 if (gop.found_existing) return ImportFileResult{
36033605 .file = gop.value_ptr.*,
36043606 .is_new = false,
36053607 };
3606 keep_resolved_path = true; // It's now owned by import_table.
36073608
36083609 const new_file = try gpa.create(File);
36093610 errdefer gpa.destroy(new_file);
......@@ -3622,6 +3623,7 @@ pub fn importFile(
36223623 resolved_root_path, resolved_path, sub_file_path, import_string,
36233624 });
36243625
3626 keep_resolved_path = true; // It's now owned by import_table.
36253627 gop.value_ptr.* = new_file;
36263628 new_file.* = .{
36273629 .sub_file_path = sub_file_path,
......@@ -3657,8 +3659,8 @@ pub fn embedFile(mod: *Module, cur_file: *File, rel_file_path: []const u8) !*Emb
36573659 defer if (!keep_resolved_path) gpa.free(resolved_path);
36583660
36593661 const gop = try mod.embed_table.getOrPut(gpa, resolved_path);
3660 if (gop.found_existing) return gop.value_ptr.*;
36613662 errdefer assert(mod.embed_table.remove(resolved_path));
3663 if (gop.found_existing) return gop.value_ptr.*;
36623664
36633665 const new_file = try gpa.create(EmbedFile);
36643666 errdefer gpa.destroy(new_file);
src/Sema.zig+7-7
......@@ -6807,17 +6807,17 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
68076807
68086808 const mod = sema.mod;
68096809 const inst_data = sema.code.instructions.items(.data)[inst].str_tok;
6810 const src = inst_data.src();
6810 const operand_src = inst_data.src();
68116811 const operand = inst_data.get(sema.code);
68126812
68136813 const result = mod.importFile(block.getFileScope(), operand) catch |err| switch (err) {
68146814 error.ImportOutsidePkgPath => {
6815 return sema.fail(block, src, "import of file outside package path: '{s}'", .{operand});
6815 return sema.fail(block, operand_src, "import of file outside package path: '{s}'", .{operand});
68166816 },
68176817 else => {
68186818 // TODO: these errors are file system errors; make sure an update() will
68196819 // retry this and not cache the file system error, which may be transient.
6820 return sema.fail(block, src, "unable to open '{s}': {s}", .{ operand, @errorName(err) });
6820 return sema.fail(block, operand_src, "unable to open '{s}': {s}", .{ operand, @errorName(err) });
68216821 },
68226822 };
68236823 try mod.semaFile(result.file);
......@@ -6832,17 +6832,17 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
68326832
68336833 const mod = sema.mod;
68346834 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
6835 const src = inst_data.src();
6836 const name = try sema.resolveConstString(block, src, inst_data.operand);
6835 const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node };
6836 const name = try sema.resolveConstString(block, operand_src, inst_data.operand);
68376837
68386838 const embed_file = mod.embedFile(block.getFileScope(), name) catch |err| switch (err) {
68396839 error.ImportOutsidePkgPath => {
6840 return sema.fail(block, src, "embed of file outside package path: '{s}'", .{name});
6840 return sema.fail(block, operand_src, "embed of file outside package path: '{s}'", .{name});
68416841 },
68426842 else => {
68436843 // TODO: these errors are file system errors; make sure an update() will
68446844 // retry this and not cache the file system error, which may be transient.
6845 return sema.fail(block, src, "unable to open '{s}': {s}", .{ name, @errorName(err) });
6845 return sema.fail(block, operand_src, "unable to open '{s}': {s}", .{ name, @errorName(err) });
68466846 },
68476847 };
68486848
test/compile_errors.zig+9-1
......@@ -11,7 +11,15 @@ pub fn addCases(ctx: *TestContext) !void {
1111 \\ return @embedFile("/root/foo").len;
1212 \\}
1313 , &[_][]const u8{
14 ":2:12: error: embed of file outside package path: '/root/foo'",
14 ":2:23: error: embed of file outside package path: '/root/foo'",
15 });
16
17 case.addError(
18 \\export fn a() usize {
19 \\ return @import("../../above.zig").len;
20 \\}
21 , &[_][]const u8{
22 ":2:20: error: import of file outside package path: '../../above.zig'",
1523 });
1624 }
1725