Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Comparing pointer into string literal is not a constant expression #49005

Open Quuxplusone opened 3 years ago

Quuxplusone commented 3 years ago
Bugzilla Link PR50036
Status NEW
Importance P enhancement
Reported by David Stone (davidfromonline@gmail.com)
Reported on 2021-04-19 21:11:23 -0700
Last modified on 2021-11-12 10:31:55 -0800
Version trunk
Hardware PC Linux
CC blitzrakete@gmail.com, dblaikie@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also

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:

(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

Quuxplusone commented 2 years ago

I also believe the following code should be accepted

constexpr char const array[] = "a";
static_assert(+array != "a");

The text at http://eel.is/c++draft/lex.string#9 says: "Evaluating a string-literal results in a string literal object with static storage duration ([basic.stc]). Whether all string-literals are distinct (that is, are stored in nonoverlapping objects) and whether successive evaluations of a string-literal yield the same or a different object is unspecified."

This text does not apply here (array is not a string literal), so this comparison is also not unspecified.