From b3c6d774d292e6e4ddf4cd54455c4ef3c6386d66 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Thu, 29 Sep 2022 17:45:27 +0300 Subject: [PATCH] stage2: improve error message for missing member in file root struct * the root struct decl name is fully qualified this prevents error messages containing 'main.main' * avoid declared here note when file struct is missing a member It always points at the start of the file which might contain another container misleading the user. --- src/Module.zig | 1 + src/Sema.zig | 5 +++++ test/cases/aarch64-macos/hello_world_with_updates.0.zig | 3 +-- test/cases/compile_errors/bogus_compile_var.zig | 3 +-- ...032_compile_diagnostic_string_for_top_level_decl_type.zig | 2 +- test/cases/x86_64-linux/hello_world_with_updates.0.zig | 3 +-- test/cases/x86_64-macos/hello_world_with_updates.0.zig | 3 +-- test/cases/x86_64-windows/hello_world_with_updates.0.zig | 3 +-- 8 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Module.zig b/src/Module.zig index e756cc3dfd29af9a42ab706e20035b0d12606c8e..6056c385e398c45e71cd64c62554e6602b1902fa 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -4435,6 +4435,7 @@ pub fn semaFile(mod: *Module, file: *File) SemaError!void { new_decl.alive = true; // This Decl corresponds to a File and is therefore always alive. new_decl.analysis = .in_progress; new_decl.generation = mod.generation; + new_decl.name_fully_qualified = true; if (file.status == .success_zir) { assert(file.zir_loaded); diff --git a/src/Sema.zig b/src/Sema.zig index 7ad7491ea1c51d8a038d0adf9f919b851badf581..aed09d6201e5cd19f1c96b8638b48de9d68a27bb 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -4353,6 +4353,11 @@ fn failWithBadMemberAccess( .Enum => "enum", else => unreachable, }; + if (sema.mod.declIsRoot(agg_ty.getOwnerDecl())) { + return sema.fail(block, field_src, "root struct of file '{}' has no member named '{s}'", .{ + agg_ty.fmt(sema.mod), field_name, + }); + } const msg = msg: { const msg = try sema.errMsg(block, field_src, "{s} '{}' has no member named '{s}'", .{ kw_name, agg_ty.fmt(sema.mod), field_name, diff --git a/test/cases/aarch64-macos/hello_world_with_updates.0.zig b/test/cases/aarch64-macos/hello_world_with_updates.0.zig index 3c7a4941800ce191e3e6e1b7079768c38e6f54f8..9f516ff1390f02aafc6203c8625793f8e58acdaf 100644 --- a/test/cases/aarch64-macos/hello_world_with_updates.0.zig +++ b/test/cases/aarch64-macos/hello_world_with_updates.0.zig @@ -2,5 +2,4 @@ // output_mode=Exe // target=aarch64-macos // -// :109:9: error: struct 'tmp.tmp' has no member named 'main' -// :7:1: note: struct declared here +// :109:9: error: root struct of file 'tmp' has no member named 'main' diff --git a/test/cases/compile_errors/bogus_compile_var.zig b/test/cases/compile_errors/bogus_compile_var.zig index b675fd941cf34031022cd6902569423032b68e53..be222e53934994ee4aee63211b33dc898c8d7183 100644 --- a/test/cases/compile_errors/bogus_compile_var.zig +++ b/test/cases/compile_errors/bogus_compile_var.zig @@ -5,5 +5,4 @@ export fn entry() usize { return @sizeOf(@TypeOf(x)); } // backend=stage2 // target=native // -// :1:29: error: struct 'builtin.builtin' has no member named 'bogus' -// :1:1: note: struct declared here +// :1:29: error: root struct of file 'builtin' has no member named 'bogus' diff --git a/test/cases/compile_errors/issue_2032_compile_diagnostic_string_for_top_level_decl_type.zig b/test/cases/compile_errors/issue_2032_compile_diagnostic_string_for_top_level_decl_type.zig index 9ae320650ae826178811fb614b7d61292e374474..d78891bb2b86763228a2802f649b23eb45011b56 100644 --- a/test/cases/compile_errors/issue_2032_compile_diagnostic_string_for_top_level_decl_type.zig +++ b/test/cases/compile_errors/issue_2032_compile_diagnostic_string_for_top_level_decl_type.zig @@ -7,5 +7,5 @@ export fn entry() void { // backend=stage2 // target=native // -// :2:27: error: expected type 'u32', found 'tmp.tmp' +// :2:27: error: expected type 'u32', found 'tmp' // :1:1: note: struct declared here diff --git a/test/cases/x86_64-linux/hello_world_with_updates.0.zig b/test/cases/x86_64-linux/hello_world_with_updates.0.zig index c9c94442d0047544c55f9193bb7810a90b5c6088..40abdd6c1fa4217b99f077793ecbb56bfa3f89a0 100644 --- a/test/cases/x86_64-linux/hello_world_with_updates.0.zig +++ b/test/cases/x86_64-linux/hello_world_with_updates.0.zig @@ -2,5 +2,4 @@ // output_mode=Exe // target=x86_64-linux // -// :109:9: error: struct 'tmp.tmp' has no member named 'main' -// :7:1: note: struct declared here +// :109:9: error: root struct of file 'tmp' has no member named 'main' diff --git a/test/cases/x86_64-macos/hello_world_with_updates.0.zig b/test/cases/x86_64-macos/hello_world_with_updates.0.zig index 5860c9c0f6d8a0e2f860dc7aca28e01431a829cd..e0680c81d7420441fabd29498bf9258a5c16f291 100644 --- a/test/cases/x86_64-macos/hello_world_with_updates.0.zig +++ b/test/cases/x86_64-macos/hello_world_with_updates.0.zig @@ -2,5 +2,4 @@ // output_mode=Exe // target=x86_64-macos // -// :109:9: error: struct 'tmp.tmp' has no member named 'main' -// :7:1: note: struct declared here +// :109:9: error: root struct of file 'tmp' has no member named 'main' diff --git a/test/cases/x86_64-windows/hello_world_with_updates.0.zig b/test/cases/x86_64-windows/hello_world_with_updates.0.zig index 142699b9dac9ae5e4cb363be6db67623afa3c7c1..04e1d4cfad0ac9045fb033b9f550e2d5fb1e64c8 100644 --- a/test/cases/x86_64-windows/hello_world_with_updates.0.zig +++ b/test/cases/x86_64-windows/hello_world_with_updates.0.zig @@ -2,5 +2,4 @@ // output_mode=Exe // target=x86_64-windows // -// :130:9: error: struct 'tmp.tmp' has no member named 'main' -// :7:1: note: struct declared here +// :130:9: error: root struct of file 'tmp' has no member named 'main' -- 2.54.0