Sema: improve array source location
This commit is contained in:
@@ -1320,7 +1320,10 @@ fn arrayInitExpr(
|
||||
const len_inst = try gz.addInt(array_init.ast.elements.len);
|
||||
const elem_type = try typeExpr(gz, scope, array_type.ast.elem_type);
|
||||
if (array_type.ast.sentinel == 0) {
|
||||
const array_type_inst = try gz.addBin(.array_type, len_inst, elem_type);
|
||||
const array_type_inst = try gz.addPlNode(.array_type, array_init.ast.type_expr, Zir.Inst.Bin{
|
||||
.lhs = len_inst,
|
||||
.rhs = elem_type,
|
||||
});
|
||||
break :inst .{
|
||||
.array = array_type_inst,
|
||||
.elem = elem_type,
|
||||
@@ -1553,7 +1556,10 @@ fn structInitExpr(
|
||||
if (is_inferred_array_len) {
|
||||
const elem_type = try typeExpr(gz, scope, array_type.ast.elem_type);
|
||||
const array_type_inst = if (array_type.ast.sentinel == 0) blk: {
|
||||
break :blk try gz.addBin(.array_type, .zero_usize, elem_type);
|
||||
break :blk try gz.addPlNode(.array_type, struct_init.ast.type_expr, Zir.Inst.Bin{
|
||||
.lhs = .zero_usize,
|
||||
.rhs = elem_type,
|
||||
});
|
||||
} else blk: {
|
||||
const sentinel = try comptimeExpr(gz, scope, .{ .ty = elem_type }, array_type.ast.sentinel);
|
||||
break :blk try gz.addPlNode(
|
||||
@@ -3203,7 +3209,10 @@ fn arrayType(gz: *GenZir, scope: *Scope, rl: ResultLoc, node: Ast.Node.Index) !Z
|
||||
const len = try expr(gz, scope, .{ .coerced_ty = .usize_type }, len_node);
|
||||
const elem_type = try typeExpr(gz, scope, node_datas[node].rhs);
|
||||
|
||||
const result = try gz.addBin(.array_type, len, elem_type);
|
||||
const result = try gz.addPlNode(.array_type, node, Zir.Inst.Bin{
|
||||
.lhs = len,
|
||||
.rhs = elem_type,
|
||||
});
|
||||
return rvalue(gz, rl, result, node);
|
||||
}
|
||||
|
||||
|
||||
18
src/Sema.zig
18
src/Sema.zig
@@ -2815,7 +2815,7 @@ fn zirAllocExtended(
|
||||
) CompileError!Air.Inst.Ref {
|
||||
const extra = sema.code.extraData(Zir.Inst.AllocExtended, extended.operand);
|
||||
const src = LazySrcLoc.nodeOffset(extra.data.src_node);
|
||||
const ty_src = src; // TODO better source location
|
||||
const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = extra.data.src_node };
|
||||
const align_src = src; // TODO better source location
|
||||
const small = @bitCast(Zir.Inst.AllocExtended.Small, extended.small);
|
||||
|
||||
@@ -6194,11 +6194,12 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
|
||||
const tracy = trace(@src());
|
||||
defer tracy.end();
|
||||
|
||||
const bin_inst = sema.code.instructions.items(.data)[inst].bin;
|
||||
const len_src = sema.src; // TODO better source location
|
||||
const elem_src = sema.src; // TODO better source location
|
||||
const len = try sema.resolveInt(block, len_src, bin_inst.lhs, Type.usize);
|
||||
const elem_type = try sema.resolveType(block, elem_src, bin_inst.rhs);
|
||||
const inst_data = sema.code.instructions.items(.data)[inst].pl_node;
|
||||
const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data;
|
||||
const len_src: LazySrcLoc = .{ .node_offset_array_type_len = inst_data.src_node };
|
||||
const elem_src: LazySrcLoc = .{ .node_offset_array_type_elem = inst_data.src_node };
|
||||
const len = try sema.resolveInt(block, len_src, extra.lhs, Type.usize);
|
||||
const elem_type = try sema.resolveType(block, elem_src, extra.rhs);
|
||||
const array_ty = try Type.array(sema.arena, len, null, elem_type, sema.mod);
|
||||
|
||||
return sema.addType(array_ty);
|
||||
@@ -10570,18 +10571,17 @@ fn analyzeArithmetic(
|
||||
if (lhs_zig_ty_tag == .Pointer) switch (lhs_ty.ptrSize()) {
|
||||
.One, .Slice => {},
|
||||
.Many, .C => {
|
||||
const op_src = src; // TODO better source location
|
||||
const air_tag: Air.Inst.Tag = switch (zir_tag) {
|
||||
.add => .ptr_add,
|
||||
.sub => .ptr_sub,
|
||||
else => return sema.fail(
|
||||
block,
|
||||
op_src,
|
||||
src,
|
||||
"invalid pointer arithmetic operand: '{s}''",
|
||||
.{@tagName(zir_tag)},
|
||||
),
|
||||
};
|
||||
return analyzePtrArithmetic(sema, block, op_src, lhs, rhs, air_tag, lhs_src, rhs_src);
|
||||
return analyzePtrArithmetic(sema, block, src, lhs, rhs, air_tag, lhs_src, rhs_src);
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -212,7 +212,7 @@ pub const Inst = struct {
|
||||
/// Uses the `pl_node` union field. Payload is `Bin`.
|
||||
array_mul,
|
||||
/// `[N]T` syntax. No source location provided.
|
||||
/// Uses the `bin` union field. lhs is length, rhs is element type.
|
||||
/// Uses the `pl_node` union field. Payload is `Bin`. lhs is length, rhs is element type.
|
||||
array_type,
|
||||
/// `[N:S]T` syntax. Source location is the array type expression node.
|
||||
/// Uses the `pl_node` union field. Payload is `ArrayTypeSentinel`.
|
||||
@@ -1571,7 +1571,7 @@ pub const Inst = struct {
|
||||
.param_anytype_comptime = .str_tok,
|
||||
.array_cat = .pl_node,
|
||||
.array_mul = .pl_node,
|
||||
.array_type = .bin,
|
||||
.array_type = .pl_node,
|
||||
.array_type_sentinel = .pl_node,
|
||||
.vector_type = .pl_node,
|
||||
.elem_type_index = .bin,
|
||||
|
||||
@@ -142,7 +142,6 @@ const Writer = struct {
|
||||
const tag = tags[inst];
|
||||
try stream.print("= {s}(", .{@tagName(tags[inst])});
|
||||
switch (tag) {
|
||||
.array_type,
|
||||
.as,
|
||||
.store,
|
||||
.store_to_block_ptr,
|
||||
@@ -354,6 +353,7 @@ const Writer = struct {
|
||||
.elem_ptr,
|
||||
.elem_val,
|
||||
.coerce_result_ptr,
|
||||
.array_type,
|
||||
=> try self.writePlNodeBin(stream, inst),
|
||||
|
||||
.elem_ptr_imm => try self.writeElemPtrImm(stream, inst),
|
||||
|
||||
@@ -9,4 +9,4 @@ pub export fn entry() void {
|
||||
// target=native
|
||||
// backend=stage2
|
||||
//
|
||||
// :4:1: error: expected type 'type', found 'fn() type'
|
||||
// :5:12: error: expected type 'type', found 'fn() type'
|
||||
|
||||
Reference in New Issue
Block a user