Skip to content

[clang][ExprConst] Diagnose ptr subs with non-zero offset #135938

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

Merged
merged 1 commit into from
Apr 17, 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
3 changes: 0 additions & 3 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14764,9 +14764,6 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
// Reject differing bases from the normal codepath; we special-case
// comparisons to null.
if (!HasSameBase(LHSValue, RHSValue)) {
// Handle &&A - &&B.
if (!LHSValue.Offset.isZero() || !RHSValue.Offset.isZero())
return Error(E);
const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>();
const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>();

Expand Down
2 changes: 1 addition & 1 deletion clang/test/AST/ByteCode/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static_assert(k1 == 1, "");
static_assert((&arr[0] - &arr[1]) == -1, "");

constexpr int k2 = &arr2[1] - &arr[0]; // both-error {{must be initialized by a constant expression}} \
// expected-note {{arithmetic involving unrelated objects}}
// both-note {{arithmetic involving unrelated objects}}

static_assert((arr + 0) == arr, "");
static_assert(&arr[0] == arr, "");
Expand Down
4 changes: 4 additions & 0 deletions clang/test/SemaCXX/constant-expression-cxx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ constexpr int a = 0;
constexpr int b = 1;
constexpr int n = &b - &a; // expected-error {{must be initialized by a constant expression}} \
// expected-note {{arithmetic involving unrelated objects '&b' and '&a' has unspecified value}}
constexpr static int arrk[2] = {1,2};
constexpr static int arrk2[2] = {3,4};
constexpr int k2 = &arrk[1] - &arrk2[0]; // expected-error {{must be initialized by a constant expression}} \
// expected-note {{arithmetic involving unrelated objects}}

namespace MaterializeTemporary {

Expand Down
Loading