Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser: allow pointer subtraction of differently-qualified types #834

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
}
Loading