Skip to content

Commit

Permalink
Parser: allow pointer subtraction of differently-qualified types
Browse files Browse the repository at this point in the history
Fixes #833
  • Loading branch information
ehaas authored and Vexu committed Jan 8, 2025
1 parent 58a9330 commit b0dec78
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5921,7 +5921,7 @@ pub const Result = struct {
return a.shouldEval(b, p);
},
.sub => {
// if both aren't arithmetic then either both should be pointers or just the lelft one.
// if both aren't arithmetic then either both should be pointers or just the left one.
if (!a_sk.isPointer() or !(b_sk.isPointer() or b_sk.isInt())) return a.invalidBinTy(tok, b, p);

if (a_sk == .void_pointer)
Expand All @@ -5931,7 +5931,10 @@ pub const Result = struct {
if (b_sk == .nullptr_t) try b.nullToPointer(p, .void_pointer, tok);

if (a_sk.isPointer() and b_sk.isPointer()) {
if (!a.qt.eql(b.qt, p.comp)) try p.errStr(.incompatible_pointers, tok, try p.typePairStr(a.qt, b.qt));
const a_child_qt = a.qt.get(p.comp, .pointer).?.child;
const b_child_qt = b.qt.get(p.comp, .pointer).?.child;

if (!a_child_qt.eql(b_child_qt, p.comp)) try p.errStr(.incompatible_pointers, tok, try p.typePairStr(a.qt, b.qt));
if (a.qt.childType(p.comp).sizeofOrNull(p.comp) orelse 1 == 0) try p.errStr(.subtract_pointers_zero_elem_size, tok, try p.typeStr(a.qt.childType(p.comp)));
a.qt = p.comp.type_store.ptrdiff;
}
Expand Down
6 changes: 6 additions & 0 deletions test/cases/pointer subtraction.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stddef.h>
ptrdiff_t foo(int x) {
char c[2] = {0, 0};
const char *p = &c[1];
return p - &c[0];
}

0 comments on commit b0dec78

Please sign in to comment.