public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104163] New: Misleading diagnostic for incompletely initialized constinit variable
@ 2022-01-21 13:36 redi at gcc dot gnu.org
  2022-01-21 13:38 ` [Bug c++/104163] " redi at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-21 13:36 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104163
           Summary: Misleading diagnostic for incompletely initialized
                    constinit variable
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

struct B
{
  int i = 0;
};

struct C
{
  void* p;
  B b;
  constexpr C() = default;
};

constinit C c{};

This is invalid because C::p is left uninitialized by the constructor, but
GCC's warning is confusing:

mut.C:13:13: error: 'constinit' variable 'c' does not have a constant
initializer
   13 | constinit C c{};
      |             ^
mut.C:13:15: error: 'C{B{0}}' is not a constant expression
   13 | constinit C c{};
      |               ^
mut.C:13:15: error: 'C()' is not a constant expression because it refers to an
incompletely initialized variable


The "'C{B{0}}' is not a constant expression" makes it look like the problem is
related to initialization of B. There is no mention of the p member. I spent
quite a bit of time trying to figure out what was wrong in a more complex,
deeply nested example.


Clang tells you exactly what's wrong:

mut.C:13:13: error: variable does not have a constant initializer
constinit C c{};
            ^~~
mut.C:13:1: note: required by 'constinit' specifier here
constinit C c{};
^~~~~~~~~
mut.C:13:13: note: subobject of type 'void *' is not initialized
constinit C c{};
            ^
mut.C:8:9: note: subobject declared here
  void* p;
        ^
1 error generated.


The notes about the void* are clear and precise.



EDG is less clear, but at least still identifies C::p as the problem:


"mut.C", line 13: error: expression must have a constant value
  constinit C c{};
               ^
"mut.C", line 13: note: access to uninitialized subobject (member "C::p")
  constinit C c{};
               ^

1 error detected in the compilation of "mut.C".

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/104163] Misleading diagnostic for incompletely initialized constinit variable
  2022-01-21 13:36 [Bug c++/104163] New: Misleading diagnostic for incompletely initialized constinit variable redi at gcc dot gnu.org
@ 2022-01-21 13:38 ` redi at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-21 13:38 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-01-21
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The deeply nested case was:

at.C:9:48: error: 'std::atomic<std::shared_ptr<int>
>{std::_Sp_atomic<std::shared_ptr<int> >{std::_Sp_atomic<std::shared_ptr<int>
>::_Atomic_count{std::__atomic_base<long unsigned int>{0}}}}' is not a constant
expression

The real problem was a member of _Sp_atomic, so everything about
std::_Sp_atomic<std::shared_ptr<int> >::_Atomic_count{std::__atomic_base<long
unsigned int>{0}} was a red herring.

Simplified:

struct atomic_base
{
  constexpr atomic_base(int i) : i(i) { }
  int i{0};
};

struct Sp_atomic
{
  struct Atomic_count
  {
    constexpr Atomic_count() = default;
    atomic_base a{0};
  };

  void* ptr;
  Atomic_count c{};
  constexpr Sp_atomic() = default;
};

struct atomic
{
  constexpr atomic() = default;
  Sp_atomic s;
};


constinit atomic a{};


mut.C:27:18: error: 'constinit' variable 'a' does not have a constant
initializer
   27 | constinit atomic a{};
      |                  ^
mut.C:27:20: error:
'atomic{Sp_atomic{Sp_atomic::Atomic_count{atomic_base{0}}}}' is not a constant
expression
   27 | constinit atomic a{};
      |                    ^
mut.C:27:20: error: 'atomic()' is not a constant expression because it refers
to an incompletely initialized variable

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-21 13:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-21 13:36 [Bug c++/104163] New: Misleading diagnostic for incompletely initialized constinit variable redi at gcc dot gnu.org
2022-01-21 13:38 ` [Bug c++/104163] " redi at gcc dot gnu.org

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).