-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[clang] Fix pointer comparisons between pointers to constexpr-unknown #147663
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
base: main
Are you sure you want to change the base?
[clang] Fix pointer comparisons between pointers to constexpr-unknown #147663
Conversation
A constexpr-unknown reference can be equal to an arbitrary value, except values allocated during constant evaluation. Fix the handling. The standard is unclear exactly which pointer comparisons count as "unknown" in this context; for example, in some cases we could use alignment to prove two constexpr-unknown references are not equal. I decided to ignore all the cases involving variables not allocated during constant evaluation. While looking at this, I also spotted that there might be issues with lifetimes, but I didn't try to address it.
@llvm/pr-subscribers-clang Author: Eli Friedman (efriedma-quic) ChangesA constexpr-unknown reference can be equal to an arbitrary value, except values allocated during constant evaluation. Fix the handling. The standard is unclear exactly which pointer comparisons count as "unknown" in this context; for example, in some cases we could use alignment to prove two constexpr-unknown references are not equal. I decided to ignore all the cases involving variables not allocated during constant evaluation. While looking at this, I also spotted that there might be issues with lifetimes, but I didn't try to address it. Full diff: https://github.com/llvm/llvm-project/pull/147663.diff 2 Files Affected:
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 60c658a8d8f99..6ade7e6ec8a6a 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14477,12 +14477,6 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
if (!EvaluatePointer(E->getRHS(), RHSValue, Info) || !LHSOK)
return false;
- // If we have Unknown pointers we should fail if they are not global values.
- if (!(IsGlobalLValue(LHSValue.getLValueBase()) &&
- IsGlobalLValue(RHSValue.getLValueBase())) &&
- (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
- return false;
-
// Reject differing bases from the normal codepath; we special-case
// comparisons to null.
if (!HasSameBase(LHSValue, RHSValue)) {
@@ -14544,6 +14538,23 @@ EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
(LHSValue.Base && isZeroSized(RHSValue)))
return DiagComparison(
diag::note_constexpr_pointer_comparison_zero_sized);
+ // A constexpr-unknown reference can be equal to any other lvalue, except
+ // for variables allocated during constant evaluation. (The "lifetime
+ // [...] includes the entire constant evaluation", so it has to be
+ // distinct from anything allocated during constant evaluation.)
+ //
+ // Theoretically we could handle other cases, but the standard doesn't say
+ // what other cases we need to handle; it just says an "equality
+ // operator where the result is unspecified" isn't a constant expression.
+ auto AllocatedDuringEval = [](LValue &Value) {
+ return Value.Base.is<DynamicAllocLValue>() ||
+ Value.getLValueCallIndex();
+ };
+ if ((LHSValue.AllowConstexprUnknown && !AllocatedDuringEval(RHSValue)) ||
+ (RHSValue.AllowConstexprUnknown && !AllocatedDuringEval(LHSValue)))
+ return DiagComparison(
+ diag::note_constexpr_pointer_comparison_unspecified);
+ // FIXME: Verify both variables are live.
return Success(CmpResult::Unequal, E);
}
diff --git a/clang/test/SemaCXX/constant-expression-p2280r4.cpp b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
index dffb386f530f4..640ac18aad738 100644
--- a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
+++ b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
@@ -319,7 +319,7 @@ namespace casting {
}
namespace pointer_comparisons {
- extern int &extern_n; // interpreter-note 2 {{declared here}}
+ extern int &extern_n; // interpreter-note 4 {{declared here}}
extern int &extern_n2;
constexpr int f1(bool b, int& n) {
if (b) {
@@ -330,14 +330,29 @@ namespace pointer_comparisons {
// FIXME: interpreter incorrectly rejects; both sides are the same constexpr-unknown value.
static_assert(f1(false, extern_n)); // interpreter-error {{static assertion expression is not an integral constant expression}} \
// interpreter-note {{initializer of 'extern_n' is unknown}}
- // FIXME: We should diagnose this: we don't know if the references bind
- // to the same object.
- static_assert(&extern_n != &extern_n2); // interpreter-error {{static assertion expression is not an integral constant expression}} \
+ static_assert(&extern_n != &extern_n2); // expected-error {{static assertion expression is not an integral constant expression}} \
+ // nointerpreter-note {{comparison between pointers to unrelated objects '&extern_n' and '&extern_n2' has unspecified value}} \
// interpreter-note {{initializer of 'extern_n' is unknown}}
void f2(const int &n) {
- // FIXME: We should not diagnose this: the two objects provably have
- // different addresses because the lifetime of "n" extends across
- // the initialization.
- constexpr int x = &x == &n; // nointerpreter-error {{must be initialized by a constant expression}}
+ // We can prove these two aren't equal, but for now we don't try.
+ constexpr int x = &x == &n; // nointerpreter-error {{must be initialized by a constant expression}} \
+ // nointerpreter-note {{comparison between pointers to unrelated objects '&x' and '&n' has unspecified value}}
+ // Distinct variables are not equal, even if they're local variables.
+ constexpr int y = &x == &y;
+ static_assert(!y);
}
+ constexpr int f3() {
+ int x;
+ return &x == &extern_n; // interpreter-note {{initializer of 'extern_n' is unknown}}
+ }
+ static_assert(!f3()); // interpreter-error {{static assertion expression is not an integral constant expression}} \
+ // interpreter-note {{in call to 'f3()'}}
+ constexpr int f4() {
+ int *p = new int;
+ bool b = p == &extern_n; // interpreter-note {{initializer of 'extern_n' is unknown}}
+ delete p;
+ return b;
+ }
+ static_assert(!f4()); // interpreter-error {{static assertion expression is not an integral constant expression}} \
+ // interpreter-note {{in call to 'f4()'}}
}
|
A constexpr-unknown reference can be equal to an arbitrary value, except values allocated during constant evaluation. Fix the handling.
The standard is unclear exactly which pointer comparisons count as "unknown" in this context; for example, in some cases we could use alignment to prove two constexpr-unknown references are not equal. I decided to ignore all the cases involving variables not allocated during constant evaluation.
While looking at this, I also spotted that there might be issues with lifetimes, but I didn't try to address it.