public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101780] New: Missing initializers whereas structure has default initializers
@ 2021-08-04 19:53 deco33000 at yandex dot com
  2021-08-05 11:39 ` [Bug c++/101780] " redi at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: deco33000 at yandex dot com @ 2021-08-04 19:53 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101780
           Summary: Missing initializers whereas structure has default
                    initializers
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: deco33000 at yandex dot com
  Target Milestone: ---

Hi,

The issue is a false positive warning being issued whereas even if there are
missing initializers, the rest of the struct is default initialized.


// Type your code here, or load an example.
#include <iostream>
#include <vector>
using namespace std;

struct A {
  union {
    int i = 82, j;
  };
  int a = 98, b = 22;
};

int main() {
  A polo = {.a = 45};

  cout << polo.a << endl;
  cout << polo.i << endl;

  return 0;
}
warning: missing initializer for member 'A::<anonymous>'
[-Wmissing-field-initializers]


Try on godbolt:
https://godbolt.org/z/ohnWPqcE5

Can you confirm it is the intended behavior?
If so, could we make the message clearer, adding: "the following designated
fields X,Y,Z will be default initialized. P,Q,R are left uninitialized."

That way, it is much more useful as a warning, instead of making the dev fear
of undefined behavior, when there is not.

Thanks,

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

* [Bug c++/101780] Missing initializers whereas structure has default initializers
  2021-08-04 19:53 [Bug c++/101780] New: Missing initializers whereas structure has default initializers deco33000 at yandex dot com
@ 2021-08-05 11:39 ` redi at gcc dot gnu.org
  2021-08-05 14:17 ` deco33000 at yandex dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-05 11:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to KL from comment #0)
> Can you confirm it is the intended behavior?

See the documentation for -Wmissing-field-initializers.

It doesn't warn about members being uninitialized, it warns about them not
being explicitly initialized, which is the case here.

The manual does say it doesn't warn about designated initializers, but it seems
the anonymous union is confusing it.

> If so, could we make the message clearer, adding: "the following designated
> fields X,Y,Z will be default initialized. P,Q,R are left uninitialized."

How would P,Q,R be left uninitialized?

> That way, it is much more useful as a warning, instead of making the dev
> fear of undefined behavior, when there is not.

It doesn't say anything about undefined behaviour.

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

* [Bug c++/101780] Missing initializers whereas structure has default initializers
  2021-08-04 19:53 [Bug c++/101780] New: Missing initializers whereas structure has default initializers deco33000 at yandex dot com
  2021-08-05 11:39 ` [Bug c++/101780] " redi at gcc dot gnu.org
@ 2021-08-05 14:17 ` deco33000 at yandex dot com
  2021-08-05 16:00 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: deco33000 at yandex dot com @ 2021-08-05 14:17 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from KL <deco33000 at yandex dot com> ---
(In reply to Jonathan Wakely from comment #1)

Thanks,

I thought that this warning is fine if it helps to assure the developer that
everything is still under control: that if the analyzer sees that a field is
left uninitialized (with no default value), it should tell it in this warning.


> How would P,Q,R be left uninitialized?

Only if the analyzer has the info that, at this stage, there no default value
for the field. 
It could be interesting in the context of a partially initialized struct (by
mistake, intended by the developer?)?

It help with quality of code.

> It doesn't say anything about undefined behaviour.

In case the developer forgot to properly initialize a member, it *can* lead to
undefined behavior. So, it is good from the compiler to warn about it as well.

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

* [Bug c++/101780] Missing initializers whereas structure has default initializers
  2021-08-04 19:53 [Bug c++/101780] New: Missing initializers whereas structure has default initializers deco33000 at yandex dot com
  2021-08-05 11:39 ` [Bug c++/101780] " redi at gcc dot gnu.org
  2021-08-05 14:17 ` deco33000 at yandex dot com
@ 2021-08-05 16:00 ` redi at gcc dot gnu.org
  2021-08-06  5:01 ` deco33000 at yandex dot com
  2023-05-04 16:11 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2021-08-05 16:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to KL from comment #2)
> > How would P,Q,R be left uninitialized?
> 
> Only if the analyzer has the info that, at this stage, there no default
> value for the field. 
> It could be interesting in the context of a partially initialized struct (by
> mistake, intended by the developer?)?

But if you initialize it with = {.a = 45}; or anything similar, there are no
uninitialized members. The members without an explicit initializer are
value-initialized.

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

* [Bug c++/101780] Missing initializers whereas structure has default initializers
  2021-08-04 19:53 [Bug c++/101780] New: Missing initializers whereas structure has default initializers deco33000 at yandex dot com
                   ` (2 preceding siblings ...)
  2021-08-05 16:00 ` redi at gcc dot gnu.org
@ 2021-08-06  5:01 ` deco33000 at yandex dot com
  2023-05-04 16:11 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: deco33000 at yandex dot com @ 2021-08-06  5:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from KL <deco33000 at yandex dot com> ---
I get it but, as is, with no more contextual information, it is an annoying
warning.

Not enough information for the developer is the real problem here.

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

* [Bug c++/101780] Missing initializers whereas structure has default initializers
  2021-08-04 19:53 [Bug c++/101780] New: Missing initializers whereas structure has default initializers deco33000 at yandex dot com
                   ` (3 preceding siblings ...)
  2021-08-06  5:01 ` deco33000 at yandex dot com
@ 2023-05-04 16:11 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-04 16:11 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=96868

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Related to PR 96868.

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

end of thread, other threads:[~2023-05-04 16:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-04 19:53 [Bug c++/101780] New: Missing initializers whereas structure has default initializers deco33000 at yandex dot com
2021-08-05 11:39 ` [Bug c++/101780] " redi at gcc dot gnu.org
2021-08-05 14:17 ` deco33000 at yandex dot com
2021-08-05 16:00 ` redi at gcc dot gnu.org
2021-08-06  5:01 ` deco33000 at yandex dot com
2023-05-04 16:11 ` pinskia 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).