Skip to content

Commit

Permalink
Sema: improve array source location
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Jul 7, 2022
1 parent 27ee414 commit b5ac2b4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
15 changes: 12 additions & 3 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}

Expand Down
18 changes: 9 additions & 9 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
},
};

Expand Down
4 changes: 2 additions & 2 deletions src/Zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/print_zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion test/cases/compile_errors/invalid_array_elem_ty.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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'

0 comments on commit b5ac2b4

Please sign in to comment.