authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-15 15:45:20+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2024-09-16 16:42:42+01:00
log1365be5d02429dbbbfab43133d399b2af42047c5
tree2cb61dabb88b84094701df907ad67479951f53a8
parent5d7fa5513f92a43a418e3c5c4d27f0b61db313ff
signaturelock-open Commit is signed but in an unrecognized format.

compiler: provide correct result types to `+=` and `-=`

Resolves: #21341

5 files changed, 80 insertions(+), 2 deletions(-)

lib/std/zig/AstGen.zig+20-2
...@@ -3785,8 +3785,26 @@ fn assignOp(...@@ -3785,8 +3785,26 @@ fn assignOp(
3785 else => undefined,3785 else => undefined,
3786 };3786 };
3787 const lhs = try gz.addUnNode(.load, lhs_ptr, infix_node);3787 const lhs = try gz.addUnNode(.load, lhs_ptr, infix_node);
3788 const lhs_type = try gz.addUnNode(.typeof, lhs, infix_node);3788
3789 const rhs = try expr(gz, scope, .{ .rl = .{ .coerced_ty = lhs_type } }, node_datas[infix_node].rhs);3789 const rhs_res_ty = switch (op_inst_tag) {
3790 .add,
3791 .sub,
3792 => try gz.add(.{
3793 .tag = .extended,
3794 .data = .{ .extended = .{
3795 .opcode = .inplace_arith_result_ty,
3796 .small = @intFromEnum(@as(Zir.Inst.InplaceOp, switch (op_inst_tag) {
3797 .add => .add_eq,
3798 .sub => .sub_eq,
3799 else => unreachable,
3800 })),
3801 .operand = @intFromEnum(lhs),
3802 } },
3803 }),
3804 else => try gz.addUnNode(.typeof, lhs, infix_node), // same as LHS type
3805 };
3806 // Not `coerced_ty` since `add`/etc won't coerce to this type.
3807 const rhs = try expr(gz, scope, .{ .rl = .{ .ty = rhs_res_ty } }, node_datas[infix_node].rhs);
37903808
3791 switch (op_inst_tag) {3809 switch (op_inst_tag) {
3792 .add, .sub, .mul, .div, .mod_rem => {3810 .add, .sub, .mul, .div, .mod_rem => {
lib/std/zig/Zir.zig+10
...@@ -2086,6 +2086,10 @@ pub const Inst = struct {...@@ -2086,6 +2086,10 @@ pub const Inst = struct {
2086 /// `operand` is payload index to `UnNode`.2086 /// `operand` is payload index to `UnNode`.
2087 /// `small` is unused.2087 /// `small` is unused.
2088 branch_hint,2088 branch_hint,
2089 /// Compute the result type for in-place arithmetic, e.g. `+=`.
2090 /// `operand` is `Zir.Inst.Ref` of the loaded LHS (*not* its type).
2091 /// `small` is an `Inst.InplaceOp`.
2092 inplace_arith_result_ty,
20892093
2090 pub const InstData = struct {2094 pub const InstData = struct {
2091 opcode: Extended,2095 opcode: Extended,
...@@ -3188,6 +3192,11 @@ pub const Inst = struct {...@@ -3188,6 +3192,11 @@ pub const Inst = struct {
3188 calling_convention_inline,3192 calling_convention_inline,
3189 };3193 };
31903194
3195 pub const InplaceOp = enum(u16) {
3196 add_eq,
3197 sub_eq,
3198 };
3199
3191 /// Trailing:3200 /// Trailing:
3192 /// 0. tag_type: Ref, // if has_tag_type3201 /// 0. tag_type: Ref, // if has_tag_type
3193 /// 1. captures_len: u32, // if has_captures_len3202 /// 1. captures_len: u32, // if has_captures_len
...@@ -4032,6 +4041,7 @@ fn findDeclsInner(...@@ -4032,6 +4041,7 @@ fn findDeclsInner(
4032 .field_parent_ptr,4041 .field_parent_ptr,
4033 .builtin_value,4042 .builtin_value,
4034 .branch_hint,4043 .branch_hint,
4044 .inplace_arith_result_ty,
4035 => return,4045 => return,
40364046
4037 // `@TypeOf` has a body.4047 // `@TypeOf` has a body.
src/Sema.zig+28
...@@ -1361,6 +1361,7 @@ fn analyzeBodyInner(...@@ -1361,6 +1361,7 @@ fn analyzeBodyInner(
1361 .value_placeholder => unreachable, // never appears in a body1361 .value_placeholder => unreachable, // never appears in a body
1362 .field_parent_ptr => try sema.zirFieldParentPtr(block, extended),1362 .field_parent_ptr => try sema.zirFieldParentPtr(block, extended),
1363 .builtin_value => try sema.zirBuiltinValue(extended),1363 .builtin_value => try sema.zirBuiltinValue(extended),
1364 .inplace_arith_result_ty => try sema.zirInplaceArithResultTy(extended),
1364 };1365 };
1365 },1366 },
13661367
...@@ -27342,6 +27343,33 @@ fn zirBuiltinValue(sema: *Sema, extended: Zir.Inst.Extended.InstData) CompileErr...@@ -27342,6 +27343,33 @@ fn zirBuiltinValue(sema: *Sema, extended: Zir.Inst.Extended.InstData) CompileErr
27342 return Air.internedToRef(ty.toIntern());27343 return Air.internedToRef(ty.toIntern());
27343}27344}
2734427345
27346fn zirInplaceArithResultTy(sema: *Sema, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
27347 const pt = sema.pt;
27348 const zcu = pt.zcu;
27349
27350 const lhs = try sema.resolveInst(@enumFromInt(extended.operand));
27351 const lhs_ty = sema.typeOf(lhs);
27352
27353 const op: Zir.Inst.InplaceOp = @enumFromInt(extended.small);
27354 const ty: Type = switch (op) {
27355 .add_eq => ty: {
27356 const ptr_size = lhs_ty.ptrSizeOrNull(zcu) orelse break :ty lhs_ty;
27357 switch (ptr_size) {
27358 .One, .Slice => break :ty lhs_ty, // invalid, let it error
27359 .Many, .C => break :ty .usize, // `[*]T + usize`
27360 }
27361 },
27362 .sub_eq => ty: {
27363 const ptr_size = lhs_ty.ptrSizeOrNull(zcu) orelse break :ty lhs_ty;
27364 switch (ptr_size) {
27365 .One, .Slice => break :ty lhs_ty, // invalid, let it error
27366 .Many, .C => break :ty .generic_poison, // could be `[*]T - [*]T` or `[*]T - usize`
27367 }
27368 },
27369 };
27370 return Air.internedToRef(ty.toIntern());
27371}
27372
27345fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {27373fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
27346 const pt = sema.pt;27374 const pt = sema.pt;
27347 const zcu = pt.zcu;27375 const zcu = pt.zcu;
src/print_zir.zig+7
...@@ -620,6 +620,7 @@ const Writer = struct {...@@ -620,6 +620,7 @@ const Writer = struct {
620 .closure_get => try self.writeClosureGet(stream, extended),620 .closure_get => try self.writeClosureGet(stream, extended),
621 .field_parent_ptr => try self.writeFieldParentPtr(stream, extended),621 .field_parent_ptr => try self.writeFieldParentPtr(stream, extended),
622 .builtin_value => try self.writeBuiltinValue(stream, extended),622 .builtin_value => try self.writeBuiltinValue(stream, extended),
623 .inplace_arith_result_ty => try self.writeInplaceArithResultTy(stream, extended),
623 }624 }
624 }625 }
625626
...@@ -2781,6 +2782,12 @@ const Writer = struct {...@@ -2781,6 +2782,12 @@ const Writer = struct {
2781 try self.writeSrcNode(stream, @bitCast(extended.operand));2782 try self.writeSrcNode(stream, @bitCast(extended.operand));
2782 }2783 }
27832784
2785 fn writeInplaceArithResultTy(self: *Writer, stream: anytype, extended: Zir.Inst.Extended.InstData) !void {
2786 const op: Zir.Inst.InplaceOp = @enumFromInt(extended.small);
2787 try self.writeInstRef(stream, @enumFromInt(extended.operand));
2788 try stream.print(", {s}))", .{@tagName(op)});
2789 }
2790
2784 fn writeInstRef(self: *Writer, stream: anytype, ref: Zir.Inst.Ref) !void {2791 fn writeInstRef(self: *Writer, stream: anytype, ref: Zir.Inst.Ref) !void {
2785 if (ref == .none) {2792 if (ref == .none) {
2786 return stream.writeAll(".none");2793 return stream.writeAll(".none");
test/behavior/pointers.zig+15
...@@ -98,6 +98,21 @@ test "pointer subtraction" {...@@ -98,6 +98,21 @@ test "pointer subtraction" {
98 }98 }
99}99}
100100
101test "pointer arithmetic with non-trivial RHS" {
102 var t: bool = undefined;
103 t = true;
104
105 var ptr: [*]const u8 = "Hello, World!";
106 ptr += if (t) 5 else 2;
107 try expect(ptr[0] == ',');
108 ptr += if (!t) 4 else 2;
109 try expect(ptr[0] == 'W');
110 ptr -= if (t) @as(usize, 6) else 3;
111 try expect(ptr[0] == 'e');
112 ptr -= if (!t) @as(usize, 0) else 1;
113 try expect(ptr[0] == 'H');
114}
115
101test "double pointer parsing" {116test "double pointer parsing" {
102 comptime assert(PtrOf(PtrOf(i32)) == **i32);117 comptime assert(PtrOf(PtrOf(i32)) == **i32);
103}118}