diff options
| author | Matthew Lugg <mlugg@mlugg.co.uk> | 2024-09-16 22:04:06 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-16 22:04:06 +0100 |
| commit | 7caa3d9da71c38665340247a1c2bf9bedb8db925 (patch) | |
| tree | dcb7637f26db5363d0bdc3c60fd05660f8a371fd /src/Sema.zig | |
| parent | f3445f8f6935b4532aab3f339f5d86319d2dca72 (diff) | |
| parent | 7f60d2e4658ad78839ce0fce63a95dbcb893a256 (diff) | |
| download | zig-7caa3d9da71c38665340247a1c2bf9bedb8db925.tar.gz zig-7caa3d9da71c38665340247a1c2bf9bedb8db925.zip | |
Merge pull request #21425 from mlugg/pointer-arith-inplace-res-ty
compiler: provide correct result types to `+=` and `-=`
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 559fbe3e53..ded3999b84 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; |
