From 61ba225709b88579984a1a679021d121d1aada64 Mon Sep 17 00:00:00 2001 From: Krzysztof Wolicki Date: Fri, 26 Jan 2024 14:22:15 +0100 Subject: Sema: tuples have no names to be used for reporting errors in finishStructInit --- src/Sema.zig | 21 +++++++++++++++------ .../missing_field_in_struct_value_expression.zig | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 4244dd9aed..f4ab11969a 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -19963,13 +19963,22 @@ fn finishStructInit( const field_init = struct_type.fieldInit(ip, i); if (field_init == .none) { - const field_name = struct_type.field_names.get(ip)[i]; - const template = "missing struct field: {}"; - const args = .{field_name.fmt(ip)}; - if (root_msg) |msg| { - try sema.errNote(block, init_src, msg, template, args); + if (!struct_type.isTuple(ip)) { + const field_name = struct_type.field_names.get(ip)[i]; + const template = "missing struct field: {}"; + const args = .{field_name.fmt(ip)}; + if (root_msg) |msg| { + try sema.errNote(block, init_src, msg, template, args); + } else { + root_msg = try sema.errMsg(block, init_src, template, args); + } } else { - root_msg = try sema.errMsg(block, init_src, template, args); + const template = "missing tuple field with index {d}"; + if (root_msg) |msg| { + try sema.errNote(block, init_src, msg, template, .{i}); + } else { + root_msg = try sema.errMsg(block, init_src, template, .{i}); + } } } else { field_inits[i] = Air.internedToRef(field_init); diff --git a/test/cases/compile_errors/missing_field_in_struct_value_expression.zig b/test/cases/compile_errors/missing_field_in_struct_value_expression.zig index eec50ee1af..d0b09643b8 100644 --- a/test/cases/compile_errors/missing_field_in_struct_value_expression.zig +++ b/test/cases/compile_errors/missing_field_in_struct_value_expression.zig @@ -13,9 +13,23 @@ export fn f() void { _ = a; } +const B = struct { u32, u32 }; +export fn g() void { + const b = B{0}; + _ = b; +} +export fn h() void { + const c = B{}; + _ = c; +} // error // backend=stage2 // target=native // // :9:16: error: missing struct field: x // :1:11: note: struct 'tmp.A' declared here +// :18:16: error: missing tuple field with index 1 +// :16:11: note: struct declared here +// :22:16: error: missing tuple field with index 0 +// :22:16: note: missing tuple field with index 1 +// :16:11: note: struct 'tmp.B' declared here -- cgit v1.2.3