authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-06 12:29:16+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-07 10:50:06+03:00
logb5ac2b4330b84799060609ce0f22eda266ad171b
tree8a6e8401eefd602691e2b6d3686757a051513fb3
parent27ee4141592c7a9d77a2d73f5fa6a3c6262ac7fc

Sema: improve array source location


5 files changed, 25 insertions(+), 16 deletions(-)

src/AstGen.zig+12-3
......@@ -1320,7 +1320,10 @@ fn arrayInitExpr(
13201320 const len_inst = try gz.addInt(array_init.ast.elements.len);
13211321 const elem_type = try typeExpr(gz, scope, array_type.ast.elem_type);
13221322 if (array_type.ast.sentinel == 0) {
1323 const array_type_inst = try gz.addBin(.array_type, len_inst, elem_type);
1323 const array_type_inst = try gz.addPlNode(.array_type, array_init.ast.type_expr, Zir.Inst.Bin{
1324 .lhs = len_inst,
1325 .rhs = elem_type,
1326 });
13241327 break :inst .{
13251328 .array = array_type_inst,
13261329 .elem = elem_type,
......@@ -1553,7 +1556,10 @@ fn structInitExpr(
15531556 if (is_inferred_array_len) {
15541557 const elem_type = try typeExpr(gz, scope, array_type.ast.elem_type);
15551558 const array_type_inst = if (array_type.ast.sentinel == 0) blk: {
1556 break :blk try gz.addBin(.array_type, .zero_usize, elem_type);
1559 break :blk try gz.addPlNode(.array_type, struct_init.ast.type_expr, Zir.Inst.Bin{
1560 .lhs = .zero_usize,
1561 .rhs = elem_type,
1562 });
15571563 } else blk: {
15581564 const sentinel = try comptimeExpr(gz, scope, .{ .ty = elem_type }, array_type.ast.sentinel);
15591565 break :blk try gz.addPlNode(
......@@ -3203,7 +3209,10 @@ fn arrayType(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) !Z
32033209 const len = try expr(gz, scope, .{ .coerced_ty = .usize_type }, len_node);
32043210 const elem_type = try typeExpr(gz, scope, node_datas[node].rhs);
32053211
3206 const result = try gz.addBin(.array_type, len, elem_type);
3212 const result = try gz.addPlNode(.array_type, node, Zir.Inst.Bin{
3213 .lhs = len,
3214 .rhs = elem_type,
3215 });
32073216 return rvalue(gz, rl, result, node);
32083217}
32093218
src/Sema.zig+9-9
......@@ -2815,7 +2815,7 @@ fn zirAllocExtended(
28152815) CompileError!Air.Inst.Ref {
28162816 const extra = sema.code.extraData(Zir.Inst.AllocExtended, extended.operand);
28172817 const src = LazySrcLoc.nodeOffset(extra.data.src_node);
2818 const ty_src = src; // TODO better source location
2818 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = extra.data.src_node };
28192819 const align_src = src; // TODO better source location
28202820 const small = @bitCast(Zir.Inst.AllocExtended.Small, extended.small);
28212821
......@@ -6194,11 +6194,12 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
61946194 const tracy = trace(@src());
61956195 defer tracy.end();
61966196
6197 const bin_inst = sema.code.instructions.items(.data)[inst].bin;
6198 const len_src = sema.src; // TODO better source location
6199 const elem_src = sema.src; // TODO better source location
6200 const len = try sema.resolveInt(block, len_src, bin_inst.lhs, Type.usize);
6201 const elem_type = try sema.resolveType(block, elem_src, bin_inst.rhs);
6197 const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
6198 const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
6199 const len_src: LazySrcLoc = .{ .node_offset_array_type_len = inst_data.src_node };
6200 const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node };
6201 const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize);
6202 const elem_type = try sema.resolveType(block, elem_src, extra.rhs);
62026203 const array_ty = try Type.array(sema.arena, len, null, elem_type, sema.mod);
62036204
62046205 return sema.addType(array_ty);
......@@ -10570,18 +10571,17 @@ fn analyzeArithmetic(
1057010571 if (lhs_zig_ty_tag == .Pointer) switch (lhs_ty.ptrSize()) {
1057110572 .One, .Slice => {},
1057210573 .Many, .C => {
10573 const op_src = src; // TODO better source location
1057410574 const air_tag: Air.Inst.Tag = switch (zir_tag) {
1057510575 .add => .ptr_add,
1057610576 .sub => .ptr_sub,
1057710577 else => return sema.fail(
1057810578 block,
10579 op_src,
10579 src,
1058010580 "invalid pointer arithmetic operand: '{s}''",
1058110581 .{@tagName(zir_tag)},
1058210582 ),
1058310583 };
10584 return analyzePtrArithmetic(sema, block, op_src, lhs, rhs, air_tag, lhs_src, rhs_src);
10584 return analyzePtrArithmetic(sema, block, src, lhs, rhs, air_tag, lhs_src, rhs_src);
1058510585 },
1058610586 };
1058710587
src/Zir.zig+2-2
......@@ -212,7 +212,7 @@ pub const Inst = struct {
212212 /// Uses the `pl_node` union field. Payload is `Bin`.
213213 array_mul,
214214 /// `[N]T` syntax. No source location provided.
215 /// Uses the `bin` union field. lhs is length, rhs is element type.
215 /// Uses the `pl_node` union field. Payload is `Bin`. lhs is length, rhs is element type.
216216 array_type,
217217 /// `[N:S]T` syntax. Source location is the array type expression node.
218218 /// Uses the `pl_node` union field. Payload is `ArrayTypeSentinel`.
......@@ -1571,7 +1571,7 @@ pub const Inst = struct {
15711571 .param_anytype_comptime = .str_tok,
15721572 .array_cat = .pl_node,
15731573 .array_mul = .pl_node,
1574 .array_type = .bin,
1574 .array_type = .pl_node,
15751575 .array_type_sentinel = .pl_node,
15761576 .vector_type = .pl_node,
15771577 .elem_type_index = .bin,
src/print_zir.zig+1-1
......@@ -142,7 +142,6 @@ const Writer = struct {
142142 const tag = tags[inst];
143143 try stream.print("= {s}(", .{@tagName(tags[inst])});
144144 switch (tag) {
145 .array_type,
146145 .as,
147146 .store,
148147 .store_to_block_ptr,
......@@ -354,6 +353,7 @@ const Writer = struct {
354353 .elem_ptr,
355354 .elem_val,
356355 .coerce_result_ptr,
356 .array_type,
357357 => try self.writePlNodeBin(stream, inst),
358358
359359 .elem_ptr_imm => try self.writeElemPtrImm(stream, inst),
test/cases/compile_errors/invalid_array_elem_ty.zig+1-1
......@@ -9,4 +9,4 @@ pub export fn entry() void {
99// target=native
1010// backend=stage2
1111//
12// :4:1: error: expected type 'type', found 'fn() type'
12// :5:12: error: expected type 'type', found 'fn() type'