[llvm-bugs] [Bug 50036] New: Comparing pointer into string literal is not a constant expression

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Apr 19 21:11:23 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=50036

            Bug ID: 50036
           Summary: Comparing pointer into string literal is not a
                    constant expression
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: davidfromonline at gmail.com
                CC: blitzrakete at gmail.com, dgregor at apple.com,
                    erik.pilkington at gmail.com, llvm-bugs at lists.llvm.org,
                    richard-llvm at metafoo.co.uk

The following well-formed translation unit

```
static_assert(+"a" != +"b");
```

is rejected by clang with the error message

```
<source>:1:20: error: static_assert expression is not an integral constant
expression
static_assert(+"a" != +"b");
              ~~~~~^~~~~~~
1 error generated.
Compiler returned: 1
```

>From the standard:

"An expression E is a core constant expression unless the evaluation of E,
following the rules of the abstract machine ([intro.execution]), would evaluate
one of the following: ... a three-way comparison ([expr.spaceship]), relational
([expr.rel]), or equality ([expr.eq]) operator where the result is
unspecified;" (http://eel.is/c++draft/expr.const#5.23)

And:


If at least one of the operands is a pointer, pointer conversions, function
pointer conversions, and qualification conversions are performed on both
operands to bring them to their composite pointer type.
Comparing pointers is defined as follows:

 - If one pointer represents the address of a complete object, and another
pointer represents the address one past the last element of a different
complete object,75 the result of the comparison is unspecified.
 - Otherwise, if the pointers are both null, both point to the same function,
or both represent the same address, they compare equal.
 - Otherwise, the pointers compare unequal.

(http://eel.is/c++draft/expr.eq#3)


The unspecified condition does not apply, so this should be accepted.


See it live: https://godbolt.org/z/vrWh54KM1

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210420/334cb9b6/attachment.html>


More information about the llvm-bugs mailing list