public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal
@ 2024-01-02 15:41 pdimov at gmail dot com
2024-01-02 16:45 ` [Bug c++/113200] " pinskia at gcc dot gnu.org
` (13 more replies)
0 siblings, 14 replies; 15+ messages in thread
From: pdimov at gmail dot com @ 2024-01-02 15:41 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
Bug ID: 113200
Summary: std::char_traits<char>::move is not constexpr when the
argument is a string literal
Product: gcc
Version: 13.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: pdimov at gmail dot com
Target Milestone: ---
```
#include <string>
#include <cassert>
template<std::size_t N> struct S
{
char data_[ N ];
using traits_type = std::char_traits<char>;
constexpr S( char const* p ): data_{}
{
std::size_t n = traits_type::length( p );
assert( n < N );
traits_type::move( data_, p, n + 1 );
}
};
template<std::size_t N> S( char const(&)[N] ) -> S<N>;
constexpr S s( "test" );
```
(https://godbolt.org/z/PofY8MP6G)
fails with
```
In file included from
/opt/compiler-explorer/gcc-trunk-20240102/include/c++/14.0.0/string:42,
from <source>:1:
<source>:22:23: in 'constexpr' expansion of 'S<5>(((const char*)"test"))'
<source>:16:26: in 'constexpr' expansion of
'std::char_traits<char>::move(((char*)(&((S<5>*)this)->S<5>::data_)), p, (n +
1))'
/opt/compiler-explorer/gcc-trunk-20240102/include/c++/14.0.0/bits/char_traits.h:423:50:
in 'constexpr' expansion of '__gnu_cxx::char_traits<char>::move(__s1, __s2,
__n)'
/opt/compiler-explorer/gcc-trunk-20240102/include/c++/14.0.0/bits/char_traits.h:230:20:
error: '(((const __gnu_cxx::char_traits<char>::char_type*)(& s.S<5>::data_)) ==
((const char*)"test"))' is not a constant expression
230 | if (__s1 == __s2) // unlikely, but saves a lot of work
| ~~~~~^~~~~~~
```
(Reduced from a similar failure in Boost.StaticString.)
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug c++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
@ 2024-01-02 16:45 ` pinskia at gcc dot gnu.org
2024-01-02 16:47 ` [Bug libstdc++/113200] " pinskia at gcc dot gnu.org
` (12 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-01-02 16:45 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Blocks| |55004
Keywords| |rejects-valid
Component|libstdc++ |c++
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Looks more like a front-end issue ...
Referenced Bugs:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55004
[Bug 55004] [meta-bug] constexpr issues
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug libstdc++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
2024-01-02 16:45 ` [Bug c++/113200] " pinskia at gcc dot gnu.org
@ 2024-01-02 16:47 ` pinskia at gcc dot gnu.org
2024-01-02 16:49 ` pdimov at gmail dot com
` (11 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-01-02 16:47 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Component|c++ |libstdc++
--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I take that back, clang provideds a better error message on why `&a ==
"string"` is not constexpr:
```
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/bits/char_traits.h:230:13:
note: comparison of addresses of literals has unspecified value
230 | if (__s1 == __s2) // unlikely, but saves a lot of work
| ^
```
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug libstdc++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
2024-01-02 16:45 ` [Bug c++/113200] " pinskia at gcc dot gnu.org
2024-01-02 16:47 ` [Bug libstdc++/113200] " pinskia at gcc dot gnu.org
@ 2024-01-02 16:49 ` pdimov at gmail dot com
2024-01-02 16:51 ` pdimov at gmail dot com
` (10 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: pdimov at gmail dot com @ 2024-01-02 16:49 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
--- Comment #3 from Peter Dimov <pdimov at gmail dot com> ---
I think that the compiler is correct; string literal address comparisons aren't
constant expressions. Clang gives the same error:
https://godbolt.org/z/xPWEf4z63.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug libstdc++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
` (2 preceding siblings ...)
2024-01-02 16:49 ` pdimov at gmail dot com
@ 2024-01-02 16:51 ` pdimov at gmail dot com
2024-01-03 11:13 ` [Bug c++/113200] " redi at gcc dot gnu.org
` (9 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: pdimov at gmail dot com @ 2024-01-02 16:51 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
--- Comment #4 from Peter Dimov <pdimov at gmail dot com> ---
I didn't notice your subsequent comment, sorry. :-)
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug c++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
` (3 preceding siblings ...)
2024-01-02 16:51 ` pdimov at gmail dot com
@ 2024-01-03 11:13 ` redi at gcc dot gnu.org
2024-01-03 11:23 ` [Bug libstdc++/113200] " redi at gcc dot gnu.org
` (8 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-03 11:13 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Last reconfirmed| |2024-01-03
Component|libstdc++ |c++
Status|UNCONFIRMED |NEW
Keywords|rejects-valid |diagnostic
Ever confirmed|0 |1
--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Let's recategorize this as a front-end diagnostic issue. GCC's error is bad.
That's mostly because of the ((char*)(&((S<5>*)this)->S<5>::data_)) noise, but
it would also be good to explicitly state that comparisons with literals give
unspecified results and so are not constant expressions.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug libstdc++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
` (4 preceding siblings ...)
2024-01-03 11:13 ` [Bug c++/113200] " redi at gcc dot gnu.org
@ 2024-01-03 11:23 ` redi at gcc dot gnu.org
2024-01-03 12:57 ` redi at gcc dot gnu.org
` (7 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-03 11:23 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Component|c++ |libstdc++
Keywords|diagnostic |rejects-valid
--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Although we should fix the libstdc++ problem first, then re-assign (or maybe
file a separate bug for the FE diagnostic).
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug libstdc++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
` (5 preceding siblings ...)
2024-01-03 11:23 ` [Bug libstdc++/113200] " redi at gcc dot gnu.org
@ 2024-01-03 12:57 ` redi at gcc dot gnu.org
2024-01-03 13:01 ` redi at gcc dot gnu.org
` (6 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-03 12:57 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Why does GCC accept this reduced version, which is invalid for the same reason
as the original?
#include <string>
constexpr int N = 5;
struct S
{
char data_[ N ];
constexpr S( char const* p )
{
std::char_traits<char>::move( data_, p, N );
}
};
constexpr S s( "test" );
Clang rejects it the same way.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug libstdc++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
` (6 preceding siblings ...)
2024-01-03 12:57 ` redi at gcc dot gnu.org
@ 2024-01-03 13:01 ` redi at gcc dot gnu.org
2024-01-04 1:58 ` de34 at live dot cn
` (5 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-03 13:01 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #7)
> Why does GCC accept this reduced version, which is invalid for the same
> reason as the original?
Looks like PR 70248
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug libstdc++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
` (7 preceding siblings ...)
2024-01-03 13:01 ` redi at gcc dot gnu.org
@ 2024-01-04 1:58 ` de34 at live dot cn
2024-01-05 10:23 ` cvs-commit at gcc dot gnu.org
` (4 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: de34 at live dot cn @ 2024-01-04 1:58 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
Jiang An <de34 at live dot cn> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |de34 at live dot cn
--- Comment #9 from Jiang An <de34 at live dot cn> ---
(In reply to Peter Dimov from comment #3)
> I think that the compiler is correct; string literal address comparisons
> aren't constant expressions. Clang gives the same error:
> https://godbolt.org/z/xPWEf4z63.
This looks weird... It seems that `+"abc" == +"def"` is never unspecified (must
be false, but Clang rejects it in constant evaluation), while `"abcd" + 1 ==
+"bcd"` is unspecified.
It's unclear to me whether we can practically detect all kinds of
unspecifiedness in pointer comparision involving string literals.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug libstdc++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
` (8 preceding siblings ...)
2024-01-04 1:58 ` de34 at live dot cn
@ 2024-01-05 10:23 ` cvs-commit at gcc dot gnu.org
2024-01-05 11:46 ` redi at gcc dot gnu.org
` (3 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-01-05 10:23 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
--- Comment #10 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:
https://gcc.gnu.org/g:15cc291887dc9dd92b2c93f4545e20eb6c190122
commit r14-6944-g15cc291887dc9dd92b2c93f4545e20eb6c190122
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Wed Jan 3 15:01:09 2024 +0000
libstdc++: Fix std::char_traits<C>::move [PR113200]
The current constexpr implementation of std::char_traits<C>::move relies
on being able to compare the pointer parameters, which is not allowed
for unrelated pointers. We can use __builtin_constant_p to determine
whether it's safe to compare the pointers directly. If not, then we know
the ranges must be disjoint and so we can use char_traits<C>::copy to
copy forwards from the first character to the last. If the pointers can
be compared directly, then we can simplify the condition for copying
backwards to just two pointer comparisons.
libstdc++-v3/ChangeLog:
PR libstdc++/113200
* include/bits/char_traits.h (__gnu_cxx::char_traits::move): Use
__builtin_constant_p to check for unrelated pointers that cannot
be compared during constant evaluation.
* testsuite/21_strings/char_traits/requirements/113200.cc: New
test.
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug libstdc++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
` (9 preceding siblings ...)
2024-01-05 10:23 ` cvs-commit at gcc dot gnu.org
@ 2024-01-05 11:46 ` redi at gcc dot gnu.org
2024-01-06 13:40 ` cvs-commit at gcc dot gnu.org
` (2 subsequent siblings)
13 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-05 11:46 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Target Milestone|--- |12.4
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug libstdc++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
` (10 preceding siblings ...)
2024-01-05 11:46 ` redi at gcc dot gnu.org
@ 2024-01-06 13:40 ` cvs-commit at gcc dot gnu.org
2024-01-11 23:16 ` cvs-commit at gcc dot gnu.org
2024-01-11 23:17 ` redi at gcc dot gnu.org
13 siblings, 0 replies; 15+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-01-06 13:40 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
--- Comment #11 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:
https://gcc.gnu.org/g:22601c1c25652c71c8bab4707866c020d6dad79a
commit r13-8193-g22601c1c25652c71c8bab4707866c020d6dad79a
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Wed Jan 3 15:01:09 2024 +0000
libstdc++: Fix std::char_traits<C>::move [PR113200]
The current constexpr implementation of std::char_traits<C>::move relies
on being able to compare the pointer parameters, which is not allowed
for unrelated pointers. We can use __builtin_constant_p to determine
whether it's safe to compare the pointers directly. If not, then we know
the ranges must be disjoint and so we can use char_traits<C>::copy to
copy forwards from the first character to the last. If the pointers can
be compared directly, then we can simplify the condition for copying
backwards to just two pointer comparisons.
libstdc++-v3/ChangeLog:
PR libstdc++/113200
* include/bits/char_traits.h (__gnu_cxx::char_traits::move): Use
__builtin_constant_p to check for unrelated pointers that cannot
be compared during constant evaluation.
* testsuite/21_strings/char_traits/requirements/113200.cc: New
test.
(cherry picked from commit 15cc291887dc9dd92b2c93f4545e20eb6c190122)
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug libstdc++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
` (11 preceding siblings ...)
2024-01-06 13:40 ` cvs-commit at gcc dot gnu.org
@ 2024-01-11 23:16 ` cvs-commit at gcc dot gnu.org
2024-01-11 23:17 ` redi at gcc dot gnu.org
13 siblings, 0 replies; 15+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-01-11 23:16 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
--- Comment #12 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-12 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:
https://gcc.gnu.org/g:26a9e8cee4d20e5b08c0336439c8f69a2f06af1c
commit r12-10090-g26a9e8cee4d20e5b08c0336439c8f69a2f06af1c
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Wed Jan 3 15:01:09 2024 +0000
libstdc++: Fix std::char_traits<C>::move [PR113200]
The current constexpr implementation of std::char_traits<C>::move relies
on being able to compare the pointer parameters, which is not allowed
for unrelated pointers. We can use __builtin_constant_p to determine
whether it's safe to compare the pointers directly. If not, then we know
the ranges must be disjoint and so we can use char_traits<C>::copy to
copy forwards from the first character to the last. If the pointers can
be compared directly, then we can simplify the condition for copying
backwards to just two pointer comparisons.
libstdc++-v3/ChangeLog:
PR libstdc++/113200
* include/bits/char_traits.h (__gnu_cxx::char_traits::move): Use
__builtin_constant_p to check for unrelated pointers that cannot
be compared during constant evaluation.
* testsuite/21_strings/char_traits/requirements/113200.cc: New
test.
(cherry picked from commit 15cc291887dc9dd92b2c93f4545e20eb6c190122)
^ permalink raw reply [flat|nested] 15+ messages in thread
* [Bug libstdc++/113200] std::char_traits<char>::move is not constexpr when the argument is a string literal
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
` (12 preceding siblings ...)
2024-01-11 23:16 ` cvs-commit at gcc dot gnu.org
@ 2024-01-11 23:17 ` redi at gcc dot gnu.org
13 siblings, 0 replies; 15+ messages in thread
From: redi at gcc dot gnu.org @ 2024-01-11 23:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113200
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |FIXED
Status|NEW |RESOLVED
--- Comment #13 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for 12.4 and 13.3, thanks for the report.
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2024-01-11 23:17 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-02 15:41 [Bug libstdc++/113200] New: std::char_traits<char>::move is not constexpr when the argument is a string literal pdimov at gmail dot com
2024-01-02 16:45 ` [Bug c++/113200] " pinskia at gcc dot gnu.org
2024-01-02 16:47 ` [Bug libstdc++/113200] " pinskia at gcc dot gnu.org
2024-01-02 16:49 ` pdimov at gmail dot com
2024-01-02 16:51 ` pdimov at gmail dot com
2024-01-03 11:13 ` [Bug c++/113200] " redi at gcc dot gnu.org
2024-01-03 11:23 ` [Bug libstdc++/113200] " redi at gcc dot gnu.org
2024-01-03 12:57 ` redi at gcc dot gnu.org
2024-01-03 13:01 ` redi at gcc dot gnu.org
2024-01-04 1:58 ` de34 at live dot cn
2024-01-05 10:23 ` cvs-commit at gcc dot gnu.org
2024-01-05 11:46 ` redi at gcc dot gnu.org
2024-01-06 13:40 ` cvs-commit at gcc dot gnu.org
2024-01-11 23:16 ` cvs-commit at gcc dot gnu.org
2024-01-11 23:17 ` redi at gcc dot gnu.org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).