authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-21 15:12:03-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-11-21 15:12:03-05:00
log54d196bb300770394fd57db718ff1768edd552f8
treebea1eab99aeed800b92fc589ad505cb84a86f122
parentbe6f76655fb355426dc8f3824368376608bc0ba1
parentd63298da65df7fa2712bf9e9d65d36cd91af22fa
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #18057 from Vexu/fixes

Fix bad error location on field init with field access

5 files changed, 54 insertions(+), 4 deletions(-)

src/InternPool.zig+7
......@@ -8301,6 +8301,13 @@ pub fn funcZirBodyInst(ip: *const InternPool, i: Index) Zir.Inst.Index {
83018301 assert(ip.items.items(.tag)[func_decl_index] == .func_decl);
83028302 break :b ip.items.items(.data)[func_decl_index] + zir_body_inst_field_index;
83038303 },
8304 .func_coerced => {
8305 const datas = ip.items.items(.data);
8306 const uncoerced_func_index: Index = @enumFromInt(ip.extra.items[
8307 datas[@intFromEnum(i)] + std.meta.fieldIndex(Tag.FuncCoerced, "func").?
8308 ]);
8309 return ip.funcZirBodyInst(uncoerced_func_index);
8310 },
83048311 else => unreachable,
83058312 };
83068313 return @enumFromInt(ip.extra.items[extra_index]);
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/behavior/call.zig+21
......@@ -499,3 +499,24 @@ test "call inline fn through pointer" {
499499 const f = &S.foo;
500500 try f(123);
501501}
502
503test "call coerced function" {
504 const T = struct {
505 x: f64,
506 const T = @This();
507 usingnamespace Implement(1);
508 const F = fn (comptime f64) type;
509 const Implement: F = opaque {
510 fn implementer(comptime val: anytype) type {
511 return opaque {
512 fn incr(self: T) T {
513 return .{ .x = self.x + val };
514 }
515 };
516 }
517 }.implementer;
518 };
519
520 const a = T{ .x = 3 };
521 try std.testing.expect(a.incr().x == 4);
522}
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