authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-11-21 13:23:24+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-11-21 13:59:14+02:00
loga947f97331595df4ee340bbcfcef7241555c687b
tree2001af089d40b9a42e8ffbd564c85831232dcaf3
parentf64f3423e4a4d63838a54dfb01f5cd9ea8f68b19

Sema: fix bad error location on field init with field access

Closes #14753

3 files changed, 26 insertions(+), 4 deletions(-)

src/Module.zig+15-2
......@@ -1467,6 +1467,14 @@ pub const SrcLoc = struct {
14671467 const end = start + @as(u32, @intCast(tree.tokenSlice(tok_index).len));
14681468 return Span{ .start = start, .end = end, .main = start };
14691469 },
1470 .node_offset_field_name_init => |node_off| {
1471 const tree = try src_loc.file_scope.getTree(gpa);
1472 const node = src_loc.declRelativeToNodeIndex(node_off);
1473 const tok_index = tree.firstToken(node) - 2;
1474 const start = tree.tokens.items(.start)[tok_index];
1475 const end = start + @as(u32, @intCast(tree.tokenSlice(tok_index).len));
1476 return Span{ .start = start, .end = end, .main = start };
1477 },
14701478 .node_offset_deref_ptr => |node_off| {
14711479 const tree = try src_loc.file_scope.getTree(gpa);
14721480 const node = src_loc.declRelativeToNodeIndex(node_off);
......@@ -2132,10 +2140,14 @@ pub const LazySrcLoc = union(enum) {
21322140 /// The payload is offset from the containing Decl AST node.
21332141 /// The source location points to the field name of:
21342142 /// * a field access expression (`a.b`), or
2135 /// * the callee of a method call (`a.b()`), or
2136 /// * the operand ("b" node) of a field initialization expression (`.a = b`), or
2143 /// * the callee of a method call (`a.b()`)
21372144 /// The Decl is determined contextually.
21382145 node_offset_field_name: i32,
2146 /// The payload is offset from the containing Decl AST node.
2147 /// The source location points to the field name of the operand ("b" node)
2148 /// of a field initialization expression (`.a = b`)
2149 /// The Decl is determined contextually.
2150 node_offset_field_name_init: i32,
21392151 /// The source location points to the pointer of a pointer deref expression,
21402152 /// found by taking this AST node index offset from the containing
21412153 /// Decl AST node, which points to a pointer deref AST node. Next, navigate
......@@ -2374,6 +2386,7 @@ pub const LazySrcLoc = union(enum) {
23742386 .node_offset_slice_sentinel,
23752387 .node_offset_call_func,
23762388 .node_offset_field_name,
2389 .node_offset_field_name_init,
23772390 .node_offset_deref_ptr,
23782391 .node_offset_asm_source,
23792392 .node_offset_asm_ret_ty,
src/Sema.zig+2-2
......@@ -9931,7 +9931,7 @@ fn zirStructInitFieldPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compi
99319931 const mod = sema.mod;
99329932 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
99339933 const src = inst_data.src();
9934 const field_name_src: LazySrcLoc = .{ .node_offset_field_name = inst_data.src_node };
9934 const field_name_src: LazySrcLoc = .{ .node_offset_field_name_init = inst_data.src_node };
99359935 const extra = sema.code.extraData(Zir.Inst.Field, inst_data.payload_index).data;
99369936 const field_name = try mod.intern_pool.getOrPutString(sema.gpa, sema.code.nullTerminatedString(extra.field_name_start));
99379937 const object_ptr = try sema.resolveInst(extra.lhs);
......@@ -19921,7 +19921,7 @@ fn zirStructInitFieldType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp
1992119921 const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node;
1992219922 const extra = sema.code.extraData(Zir.Inst.FieldType, inst_data.payload_index).data;
1992319923 const ty_src = inst_data.src();
19924 const field_name_src: LazySrcLoc = .{ .node_offset_field_name = inst_data.src_node };
19924 const field_name_src: LazySrcLoc = .{ .node_offset_field_name_init = inst_data.src_node };
1992519925 const wrapped_aggregate_ty = sema.resolveType(block, ty_src, extra.container_type) catch |err| switch (err) {
1992619926 // Since this is a ZIR instruction that returns a type, encountering
1992719927 // generic poison should not result in a failed compilation, but the
test/cases/compile_errors/invalid_field_in_struct_value_expression.zig+9
......@@ -21,6 +21,13 @@ pub export fn entry() void {
2121 dump(.{ .field_1 = 123, .field_3 = 456 });
2222}
2323
24pub export fn entry1() void {
25 const x = Object{
26 .abc = 1,
27 };
28 _ = x;
29}
30
2431// error
2532// backend=stage2
2633// target=native
......@@ -29,3 +36,5 @@ pub export fn entry() void {
2936// :1:11: note: struct declared here
3037// :21:30: error: no field named 'field_3' in struct 'tmp.Object'
3138// :15:16: note: struct declared here
39// :26:10: error: no field named 'abc' in struct 'tmp.Object'
40// :15:16: note: struct declared here