public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/111258] New: std::string cannot to be moved in constant evaluated expression
@ 2023-08-31 13:56 kisses07_rupee at icloud dot com
2023-09-01 3:29 ` [Bug libstdc++/111258] " pinskia at gcc dot gnu.org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: kisses07_rupee at icloud dot com @ 2023-08-31 13:56 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111258
Bug ID: 111258
Summary: std::string cannot to be moved in constant evaluated
expression
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: kisses07_rupee at icloud dot com
Target Milestone: ---
c++20 std::string's destructor fails after move:
#include <string>
using namespace std::literals;
constexpr auto foo(auto a) {
auto b = std::move(a);
return b[1];
}
int main(int,char**) {
static_assert( foo("hello"s) == 'e' );
return 0;
}
fails with:
<source>: In function 'int main(int, char**)':
<source>:11:34: error: non-constant condition for static assertion
11 | static_assert( foo("hello"s) == 'e' );
| ~~~~~~~~~~~~~~^~~~~~
<source>:11:23: in 'constexpr' expansion of
'foo<std::__cxx11::basic_string<char>
>(std::literals::string_literals::operator""s(const char*, std::size_t)(5))'
<source>:11:42: in 'constexpr' expansion of
'std::__cxx11::basic_string<char>((* &
std::move<__cxx11::basic_string<char>&>(a)))'
<source>:11:34: error: accessing 'std::__cxx11::basic_string<char>::<unnamed
union>::_M_allocated_capacity' member instead of initialized
'std::__cxx11::basic_string<char>::<unnamed union>::_M_local_buf' member in
constant expression
Compiler returned: 1
online demo: https://godbolt.org/z/Tz7MK58f1
it seems it's affecting on bug 103924
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug libstdc++/111258] std::string cannot to be moved in constant evaluated expression
2023-08-31 13:56 [Bug libstdc++/111258] New: std::string cannot to be moved in constant evaluated expression kisses07_rupee at icloud dot com
@ 2023-09-01 3:29 ` pinskia at gcc dot gnu.org
2023-09-01 7:20 ` de34 at live dot cn
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-01 3:29 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111258
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Keywords| |rejects-valid
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note clang rejects (libstdc++ version) for a different reason:
```
<source>:12:20: error: static assertion expression is not an integral constant
expression
12 | static_assert( foo("hello"s) == 'e' );
| ^~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/bits/basic_string.h:626:2:
note: undefined function '_M_construct<const char *>' cannot be used in a
constant expression
626 | _M_construct(__s, __s + __n, std::forward_iterator_tag());
| ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/bits/basic_string.h:4686:14:
note: in call to 'basic_string(&"hello"[0], 5, std::allocator<char>())'
4686 | { return basic_string<char>{__str, __len}; }
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:12:24: note: in call to 'operator""s(&"hello"[0], 5)'
12 | static_assert( foo("hello"s) == 'e' );
| ^~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/bits/basic_string.h:336:9:
note: declared here
336 | _M_construct(_FwdIterator __beg, _FwdIterator __end,
| ^
```
clang accepts LLVM's libc++ version though.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug libstdc++/111258] std::string cannot to be moved in constant evaluated expression
2023-08-31 13:56 [Bug libstdc++/111258] New: std::string cannot to be moved in constant evaluated expression kisses07_rupee at icloud dot com
2023-09-01 3:29 ` [Bug libstdc++/111258] " pinskia at gcc dot gnu.org
@ 2023-09-01 7:20 ` de34 at live dot cn
2023-09-04 11:18 ` de34 at live dot cn
2024-04-26 17:13 ` ppalka at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: de34 at live dot cn @ 2023-09-01 7:20 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111258
Jiang An <de34 at live dot cn> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |de34 at live dot cn
--- Comment #2 from Jiang An <de34 at live dot cn> ---
Perhaps this is a compiler bug and there's nothing wrong in the move
constructor.
GCC 13 also incorrectly rejects the following program
(https://godbolt.org/z/Yn98KMKv5)
```
#include <string>
constexpr bool foo11(std::string s)
{
s.push_back('\0');
return true;
}
int main()
{
static_assert(foo11(std::string{}));
}
```
with similar message:
```
<source>: In function 'int main()':
<source>:12:24: error: non-constant condition for static assertion
12 | static_assert(foo11(std::string{}));
| ~~~~~^~~~~~~~~~~~~~~
<source>:12:24: in 'constexpr' expansion of
'foo11(std::__cxx11::basic_string<char>())'
<source>:5:16: in 'constexpr' expansion of
's.std::__cxx11::basic_string<char>::push_back(0)'
/opt/compiler-explorer/gcc-13.2.0/include/c++/13.2.0/bits/basic_string.h:1552:33:
in 'constexpr' expansion of
'((std::__cxx11::basic_string<char>*)this)->std::__cxx11::basic_string<char>::capacity()'
<source>:12:24: error: accessing 'std::__cxx11::basic_string<char>::<unnamed
union>::_M_allocated_capacity' member instead of initialized
'std::__cxx11::basic_string<char>::<unnamed union>::_M_local_buf' member in
constant expression
Compiler returned: 1
```
It seems that gcc currently miscompiles passing-by-value of
non-trivially-copyable classes in constant evaluation.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug libstdc++/111258] std::string cannot to be moved in constant evaluated expression
2023-08-31 13:56 [Bug libstdc++/111258] New: std::string cannot to be moved in constant evaluated expression kisses07_rupee at icloud dot com
2023-09-01 3:29 ` [Bug libstdc++/111258] " pinskia at gcc dot gnu.org
2023-09-01 7:20 ` de34 at live dot cn
@ 2023-09-04 11:18 ` de34 at live dot cn
2024-04-26 17:13 ` ppalka at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: de34 at live dot cn @ 2023-09-04 11:18 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111258
--- Comment #3 from Jiang An <de34 at live dot cn> ---
I've reduced the example and filed Bug 111284.
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug libstdc++/111258] std::string cannot to be moved in constant evaluated expression
2023-08-31 13:56 [Bug libstdc++/111258] New: std::string cannot to be moved in constant evaluated expression kisses07_rupee at icloud dot com
` (2 preceding siblings ...)
2023-09-04 11:18 ` de34 at live dot cn
@ 2024-04-26 17:13 ` ppalka at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: ppalka at gcc dot gnu.org @ 2024-04-26 17:13 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111258
--- Comment #4 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Many of these testcases (from here and the related PRs) are now accepted by GCC
14 after r14-10134.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2024-04-26 17:13 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-31 13:56 [Bug libstdc++/111258] New: std::string cannot to be moved in constant evaluated expression kisses07_rupee at icloud dot com
2023-09-01 3:29 ` [Bug libstdc++/111258] " pinskia at gcc dot gnu.org
2023-09-01 7:20 ` de34 at live dot cn
2023-09-04 11:18 ` de34 at live dot cn
2024-04-26 17:13 ` ppalka 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).