https://github.com/efriedma-quic updated 
https://github.com/llvm/llvm-project/pull/147663

>From 1e51f7bd82fe91d6d13bee6f634d7401a3ddbc8a Mon Sep 17 00:00:00 2001
From: Eli Friedman <efrie...@quicinc.com>
Date: Tue, 8 Jul 2025 23:35:26 -0700
Subject: [PATCH 1/2] [clang] Fix pointer comparisons between pointers to
 constexpr-unknown

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.
---
 clang/lib/AST/ExprConstant.cpp                | 23 ++++++++++----
 .../SemaCXX/constant-expression-p2280r4.cpp   | 31 ++++++++++++++-----
 2 files changed, 40 insertions(+), 14 deletions(-)

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()'}}
 }

>From f5941256beff45e3f3e54582f862d10addbeda6e Mon Sep 17 00:00:00 2001
From: Eli Friedman <efrie...@quicinc.com>
Date: Fri, 11 Jul 2025 15:44:10 -0700
Subject: [PATCH 2/2] Address review comments.

---
 clang/docs/ReleaseNotes.rst                       |  1 +
 clang/lib/AST/ExprConstant.cpp                    | 15 +--------------
 clang/test/SemaCXX/constant-expression-cxx14.cpp  | 15 +++++++++++++++
 .../test/SemaCXX/constant-expression-p2280r4.cpp  | 15 ++++++++-------
 4 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8b4f9229c4463..db8e4897040b7 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -923,6 +923,7 @@ Bug Fixes to C++ Support
 - Improved handling of variables with ``consteval`` constructors, to
   consistently treat the initializer as manifestly constant-evaluated.
   (#GH135281)
+- Fixed constant evaluation of equality comparisons of constexpr-unknown 
references. (#GH147663)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 6ade7e6ec8a6a..d45ba9322c945 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14538,20 +14538,7 @@ 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)))
+      if (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown)
         return DiagComparison(
             diag::note_constexpr_pointer_comparison_unspecified);
       // FIXME: Verify both variables are live.
diff --git a/clang/test/SemaCXX/constant-expression-cxx14.cpp 
b/clang/test/SemaCXX/constant-expression-cxx14.cpp
index e16a69df3830d..e93b98c185a82 100644
--- a/clang/test/SemaCXX/constant-expression-cxx14.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx14.cpp
@@ -1321,3 +1321,18 @@ constexpr bool check = different_in_loop();
   // expected-error@-1 {{}} expected-note@-1 {{in call}}
 
 }
+
+namespace comparison_dead_variable {
+  constexpr bool f() {
+    int *p1 = 0, *p2 = 0;
+    {
+        int x = 0; p1 = &x;
+    }
+    {
+        int x = 0; p2 = &x;
+    }
+    return p1 != p2;
+  }
+  // FIXME: This should fail.
+  static_assert(f(),"");
+}
diff --git a/clang/test/SemaCXX/constant-expression-p2280r4.cpp 
b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
index 640ac18aad738..03fea91169787 100644
--- a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
+++ b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
@@ -334,7 +334,6 @@ namespace pointer_comparisons {
                                           // 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) {
-    // 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.
@@ -343,16 +342,18 @@ namespace pointer_comparisons {
   }
   constexpr int f3() {
     int x;
-    return &x == &extern_n; // interpreter-note {{initializer of 'extern_n' is 
unknown}}
+    return &x == &extern_n; // nointerpreter-note {{comparison between 
pointers to unrelated objects '&x' and '&extern_n' has unspecified value}} \
+                            // 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()'}}
+  static_assert(!f3()); // expected-error {{static assertion expression is not 
an integral constant expression}} \
+                        // expected-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}}
+    bool b = p == &extern_n; // nointerpreter-note {{comparison between 
pointers to unrelated objects '&{*new int#0}' and '&extern_n' has unspecified 
value}} \
+                             // 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()'}}
+  static_assert(!f4()); // expected-error {{static assertion expression is not 
an integral constant expression}} \
+                        // expected-note {{in call to 'f4()'}}
 }

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to