Skip to content

Commit 25beac6

Browse files
committed
Sema: improve array source location
1 parent ce3bff8 commit 25beac6

File tree

5 files changed

+25
-16
lines changed

5 files changed

+25
-16
lines changed

src/AstGen.zig

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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(
@@ -3205,7 +3211,10 @@ fn arrayType(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) !Z
32053211
const len = try expr(gz, scope, .{ .coerced_ty = .usize_type }, len_node);
32063212
const elem_type = try typeExpr(gz, scope, node_datas[node].rhs);
32073213

3208-
const result = try gz.addBin(.array_type, len, elem_type);
3214+
const result = try gz.addPlNode(.array_type, node, Zir.Inst.Bin{
3215+
.lhs = len,
3216+
.rhs = elem_type,
3217+
});
32093218
return rvalue(gz, rl, result, node);
32103219
}
32113220

src/Sema.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,7 +2814,7 @@ fn zirAllocExtended(
28142814
) CompileError!Air.Inst.Ref {
28152815
const extra = sema.code.extraData(Zir.Inst.AllocExtended, extended.operand);
28162816
const src = LazySrcLoc.nodeOffset(extra.data.src_node);
2817-
const ty_src = src; // TODO better source location
2817+
const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = extra.data.src_node };
28182818
const align_src = src; // TODO better source location
28192819
const small = @bitCast(Zir.Inst.AllocExtended.Small, extended.small);
28202820

@@ -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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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`.
@@ -1580,7 +1580,7 @@ pub const Inst = struct {
15801580
.param_anytype_comptime = .str_tok,
15811581
.array_cat = .pl_node,
15821582
.array_mul = .pl_node,
1583-
.array_type = .bin,
1583+
.array_type = .pl_node,
15841584
.array_type_sentinel = .pl_node,
15851585
.vector_type = .pl_node,
15861586
.elem_type_index = .bin,

src/print_zir.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -355,6 +354,7 @@ const Writer = struct {
355354
.elem_ptr,
356355
.elem_val,
357356
.coerce_result_ptr,
357+
.array_type,
358358
=> try self.writePlNodeBin(stream, inst),
359359

360360
.elem_ptr_imm => try self.writeElemPtrImm(stream, inst),

test/cases/compile_errors/invalid_array_elem_ty.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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'

0 commit comments

Comments
 (0)