authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 20:58:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-10-08 20:58:04-07:00
log5eb5d523b50e8e8912cf84bf021dc4c95e521a7a
tree20190d08927471c5e9e909875927224e6f0466ad
parentcd4397783f7ae86de867bed3346ea10547a94ee1

give modules friendly names for error reporting


8 files changed, 40 insertions(+), 23 deletions(-)

src/Compilation.zig+10-8
...@@ -1290,6 +1290,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1290,6 +1290,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1290 const builtin_mod = try Package.Module.create(arena, .{1290 const builtin_mod = try Package.Module.create(arena, .{
1291 .root = .{ .root_dir = zig_cache_artifact_directory },1291 .root = .{ .root_dir = zig_cache_artifact_directory },
1292 .root_src_path = "builtin.zig",1292 .root_src_path = "builtin.zig",
1293 .fully_qualified_name = "builtin",
1293 });1294 });
12941295
1295 // When you're testing std, the main module is std. In that case,1296 // When you're testing std, the main module is std. In that case,
...@@ -1319,6 +1320,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1319,6 +1320,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1319 .sub_path = "std",1320 .sub_path = "std",
1320 },1321 },
1321 .root_src_path = "std.zig",1322 .root_src_path = "std.zig",
1323 .fully_qualified_name = "std",
1322 });1324 });
13231325
1324 const root_mod = if (options.is_test) root_mod: {1326 const root_mod = if (options.is_test) root_mod: {
...@@ -1329,6 +1331,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1329,6 +1331,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1329 .sub_path = std.fs.path.dirname(test_runner) orelse "",1331 .sub_path = std.fs.path.dirname(test_runner) orelse "",
1330 },1332 },
1331 .root_src_path = std.fs.path.basename(test_runner),1333 .root_src_path = std.fs.path.basename(test_runner),
1334 .fully_qualified_name = "root",
1332 });1335 });
13331336
1334 pkg.deps = try main_mod.deps.clone(arena);1337 pkg.deps = try main_mod.deps.clone(arena);
...@@ -1338,6 +1341,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1338,6 +1341,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1338 .root_dir = options.zig_lib_directory,1341 .root_dir = options.zig_lib_directory,
1339 },1342 },
1340 .root_src_path = "test_runner.zig",1343 .root_src_path = "test_runner.zig",
1344 .fully_qualified_name = "root",
1341 });1345 });
13421346
1343 break :root_mod test_mod;1347 break :root_mod test_mod;
...@@ -1349,6 +1353,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {...@@ -1349,6 +1353,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
1349 .root_dir = options.zig_lib_directory,1353 .root_dir = options.zig_lib_directory,
1350 },1354 },
1351 .root_src_path = "compiler_rt.zig",1355 .root_src_path = "compiler_rt.zig",
1356 .fully_qualified_name = "compiler_rt",
1352 });1357 });
1353 } else null;1358 } else null;
13541359
...@@ -2616,23 +2621,19 @@ fn reportMultiModuleErrors(mod: *Module) !void {...@@ -2616,23 +2621,19 @@ fn reportMultiModuleErrors(mod: *Module) !void {
2616 errdefer for (notes[0..i]) |*n| n.deinit(mod.gpa);2621 errdefer for (notes[0..i]) |*n| n.deinit(mod.gpa);
2617 note.* = switch (ref) {2622 note.* = switch (ref) {
2618 .import => |loc| blk: {2623 .import => |loc| blk: {
2619 //const name = try loc.file_scope.mod.getName(mod.gpa, mod.*);
2620 //defer mod.gpa.free(name);
2621 break :blk try Module.ErrorMsg.init(2624 break :blk try Module.ErrorMsg.init(
2622 mod.gpa,2625 mod.gpa,
2623 loc,2626 loc,
2624 "imported from module {}",2627 "imported from module {s}",
2625 .{loc.file_scope.mod.root},2628 .{loc.file_scope.mod.fully_qualified_name},
2626 );2629 );
2627 },2630 },
2628 .root => |pkg| blk: {2631 .root => |pkg| blk: {
2629 //const name = try pkg.getName(mod.gpa, mod.*);
2630 //defer mod.gpa.free(name);
2631 break :blk try Module.ErrorMsg.init(2632 break :blk try Module.ErrorMsg.init(
2632 mod.gpa,2633 mod.gpa,
2633 .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },2634 .{ .file_scope = file, .parent_decl_node = 0, .lazy = .entire_file },
2634 "root of module {}",2635 "root of module {s}",
2635 .{pkg.root},2636 .{pkg.fully_qualified_name},
2636 );2637 );
2637 },2638 },
2638 };2639 };
...@@ -6362,6 +6363,7 @@ fn buildOutputFromZig(...@@ -6362,6 +6363,7 @@ fn buildOutputFromZig(
6362 var main_mod: Package.Module = .{6363 var main_mod: Package.Module = .{
6363 .root = .{ .root_dir = comp.zig_lib_directory },6364 .root = .{ .root_dir = comp.zig_lib_directory },
6364 .root_src_path = src_basename,6365 .root_src_path = src_basename,
6366 .fully_qualified_name = "root",
6365 };6367 };
6366 const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];6368 const root_name = src_basename[0 .. src_basename.len - std.fs.path.extension(src_basename).len];
6367 const target = comp.getTarget();6369 const target = comp.getTarget();
src/Package/Module.zig+2
...@@ -6,6 +6,8 @@...@@ -6,6 +6,8 @@
6root: Package.Path,6root: Package.Path,
7/// Relative to `root`. May contain path separators.7/// Relative to `root`. May contain path separators.
8root_src_path: []const u8,8root_src_path: []const u8,
9/// Name used in compile errors. Looks like "root.foo.bar".
10fully_qualified_name: []const u8,
9/// The dependency table of this module. Shared dependencies such as 'std',11/// The dependency table of this module. Shared dependencies such as 'std',
10/// 'builtin', and 'root' are not specified in every dependency table, but12/// 'builtin', and 'root' are not specified in every dependency table, but
11/// instead only in the table of `main_mod`. `Module.importFile` is13/// instead only in the table of `main_mod`. `Module.importFile` is
src/Sema.zig+3-4
...@@ -5798,6 +5798,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr...@@ -5798,6 +5798,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
5798 .sub_path = std.fs.path.dirname(c_import_res.out_zig_path) orelse "",5798 .sub_path = std.fs.path.dirname(c_import_res.out_zig_path) orelse "",
5799 },5799 },
5800 .root_src_path = std.fs.path.basename(c_import_res.out_zig_path),5800 .root_src_path = std.fs.path.basename(c_import_res.out_zig_path),
5801 .fully_qualified_name = c_import_res.out_zig_path,
5801 });5802 });
58025803
5803 const result = mod.importPkg(c_import_mod) catch |err|5804 const result = mod.importPkg(c_import_mod) catch |err|
...@@ -13076,10 +13077,8 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air....@@ -13076,10 +13077,8 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.
13076 return sema.fail(block, operand_src, "import of file outside module path: '{s}'", .{operand});13077 return sema.fail(block, operand_src, "import of file outside module path: '{s}'", .{operand});
13077 },13078 },
13078 error.ModuleNotFound => {13079 error.ModuleNotFound => {
13079 //const name = try block.getFileScope(mod).mod.getName(sema.gpa, mod.*);13080 return sema.fail(block, operand_src, "no module named '{s}' available within module {s}", .{
13080 //defer sema.gpa.free(name);13081 operand, block.getFileScope(mod).mod.fully_qualified_name,
13081 return sema.fail(block, operand_src, "no module named '{s}' available within module '{}'", .{
13082 operand, block.getFileScope(mod).mod.root,
13083 });13082 });
13084 },13083 },
13085 else => {13084 else => {
src/main.zig+21-7
...@@ -1022,13 +1022,10 @@ fn buildOutputType(...@@ -1022,13 +1022,10 @@ fn buildOutputType(
1022 }1022 }
1023 }1023 }
10241024
1025 var mod_it = modules.iterator();1025 if (modules.get(mod_name)) |value| {
1026 while (mod_it.next()) |kv| {1026 fatal("unable to add module '{s}' -> '{s}': already exists as '{s}'", .{
1027 if (std.mem.eql(u8, mod_name, kv.key_ptr.*)) {1027 mod_name, root_src, value.mod.root_src_path,
1028 fatal("unable to add module '{s}' -> '{s}': already exists as '{s}'", .{1028 });
1029 mod_name, root_src, kv.value_ptr.mod.root_src_path,
1030 });
1031 }
1032 }1029 }
10331030
1034 try modules.put(mod_name, .{1031 try modules.put(mod_name, .{
...@@ -1038,6 +1035,7 @@ fn buildOutputType(...@@ -1038,6 +1035,7 @@ fn buildOutputType(
1038 .sub_path = fs.path.dirname(root_src) orelse "",1035 .sub_path = fs.path.dirname(root_src) orelse "",
1039 },1036 },
1040 .root_src_path = fs.path.basename(root_src),1037 .root_src_path = fs.path.basename(root_src),
1038 .fully_qualified_name = mod_name,
1041 }),1039 }),
1042 .deps_str = deps_str,1040 .deps_str = deps_str,
1043 });1041 });
...@@ -3247,6 +3245,7 @@ fn buildOutputType(...@@ -3247,6 +3245,7 @@ fn buildOutputType(
3247 src_path3245 src_path
3248 else3246 else
3249 try fs.path.relative(arena, p, src_path),3247 try fs.path.relative(arena, p, src_path),
3248 .fully_qualified_name = "root",
3250 });3249 });
3251 } else {3250 } else {
3252 break :blk try Package.Module.create(arena, .{3251 break :blk try Package.Module.create(arena, .{
...@@ -3255,6 +3254,7 @@ fn buildOutputType(...@@ -3255,6 +3254,7 @@ fn buildOutputType(
3255 .sub_path = fs.path.dirname(src_path) orelse "",3254 .sub_path = fs.path.dirname(src_path) orelse "",
3256 },3255 },
3257 .root_src_path = fs.path.basename(src_path),3256 .root_src_path = fs.path.basename(src_path),
3257 .fully_qualified_name = "root",
3258 });3258 });
3259 }3259 }
3260 } else null;3260 } else null;
...@@ -4820,16 +4820,19 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4820,16 +4820,19 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
4820 .sub_path = fs.path.dirname(build_runner_path) orelse "",4820 .sub_path = fs.path.dirname(build_runner_path) orelse "",
4821 },4821 },
4822 .root_src_path = fs.path.basename(build_runner_path),4822 .root_src_path = fs.path.basename(build_runner_path),
4823 .fully_qualified_name = "root",
4823 }4824 }
4824 else4825 else
4825 .{4826 .{
4826 .root = .{ .root_dir = zig_lib_directory },4827 .root = .{ .root_dir = zig_lib_directory },
4827 .root_src_path = "build_runner.zig",4828 .root_src_path = "build_runner.zig",
4829 .fully_qualified_name = "root",
4828 };4830 };
48294831
4830 var build_mod: Package.Module = .{4832 var build_mod: Package.Module = .{
4831 .root = .{ .root_dir = build_root },4833 .root = .{ .root_dir = build_root },
4832 .root_src_path = build_zig_basename,4834 .root_src_path = build_zig_basename,
4835 .fully_qualified_name = "root.@build",
4833 };4836 };
4834 if (build_options.only_core_functionality) {4837 if (build_options.only_core_functionality) {
4835 try createEmptyDependenciesModule(arena, &main_mod, local_cache_directory);4838 try createEmptyDependenciesModule(arena, &main_mod, local_cache_directory);
...@@ -4921,6 +4924,11 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi...@@ -4921,6 +4924,11 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
4921 const m = try Package.Module.create(arena, .{4924 const m = try Package.Module.create(arena, .{
4922 .root = try f.package_root.clone(arena),4925 .root = try f.package_root.clone(arena),
4923 .root_src_path = Package.build_zig_basename,4926 .root_src_path = Package.build_zig_basename,
4927 .fully_qualified_name = try std.fmt.allocPrint(
4928 arena,
4929 "root.@dependencies.{s}",
4930 .{&hash},
4931 ),
4924 });4932 });
4925 const hash_cloned = try arena.dupe(u8, &hash);4933 const hash_cloned = try arena.dupe(u8, &hash);
4926 deps_mod.deps.putAssumeCapacityNoClobber(hash_cloned, m);4934 deps_mod.deps.putAssumeCapacityNoClobber(hash_cloned, m);
...@@ -5181,6 +5189,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void...@@ -5181,6 +5189,7 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
5181 file.mod = try Package.Module.create(arena, .{5189 file.mod = try Package.Module.create(arena, .{
5182 .root = Package.Path.cwd(),5190 .root = Package.Path.cwd(),
5183 .root_src_path = file.sub_file_path,5191 .root_src_path = file.sub_file_path,
5192 .fully_qualified_name = "root",
5184 });5193 });
51855194
5186 file.zir = try AstGen.generate(gpa, file.tree);5195 file.zir = try AstGen.generate(gpa, file.tree);
...@@ -5389,6 +5398,7 @@ fn fmtPathFile(...@@ -5389,6 +5398,7 @@ fn fmtPathFile(
5389 file.mod = try Package.Module.create(fmt.arena, .{5398 file.mod = try Package.Module.create(fmt.arena, .{
5390 .root = Package.Path.cwd(),5399 .root = Package.Path.cwd(),
5391 .root_src_path = file.sub_file_path,5400 .root_src_path = file.sub_file_path,
5401 .fully_qualified_name = "root",
5392 });5402 });
53935403
5394 if (stat.size > max_src_size)5404 if (stat.size > max_src_size)
...@@ -5472,6 +5482,7 @@ pub fn putAstErrorsIntoBundle(...@@ -5472,6 +5482,7 @@ pub fn putAstErrorsIntoBundle(
5472 file.mod = try Package.Module.create(gpa, .{5482 file.mod = try Package.Module.create(gpa, .{
5473 .root = Package.Path.cwd(),5483 .root = Package.Path.cwd(),
5474 .root_src_path = file.sub_file_path,5484 .root_src_path = file.sub_file_path,
5485 .fully_qualified_name = "root",
5475 });5486 });
5476 defer gpa.destroy(file.mod);5487 defer gpa.destroy(file.mod);
54775488
...@@ -6040,6 +6051,7 @@ pub fn cmdAstCheck(...@@ -6040,6 +6051,7 @@ pub fn cmdAstCheck(
6040 file.mod = try Package.Module.create(arena, .{6051 file.mod = try Package.Module.create(arena, .{
6041 .root = Package.Path.cwd(),6052 .root = Package.Path.cwd(),
6042 .root_src_path = file.sub_file_path,6053 .root_src_path = file.sub_file_path,
6054 .fully_qualified_name = "root",
6043 });6055 });
60446056
6045 file.tree = try Ast.parse(gpa, file.source, .zig);6057 file.tree = try Ast.parse(gpa, file.source, .zig);
...@@ -6211,6 +6223,7 @@ pub fn cmdChangelist(...@@ -6211,6 +6223,7 @@ pub fn cmdChangelist(
6211 file.mod = try Package.Module.create(arena, .{6223 file.mod = try Package.Module.create(arena, .{
6212 .root = Package.Path.cwd(),6224 .root = Package.Path.cwd(),
6213 .root_src_path = file.sub_file_path,6225 .root_src_path = file.sub_file_path,
6226 .fully_qualified_name = "root",
6214 });6227 });
62156228
6216 const source = try arena.allocSentinel(u8, @as(usize, @intCast(stat.size)), 0);6229 const source = try arena.allocSentinel(u8, @as(usize, @intCast(stat.size)), 0);
...@@ -6846,6 +6859,7 @@ fn createDependenciesModule(...@@ -6846,6 +6859,7 @@ fn createDependenciesModule(
6846 .sub_path = o_dir_sub_path,6859 .sub_path = o_dir_sub_path,
6847 },6860 },
6848 .root_src_path = basename,6861 .root_src_path = basename,
6862 .fully_qualified_name = "root.@dependencies",
6849 });6863 });
6850 try main_mod.deps.put(arena, "@dependencies", deps_mod);6864 try main_mod.deps.put(arena, "@dependencies", deps_mod);
6851 return deps_mod;6865 return deps_mod;
test/cases/compile_errors/import_of_missing_package.zig+1-1
...@@ -7,4 +7,4 @@ comptime {...@@ -7,4 +7,4 @@ comptime {
7// backend=stage27// backend=stage2
8// target=native8// target=native
9//9//
10// :1:21: error: no package named 'foo' available within package 'root'10// :1:21: error: no module named 'foo' available within module root
test/cases/compile_errors/import_outside_package.zig+1-1
...@@ -5,4 +5,4 @@ export fn a() usize {...@@ -5,4 +5,4 @@ export fn a() usize {
5// error5// error
6// target=native6// target=native
7//7//
8// :2:20: error: import of file outside package path: '../../above.zig'8// :2:20: error: import of file outside module path: '../../above.zig'
test/cases/compile_errors/import_outside_package_path.zig+1-1
...@@ -6,4 +6,4 @@ comptime {...@@ -6,4 +6,4 @@ comptime {
6// backend=stage26// backend=stage2
7// target=native7// target=native
8//8//
9// :2:17: error: import of file outside package path: '../a.zig'9// :2:17: error: import of file outside module path: '../a.zig'
test/compile_errors.zig+1-1
...@@ -129,7 +129,7 @@ pub fn addCases(ctx: *Cases) !void {...@@ -129,7 +129,7 @@ pub fn addCases(ctx: *Cases) !void {
129 \\}129 \\}
130 , &[_][]const u8{130 , &[_][]const u8{
131 ":1:1: error: file exists in multiple modules",131 ":1:1: error: file exists in multiple modules",
132 ":1:1: note: root of module root.foo",132 ":1:1: note: root of module foo",
133 ":3:17: note: imported from module root",133 ":3:17: note: imported from module root",
134 });134 });
135 case.addSourceFile("foo.zig",135 case.addSourceFile("foo.zig",