public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "miro.palmu at helsinki dot fi" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/112642] New: ranges::fold_left tries to access inactive union member of string in constant expression
Date: Mon, 20 Nov 2023 15:28:50 +0000	[thread overview]
Message-ID: <bug-112642-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112642

            Bug ID: 112642
           Summary: ranges::fold_left tries to access inactive union
                    member of string in constant expression
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: miro.palmu at helsinki dot fi
  Target Milestone: ---

Following example will fail to compile. Error message is included below but
most important part is here:

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

Example:

#include <string>
#include <functional>
#include <algorithm>
#include <vector>

using namespace std;
using namespace std::literals;

constexpr auto foo() {
    const auto vec = vector{ "a"s, "b"s, "c"s };
    const auto concat = ranges::fold_left(vec, ""s, plus{});
    return concat.size();
}

int main() {
    constexpr auto _ = foo();
}

Error message:

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure
--enable-languages=ada,c,c++,d,fortran,go,lto,objc,obj-c++ --enable-bootstrap
--prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/
--with-build-config=bootstrap-lto --with-linker-hash-style=gnu
--with-system-zlib --enable-__cxa_atexit --enable-cet=auto
--enable-checking=release --enable-clocale=gnu --enable-default-pie
--enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object
--enable-libstdcxx-backtrace --enable-link-serialization=1
--enable-linker-build-id --enable-lto --enable-multilib --enable-plugin
--enable-shared --enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-werror
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.2.1 20230801 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++23' '-shared-libgcc'
'-mtune=generic' '-march=x86-64' '-dumpdir' 'a-'
 /usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/cc1plus -E -quiet -v -D_GNU_SOURCE
prog.cpp -mtune=generic -march=x86-64 -std=c++23 -fpch-preprocess -o a-prog.ii
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1

/usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/x86_64-pc-linux-gnu

/usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/backward
 /usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++23' '-shared-libgcc'
'-mtune=generic' '-march=x86-64' '-dumpdir' 'a-'
 /usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/cc1plus -fpreprocessed a-prog.ii
-quiet -dumpdir a- -dumpbase prog.cpp -dumpbase-ext .cpp -mtune=generic
-march=x86-64 -std=c++23 -version -o a-prog.s
GNU C++23 (GCC) version 13.2.1 20230801 (x86_64-pc-linux-gnu)
        compiled by GNU C version 13.2.1 20230801, GMP version 6.3.0, MPFR
version 4.2.0-p12, MPC version 1.3.1, isl version isl-0.26-GMP

warning: MPFR header version 4.2.0-p12 differs from library version 4.2.1.
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 5a490a353c29b926850bca65a518c219
prog.cpp: In function ‘int main()’:
prog.cpp:16:27:   in ‘constexpr’ expansion of ‘foo()’
prog.cpp:16:28:   in ‘constexpr’ expansion of
‘std::ranges::__fold_left_fn::operator()(_Range&&, _Tp, _Fp) const [with _Range
= const std::vector<std::__cxx11::basic_string<char>,
std::allocator<std::__cxx11::basic_string<char> > >&; _Tp =
std::__cxx11::basic_string<char>; _Fp = std::plus<void>](vec,
std::literals::string_literals::operator""s(const char*, std::size_t)(0),
(std::plus<void>(), std::plus<void>()))’
prog.cpp:16:28:   in ‘constexpr’ expansion of
‘std::__cxx11::basic_string<char>((* &
std::move<__cxx11::basic_string<char>&>(__init)))’
prog.cpp:16:28: 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
   16 |     constexpr auto _ = foo();
      |                            ^

             reply	other threads:[~2023-11-20 15:28 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-20 15:28 miro.palmu at helsinki dot fi [this message]
2023-11-20 15:35 ` [Bug libstdc++/112642] " redi at gcc dot gnu.org
2023-11-20 16:02 ` redi at gcc dot gnu.org
2023-11-20 16:50 ` miro.palmu at helsinki dot fi
2023-11-20 17:10 ` [Bug c++/112642] " redi at gcc dot gnu.org
2023-11-21 17:48 ` miro.palmu at helsinki dot fi
2023-11-21 20:14 ` redi at gcc dot gnu.org
2023-11-22 11:22 ` miro.palmu at helsinki dot fi
2023-11-22 12:04 ` redi at gcc dot gnu.org
2023-11-22 12:31 ` miro.palmu at helsinki dot fi
2023-11-22 17:19 ` redi at gcc dot gnu.org

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-112642-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: link
Be 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).