================
@@ -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.
----------------
efriedma-quic wrote:

When we know two pointers point into same object, pointer equality is trivial.  
When we know two pointers point into different objects, we can follow the rules 
you outline.

The problem is if we don't know whether the two pointers point into different 
objects.  Then weird things start happening, and you have to start digging into 
other sections of the standard.  Like, consider:

```
void f() {
  void* x = malloc(4);
  int* ip = new(x)ip;
  int & ir = *ip;
  float *fp = new(x)fp;
  float &fr = *fp;
  static_assert(&ir != &fr)
}
```

If you take the rules literally, the static_assert is valid, and succeeds.  
"the reference is treated as binding to an unspecified object of the referenced 
type whose lifetime and that of all subobjects includes the entire constant 
evaluation"... and a given object can only have one type, so the two objects 
must be distinct, so the pointers can't be equal.

It would help if the standard said that an equality comparison is not a core 
constant expression if it contains "an equality comparison where one of the 
operands points into a constexpr-unknown object".

https://github.com/llvm/llvm-project/pull/147663
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to