public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "arthur.j.odwyer at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/114817] New: Wrong codegen for std::copy of "trivially copyable but not trivially assignable" type Date: Tue, 23 Apr 2024 04:02:26 +0000 [thread overview] Message-ID: <bug-114817-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114817 Bug ID: 114817 Summary: Wrong codegen for std::copy of "trivially copyable but not trivially assignable" type Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: arthur.j.odwyer at gmail dot com Target Milestone: --- Jiang An raises an interesting issue over at https://github.com/llvm/llvm-project/pull/89652#discussion_r1575540420 namely: // https://godbolt.org/z/64KGP1avE template<class T, class U> struct EvilPair { T& first; U& second; EvilPair(T& t, U& u) : first(t), second(u) {} EvilPair(const EvilPair&) = default; void operator=(const volatile EvilPair&) = delete; template<class = void> EvilPair& operator=(const EvilPair& rhs) { first = rhs.first; second = rhs.second; return *this; } }; static_assert(std::is_trivially_copyable_v<EvilPair<int&, int&>>); int main() { int a[] = {1,2,3}; int b[] = {4,5,6}; int c[] = {0,0,0}; int d[] = {0,0,0}; EvilPair<int&, int&> ps[] = { {a[0], b[0]}, {a[1], b[1]}, {a[2], b[2]}, }; EvilPair<int&, int&> qs[] = { {c[0], d[0]}, {c[1], d[1]}, {c[2], d[2]}, }; std::copy(ps, ps+3, qs); printf("%d %d %d\n", c[0], c[1], c[2]); } Here, EvilPair is trivially copyable, and also copy-assignable, but it is not trivially copy-assignable. libstdc++'s std::copy assumes that any trivially copyable type can be... well, trivially copied. So it copies the object representation of EvilPair, instead of doing overload resolution to discover that in fact the templated `operator=` should be called instead. Looks like the std::copy optimization was introduced in the GCC 9 release. Allegedly Microsoft STL's `std::pair<int&, int&>` is isomorphic to `EvilPair` these days. libc++'s `std::copy` avoids this issue (AFAICT) by gating their optimization on *both* is_trivially_assignable and is_trivially_copyable. I suspect there will be similar issues with `uninitialized_foo` functions and/or vector reallocation, for types that are trivially copyable (or trivially relocatable!) and yet not trivially destructive-movable.
next reply other threads:[~2024-04-23 4:02 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top 2024-04-23 4:02 arthur.j.odwyer at gmail dot com [this message] 2024-04-23 18:29 ` [Bug libstdc++/114817] " arthur.j.odwyer at gmail dot com 2024-04-24 3:31 ` de34 at live dot cn 2024-04-26 18:43 ` arthur.j.odwyer at gmail dot com
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-114817-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).