https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127005
Bug ID: 127005
Summary: Wmaybe-uninitialized with optional<lock_guard<mutex>>
Product: gcc
Version: 16.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: jengelh at inai dot de
Target Milestone: ---
Input
=====
#include <optional>
#include <mutex>
struct conn {
void close() { guard.reset(); }
std::optional<std::lock_guard<std::mutex>> guard;
};
extern conn make_conn();
int main()
{
conn c = make_conn();
c.close();
}
Observed output
===============
$ g++-16 -c -O2 -Wall 1.cpp
/usr/include/c++/16/bits/std_mutex.h:283:9: warning:
‘*(std::lock_guard<std::mutex>*)((char*)&c + offsetof(conn,
conn::guard.std::optional<std::lock_guard<std::mutex>
>::<unnamed>.std::_Optional_base<std::lock_guard<std::mutex>, false,
false>::_M_payload.std::_Optional_payload<std::lock_guard<std::mutex>, false,
false, false>::<unnamed>.std::_Optional_payload<std::lock_guard<std::mutex>,
true, false,
false>::<unnamed>.std::_Optional_payload_base<std::lock_guard<std::mutex>
>::_M_payload)).std::lock_guard<std::mutex>::_M_device’ may be used
uninitialized [-Wmaybe-uninitialized]
283 | { _M_device.unlock(); }
Expected behavior/output
========================
No warnings.
Version
=======
Target: x86_64-suse-linux
Configured with: ../configure CFLAGS=' -fcf-protection -O2 -funwind-tables
-fasynchronous-unwind-tables -fstack-clash-protection -Werror=return-type -g'
CXXFLAGS=' -fcf-protection -O2 -funwind-tables -fasynchronous-unwind-tables
-fstack-clash-protection -Werror=return-type -g' XCFLAGS=' -fcf-protection -O2
-funwind-tables -fasynchronous-unwind-tables -fstack-clash-protection
-Werror=return-type -g' TCFLAGS=' -fcf-protection -O2 -funwind-tables
-fasynchronous-unwind-tables -fstack-clash-protection -Werror=return-type -g'
GDCFLAGS=' -fcf-protection -O2 -funwind-tables -fasynchronous-unwind-tables
-fstack-clash-protection -g' --prefix=/usr --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64
--enable-languages=c,c++,objc,fortran,obj-c++,ada,go,d,jit,m2,cobol,algol68
--enable-offload-targets=nvptx-none,amdgcn-amdhsa, --enable-offload-defaulted
--without-cuda-driver --enable-host-shared --enable-checking=release
--disable-werror --with-gxx-include-dir=/usr/include/c++/16
--with-libstdcxx-zoneinfo=/usr/share/zoneinfo --enable-ssp --disable-libssp
--disable-libvtv --enable-cet=auto --disable-libcc1 --enable-plugin
--with-bugurl=https://bugs.opensuse.org/ --with-pkgversion='SUSE Linux'
--with-slibdir=/lib64 --with-system-zlib --enable-libstdcxx-allocator=new
--disable-libstdcxx-pch --enable-libphobos
--enable-version-specific-runtime-libs --with-gcc-major-version-only
--enable-linker-build-id --enable-linux-futex --enable-gnu-indirect-function
--program-suffix=-16 --without-system-libunwind --enable-multilib
--with-arch-32=x86-64 --with-tune=generic
--with-build-config=bootstrap-lto-lean --enable-link-serialization
--build=x86_64-suse-linux --host=x86_64-suse-linux
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 16.2.0 (SUSE Linux)
Reproduces on godbolt.org with g++-14,15,16 and -O2 -Wall.