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

            Bug ID: 125013
           Summary: False maybe-uninitialized wrapping std::optional
           Product: gcc
           Version: 14.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jesper at arista dot com
  Target Milestone: ---

The -Wmaybe-uninitialized generated in the below code appears to be a false
warning.

Compile as

% g++-14 -std=c++20 -O2 -Wall -Wmaybe-uninitialized -c
gcc_bug_reproduction_simplified.cpp
In member function 'void Wrapper::update(Wrapper)',
    inlined from 'void caller(Wrapper*, bool, State)' at
gcc_bug_reproduction_simplified.cpp:32:13:
gcc_bug_reproduction_simplified.cpp:25:13: warning: '*(int*)((char*)&p +
offsetof(Wrapper,
Wrapper::opt.std::optional<State>::<unnamed>.std::_Optional_base<State, true,
true>::<unnamed>))' may be used uninitialized [-Wmaybe-uninitialized]
   25 |       *this = p;
      |       ~~~~~~^~~
gcc_bug_reproduction_simplified.cpp: In function 'void caller(Wrapper*, bool,
State)':
gcc_bug_reproduction_simplified.cpp:31:12: note: '*(int*)((char*)&p +
offsetof(Wrapper,
Wrapper::opt.std::optional<State>::<unnamed>.std::_Optional_base<State, true,
true>::<unnamed>))' was declared here
   31 |    Wrapper p( cond, s );
      |            ^
% g++-14 --version
g++-14 (GCC) 14.3.1 20250617 (Red Hat 14.3.1-2)
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The same error is also seen with GCC11

% g++ -std=c++20 -O2 -Wall -Wmaybe-uninitialized -c
gcc_bug_reproduction_simplified.cpp
gcc_bug_reproduction_simplified.cpp: In function 'void caller(Wrapper*, bool,
State)':
gcc_bug_reproduction_simplified.cpp:25:13: warning: '*(State*)((char*)&p +
offsetof(Wrapper,
Wrapper::opt.std::optional<State>::<unnamed>.std::_Optional_base<State, true,
true>::<unnamed>)).State::v' may be used uninitialized in this function
[-Wmaybe-uninitialized]
   25 |       *this = p;
      |       ~~~~~~^~~
gcc_bug_reproduction_simplified.cpp:31:12: note: '*(State*)((char*)&p +
offsetof(Wrapper,
Wrapper::opt.std::optional<State>::<unnamed>.std::_Optional_base<State, true,
true>::<unnamed>)).State::v' was declared here
   31 |    Wrapper p( cond, s );
      |            ^
% g++ --version
g++ (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4)
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Seen on linux

% uname -a
Linux jesper-marcoOpt-56.sjc.aristanetworks.com
5.10.228-18.202507280947.el9.x86_64 #1 SMP Mon Jul 28 09:49:24 UTC 2025 x86_64
x86_64 x86_64 GNU/Linux

gcc_bug_reproduction_simplified.cpp:

#include <optional>

struct State {
   int v;
   bool operator==( const State & ) const = default;
};

struct Wrapper {
   std::optional< State > opt;

   Wrapper() = default;
   Wrapper( bool cond, State s ) : Wrapper() {
      if ( cond )
         opt.emplace( s );
   }
   bool operator==( const Wrapper & ) const = default;

   void update( Wrapper p ) {
      if ( p == *this )
         return;
      *this = p;
   }
};

void
caller( Wrapper * w, bool cond, State s ) {
   Wrapper p( cond, s );
   w->update( p );
}

Reply via email to