public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression
@ 2023-06-07 15:22 fchelnokov at gmail dot com
2023-06-07 19:40 ` [Bug c++/110158] " pinskia at gcc dot gnu.org
` (8 more replies)
0 siblings, 9 replies; 10+ messages in thread
From: fchelnokov at gmail dot com @ 2023-06-07 15:22 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110158
Bug ID: 110158
Summary: Cannot use union with std::string inside in constant
expression
Product: gcc
Version: 13.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: fchelnokov at gmail dot com
Target Milestone: ---
This program
constexpr bool f() {
union U{
std::string s;
constexpr ~U(){ s.~basic_string(); }
} u{};
return true;
}
static_assert( f() );
is accepted in MSVC and Clang with libc++. But in GCC it produces the error:
error: non-constant condition for static assertion
in 'constexpr' expansion of 'f()'
in 'constexpr' expansion of '(& u)->f()::U::~U()'
in 'constexpr' expansion of
'((f()::U*)this)->f()::U::s.std::__cxx11::basic_string<char>::~basic_string()'
/opt/compiler-explorer/gcc-13.1.0/include/c++/13.1.0/bits/basic_string.h:803:19:
in 'constexpr' expansion of
'((std::__cxx11::basic_string<char>*)this)->std::__cxx11::basic_string<char>::_M_dispose()'
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
Online demo: https://gcc.godbolt.org/z/bbf4Yo3v9
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/110158] Cannot use union with std::string inside in constant expression
2023-06-07 15:22 [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression fchelnokov at gmail dot com
@ 2023-06-07 19:40 ` pinskia at gcc dot gnu.org
2023-08-03 17:11 ` ppalka at gcc dot gnu.org
` (7 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-07 19:40 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110158
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Here is a slightly reduced testcase (for a slightly different issue still
dealing with unions):
```
struct str1
{
// bool a;
char *var;
union {
char t[15];
int allocated;
};
constexpr str1() : var(new char[2]) { t[0] = 0; }
constexpr ~str1() {if (var != t) delete[] var; }
};
typedef str1 str;
constexpr bool f1() {
str t{};
return true;
}
static_assert( f1() );
constexpr bool f() {
union U{
str s;
constexpr ~U(){ s.~str(); }
} u{};
return true;
}
static_assert( f() );
```
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/110158] Cannot use union with std::string inside in constant expression
2023-06-07 15:22 [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression fchelnokov at gmail dot com
2023-06-07 19:40 ` [Bug c++/110158] " pinskia at gcc dot gnu.org
@ 2023-08-03 17:11 ` ppalka at gcc dot gnu.org
2023-08-04 15:55 ` pinskia at gcc dot gnu.org
` (6 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-08-03 17:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110158
Patrick Palka <ppalka at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |ppalka at gcc dot gnu.org
See Also| |https://gcc.gnu.org/bugzill
| |a/show_bug.cgi?id=110245
--- Comment #2 from Patrick Palka <ppalka at gcc dot gnu.org> ---
It works if we activate the union's member:
constexpr bool f() {
union U{
std::string s = {};
constexpr ~U(){ s.~basic_string(); }
} u{};
return true;
}
static_assert( f() );
So perhaps this is a dup of PR110245?
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/110158] Cannot use union with std::string inside in constant expression
2023-06-07 15:22 [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression fchelnokov at gmail dot com
2023-06-07 19:40 ` [Bug c++/110158] " pinskia at gcc dot gnu.org
2023-08-03 17:11 ` ppalka at gcc dot gnu.org
@ 2023-08-04 15:55 ` pinskia at gcc dot gnu.org
2023-08-04 16:16 ` danakj at orodu dot net
` (5 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-04 15:55 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110158
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |danakj at orodu dot net
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 110900 has been marked as a duplicate of this bug. ***
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/110158] Cannot use union with std::string inside in constant expression
2023-06-07 15:22 [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression fchelnokov at gmail dot com
` (2 preceding siblings ...)
2023-08-04 15:55 ` pinskia at gcc dot gnu.org
@ 2023-08-04 16:16 ` danakj at orodu dot net
2023-09-06 18:11 ` danakj at orodu dot net
` (4 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: danakj at orodu dot net @ 2023-08-04 16:16 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110158
--- Comment #4 from danakj at orodu dot net ---
Here's a repro without the std::string inside a union. It is the SSO union
inside the string that causes the error.
https://gcc.godbolt.org/z/T8oM8vYnq
```
#include <string>
template <class T, class I, class S, class F>
constexpr T fold(T init, I i, S s, F f) {
while (true) {
if (i == s)
return init;
else
init = f(std::move(init), *i++);
}
}
constexpr char v[] = {'a', 'b', 'c'};
static_assert(fold(std::string(), std::begin(v), std::end(v),
[](std::string acc, char v) {
acc.push_back(v);
return acc;
}) == "abc");
int main() {}
```
<source>:18:23: error: non-constant condition for static assertion
14 | static_assert(fold(std::string(), std::begin(v), std::end(v),
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 | [](std::string acc, char v) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16 | acc.push_back(v);
| ~~~~~~~~~~~~~~~~~
17 | return acc;
| ~~~~~~~~~~~
18 | }) == "abc");
| ~~~^~~~~~~~
<source>:18:32: in 'constexpr' expansion of 'fold(T, I, S, F) [with T =
std::__cxx11::basic_string<char>; I = const char*; S = const char*; F =
<lambda(std::string, char)>](std::begin<const char, 3>(v), std::end<const char,
3>(v), (<lambda closure object><lambda(std::string, char)>(),
<lambda(std::string, char)>()))'
<source>:18:32: in 'constexpr' expansion of
'std::__cxx11::basic_string<char>((* &
std::move<__cxx11::basic_string<char>&>(init)))'
<source>:18:23: 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
ASM generation compiler returned: 1
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/110158] Cannot use union with std::string inside in constant expression
2023-06-07 15:22 [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression fchelnokov at gmail dot com
` (3 preceding siblings ...)
2023-08-04 16:16 ` danakj at orodu dot net
@ 2023-09-06 18:11 ` danakj at orodu dot net
2023-09-06 18:18 ` danakj at orodu dot net
` (3 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: danakj at orodu dot net @ 2023-09-06 18:11 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110158
--- Comment #5 from danakj at orodu dot net ---
As a means to work around #4 in static asserts, making your string long enough
that it avoids the SSO will allow the compiler to accept it.
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/110158] Cannot use union with std::string inside in constant expression
2023-06-07 15:22 [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression fchelnokov at gmail dot com
` (4 preceding siblings ...)
2023-09-06 18:11 ` danakj at orodu dot net
@ 2023-09-06 18:18 ` danakj at orodu dot net
2023-09-29 11:08 ` redi at gcc dot gnu.org
` (2 subsequent siblings)
8 siblings, 0 replies; 10+ messages in thread
From: danakj at orodu dot net @ 2023-09-06 18:18 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110158
--- Comment #6 from danakj at orodu dot net ---
In case it is of help, here's an even smaller repro that clang reports the
error in libstdc++. For whatever reason gcc does not notice the libstdc++ bug
here.
https://gcc.godbolt.org/z/Phndafeoe
```
#include <string>
struct S {
constexpr ~S() {}
std::string s;
};
constexpr std::string foo(const S& s) { return s.s; }
static_assert(foo(S("hi")) == "hi"); // Fails.
static_assert(foo(S("a longer string works")) == "a longer string works");
int main() {}
```
The error:
```
<source>:11:15: error: static assertion expression is not an integral constant
expression
static_assert(foo(S("hi")) == "hi");
^~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../include/c++/12.2.0/bits/basic_string.h:356:10:
note: assignment to member '_M_local_buf' of union with no active member is not
allowed in a constant expression
__c = _CharT();
^
/opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../include/c++/12.2.0/bits/basic_string.tcc:229:4:
note: in call to '&S(("hi")).s->_M_use_local_data()'
_M_use_local_data();
^
/opt/compiler-explorer/gcc-12.2.0/lib/gcc/x86_64-linux-gnu/12.2.0/../../../../include/c++/12.2.0/bits/basic_string.h:642:2:
note: in call to '&S(("hi")).s->_M_construct(&"hi"[0], &"hi"[2], {{}})'
_M_construct(__s, __end, forward_iterator_tag());
^
<source>:11:21: note: in call to 'basic_string(&"hi"[0],
std::allocator<char>())'
static_assert(foo(S("hi")) == "hi");
^
1 error generated.
```
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/110158] Cannot use union with std::string inside in constant expression
2023-06-07 15:22 [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression fchelnokov at gmail dot com
` (5 preceding siblings ...)
2023-09-06 18:18 ` danakj at orodu dot net
@ 2023-09-29 11:08 ` redi at gcc dot gnu.org
2023-11-22 0:45 ` ppalka at gcc dot gnu.org
2023-11-22 11:54 ` redi at gcc dot gnu.org
8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2023-09-29 11:08 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110158
--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
That bug in comment 6 has already been fixed in gcc-12.3, by the last patch for
PR 103295.
The one in comment 4 is different, and will be fixed by Nathaniel's latest
patch:
https://gcc.gnu.org/pipermail/gcc-patches/2023-September/631642.html
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/110158] Cannot use union with std::string inside in constant expression
2023-06-07 15:22 [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression fchelnokov at gmail dot com
` (6 preceding siblings ...)
2023-09-29 11:08 ` redi at gcc dot gnu.org
@ 2023-11-22 0:45 ` ppalka at gcc dot gnu.org
2023-11-22 11:54 ` redi at gcc dot gnu.org
8 siblings, 0 replies; 10+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-11-22 0:45 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110158
--- Comment #8 from Patrick Palka <ppalka at gcc dot gnu.org> ---
It seems the comment #4 testcase is still rejected on trunk after/despite
r14-4334-g28adad7a32ed92?
^ permalink raw reply [flat|nested] 10+ messages in thread
* [Bug c++/110158] Cannot use union with std::string inside in constant expression
2023-06-07 15:22 [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression fchelnokov at gmail dot com
` (7 preceding siblings ...)
2023-11-22 0:45 ` ppalka at gcc dot gnu.org
@ 2023-11-22 11:54 ` redi at gcc dot gnu.org
8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2023-11-22 11:54 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110158
--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Odd, I thought I'd checked it when testing r14-4334-g28adad7a32ed92.
Seems like the same issue as PR 112642 though (which has a minimized version
without std::string).
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2023-11-22 11:54 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-07 15:22 [Bug libstdc++/110158] New: Cannot use union with std::string inside in constant expression fchelnokov at gmail dot com
2023-06-07 19:40 ` [Bug c++/110158] " pinskia at gcc dot gnu.org
2023-08-03 17:11 ` ppalka at gcc dot gnu.org
2023-08-04 15:55 ` pinskia at gcc dot gnu.org
2023-08-04 16:16 ` danakj at orodu dot net
2023-09-06 18:11 ` danakj at orodu dot net
2023-09-06 18:18 ` danakj at orodu dot net
2023-09-29 11:08 ` redi at gcc dot gnu.org
2023-11-22 0:45 ` ppalka at gcc dot gnu.org
2023-11-22 11:54 ` 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).