public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "tomas.pecka at cesnet dot cz" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/109434] New: std::optional weird -Wmaybe-unitialized and behaviour with -O2
Date: Thu, 06 Apr 2023 11:32:10 +0000	[thread overview]
Message-ID: <bug-109434-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 109434
           Summary: std::optional weird -Wmaybe-unitialized and behaviour
                    with -O2
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tomas.pecka at cesnet dot cz
  Target Milestone: ---

Created attachment 54816
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54816&action=edit
reproducer

Hello,

I've seen a compile warning on a code (see attachment) that looks bogus. 

  In file included from optional.cpp:2:
  In member function ‘constexpr bool std::_Optional_base_impl<_Tp,
_Dp>::_M_is_engaged() const [with _Tp = int; _Dp = std::_Optional_base<int,
true, true>]’,
      inlined from ‘constexpr bool std::optional<_Tp>::has_value() const [with
_Tp = int]’ at /usr/include/c++/12.2.1/optional:988:35,
      inlined from ‘int main()’ at optional.cpp:21:63:
  /usr/include/c++/12.2.1/optional:471:58: warning: ‘*(unsigned
char*)((char*)&optInt +
offsetof(std::optional<int>,std::optional<int>::<unnamed>.std::_Optional_base<int,
true, true>::_M_payload.std::_Optional_payload<int, true, true,
true>::<unnamed>.std::_Optional_payload_base<int>::_M_engaged))’ may be used
uninitialized [-Wmaybe-uninitialized]
    471 |       { return static_cast<const _Dp*>(this)->_M_payload._M_engaged;
}
        |                                                          ^~~~~~~~~~
  optional.cpp: In function ‘int main()’:
  optional.cpp:15:24: note: ‘*(unsigned char*)((char*)&optInt +
offsetof(std::optional<int>,std::optional<int>::<unnamed>.std::_Optional_base<int,
true,   true>::_M_payload.std::_Optional_payload<int, true, true,
true>::<unnamed>.std::_Optional_payload_base<int>::_M_engaged))’ was declared
here
     15 |     std::optional<int> optInt;
        |                        ^~~~~~

The warning is present only when compiling with -Wall -O2. When I run the
executable, I see unexpected weird output, e.g.

  catch 56
  val=72704.000000

which look like some uninitialized variable is really there. When run under
valgrind, I see reports about unitialized variables.

Code seems to be working correctly when compiled with -O1 or lower and the
executable seem to be behaving expectedly as well.
Compiling and executing under clang++ works as well regardless of the
optimization level. 
I don't have older versions of g++ (< 12) but compiling the attached code on
godbolt with g++ 11 and lower does not trigger any warning.

Interesting part is that declaring optDbl as std::optional<int> makes the code
behave correctly.

             reply	other threads:[~2023-04-06 11:32 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-06 11:32 tomas.pecka at cesnet dot cz [this message]
2023-04-06 11:52 ` [Bug tree-optimization/109434] [12/13 Regression] " pinskia at gcc dot gnu.org
2023-04-08 14:31 ` law at gcc dot gnu.org
2023-04-11 12:41 ` rguenth at gcc dot gnu.org
2023-04-11 12:55 ` rguenth at gcc dot gnu.org
2023-04-12  6:49 ` cvs-commit at gcc dot gnu.org
2023-04-12  6:52 ` [Bug tree-optimization/109434] [12 " rguenth at gcc dot gnu.org
2023-04-13  7:44 ` [Bug tree-optimization/109434] [12 Regression] std::optional weird -Wmaybe-uninitialized " tomas.pecka at cesnet dot cz
2023-04-17 10:45 ` cvs-commit at gcc dot gnu.org
2023-04-17 10:47 ` rguenth 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-109434-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).