diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2024-09-15 15:45:20 +0100 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2024-09-16 16:42:42 +0100 |
| commit | 1365be5d02429dbbbfab43133d399b2af42047c5 (patch) | |
| tree | 2cb61dabb88b84094701df907ad67479951f53a8 /src/Sema.zig | |
| parent | 5d7fa5513f92a43a418e3c5c4d27f0b61db313ff (diff) | |
| download | zig-1365be5d02429dbbbfab43133d399b2af42047c5.tar.gz zig-1365be5d02429dbbbfab43133d399b2af42047c5.zip | |
compiler: provide correct result types to `+=` and `-=`
Resolves: #21341
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 7a4b9b3210..b859b8a183 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -1361,6 +1361,7 @@ fn analyzeBodyInner( .value_placeholder => unreachable, // never appears in a body .field_parent_ptr => try sema.zirFieldParentPtr(block, extended), .builtin_value => try sema.zirBuiltinValue(extended), + .inplace_arith_result_ty => try sema.zirInplaceArithResultTy(extended), }; }, @@ -27342,6 +27343,33 @@ fn zirBuiltinValue(sema: *Sema, extended: Zir.Inst.Extended.InstData) CompileErr return Air.internedToRef(ty.toIntern()); } +fn zirInplaceArithResultTy(sema: *Sema, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { + const pt = sema.pt; + const zcu = pt.zcu; + + const lhs = try sema.resolveInst(@enumFromInt(extended.operand)); + const lhs_ty = sema.typeOf(lhs); + + const op: Zir.Inst.InplaceOp = @enumFromInt(extended.small); + const ty: Type = switch (op) { + .add_eq => ty: { + const ptr_size = lhs_ty.ptrSizeOrNull(zcu) orelse break :ty lhs_ty; + switch (ptr_size) { + .One, .Slice => break :ty lhs_ty, // invalid, let it error + .Many, .C => break :ty .usize, // `[*]T + usize` + } + }, + .sub_eq => ty: { + const ptr_size = lhs_ty.ptrSizeOrNull(zcu) orelse break :ty lhs_ty; + switch (ptr_size) { + .One, .Slice => break :ty lhs_ty, // invalid, let it error + .Many, .C => break :ty .generic_poison, // could be `[*]T - [*]T` or `[*]T - usize` + } + }, + }; + return Air.internedToRef(ty.toIntern()); +} + fn zirBranchHint(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void { const pt = sema.pt; const zcu = pt.zcu; |
