public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106150] New: Incorrect error for defaulted anonymous union member
@ 2022-06-30 18:45 jens.maurer at gmx dot net
  2022-07-01  1:40 ` [Bug c++/106150] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: jens.maurer at gmx dot net @ 2022-06-30 18:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106150
           Summary: Incorrect error for defaulted anonymous union member
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jens.maurer at gmx dot net
  Target Milestone: ---

The following is valid, but yields an error with gcc 12.1:

struct A {
   A() { }
};

struct V {
   V() { };
   ~V() { }
};


struct S {
   S();
   ~S() {}

   union {
     A a = {};
     V v;
   };
};

S::S() = default;


x.cc:22:1: note: ‘S::S()’ is implicitly deleted because the default definition
would be ill-formed:
   22 | S::S() = default;
      | ^
x.cc:18:8: error: union member ‘S::<unnamed union>::v’ with non-trivial
‘V::V()’
   18 |      V v;
      |        ^


Replacing "= default" with "{}" makes the error go away.

None of the items in [class.default.ctor] p2 applies, in particular p2.2 does
not apply because S::a has a default member initializer.

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

* [Bug c++/106150] Incorrect error for defaulted anonymous union member
  2022-06-30 18:45 [Bug c++/106150] New: Incorrect error for defaulted anonymous union member jens.maurer at gmx dot net
@ 2022-07-01  1:40 ` pinskia at gcc dot gnu.org
  2022-07-01  1:43 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-01  1:40 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
clang also rejects it:
<source>:21:4: error: defaulting this default constructor would delete it after
its first declaration
S::S() = default;
   ^
<source>:17:8: note: default constructor of 'S' is implicitly deleted because
variant field 'v' has a non-trivial default constructor
     V v;
       ^

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

* [Bug c++/106150] Incorrect error for defaulted anonymous union member
  2022-06-30 18:45 [Bug c++/106150] New: Incorrect error for defaulted anonymous union member jens.maurer at gmx dot net
  2022-07-01  1:40 ` [Bug c++/106150] " pinskia at gcc dot gnu.org
@ 2022-07-01  1:43 ` pinskia at gcc dot gnu.org
  2022-07-01  1:56 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-01  1:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
MSVC and ICC accept it though.

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

* [Bug c++/106150] Incorrect error for defaulted anonymous union member
  2022-06-30 18:45 [Bug c++/106150] New: Incorrect error for defaulted anonymous union member jens.maurer at gmx dot net
  2022-07-01  1:40 ` [Bug c++/106150] " pinskia at gcc dot gnu.org
  2022-07-01  1:43 ` pinskia at gcc dot gnu.org
@ 2022-07-01  1:56 ` pinskia at gcc dot gnu.org
  2022-07-01  1:58 ` [Bug c++/106150] [DR 2084] union with more than one variant and non-trivial constructor is not accepted pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-01  1:56 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>The following is valid,

No the code is invalid but because of the non-trival deconstructor of V.
Here is an example which is valid (after
https://cplusplus.github.io/CWG/issues/2084.html):
struct S1 {
    S1();
};
struct S {
    S();
};
union U {
    S s{};
    S1 s1;
} u;

The check in GCC for this seems to be off, if only the variant s is there, GCC
(and clang) accepts it.

So the full check for the defect report was never really done (and it was not
even mentioned in the defect report commentary either).

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

* [Bug c++/106150] [DR 2084] union with more than one variant and non-trivial constructor is not accepted
  2022-06-30 18:45 [Bug c++/106150] New: Incorrect error for defaulted anonymous union member jens.maurer at gmx dot net
                   ` (2 preceding siblings ...)
  2022-07-01  1:56 ` pinskia at gcc dot gnu.org
@ 2022-07-01  1:58 ` pinskia at gcc dot gnu.org
  2022-07-01  2:05 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-01  1:58 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Incorrect error for         |[DR 2084] union with more
                   |defaulted anonymous union   |than one variant and
                   |member                      |non-trivial constructor is
                   |                            |not accepted
           Keywords|                            |rejects-valid

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Let me also file the other bug about the deconstructor for anonymous unions
since that is a different issue.

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

* [Bug c++/106150] [DR 2084] union with more than one variant and non-trivial constructor is not accepted
  2022-06-30 18:45 [Bug c++/106150] New: Incorrect error for defaulted anonymous union member jens.maurer at gmx dot net
                   ` (3 preceding siblings ...)
  2022-07-01  1:58 ` [Bug c++/106150] [DR 2084] union with more than one variant and non-trivial constructor is not accepted pinskia at gcc dot gnu.org
@ 2022-07-01  2:05 ` pinskia at gcc dot gnu.org
  2022-07-01  6:20 ` jens.maurer at gmx dot net
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-01  2:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #4)
> Let me also file the other bug about the deconstructor for anonymous unions
> since that is a different issue.

Actually I take that back, the anonymous union case for the deconstructor is
not invalid in the end; I just misunderstood.
Anyways the reduced testcase shows the problem even without the deconstructor.

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

* [Bug c++/106150] [DR 2084] union with more than one variant and non-trivial constructor is not accepted
  2022-06-30 18:45 [Bug c++/106150] New: Incorrect error for defaulted anonymous union member jens.maurer at gmx dot net
                   ` (4 preceding siblings ...)
  2022-07-01  2:05 ` pinskia at gcc dot gnu.org
@ 2022-07-01  6:20 ` jens.maurer at gmx dot net
  2022-07-20 21:31 ` redi at gcc dot gnu.org
  2022-07-22  7:23 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jens.maurer at gmx dot net @ 2022-07-01  6:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jens Maurer <jens.maurer at gmx dot net> ---
Related clang bug: https://github.com/llvm/llvm-project/issues/56313

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

* [Bug c++/106150] [DR 2084] union with more than one variant and non-trivial constructor is not accepted
  2022-06-30 18:45 [Bug c++/106150] New: Incorrect error for defaulted anonymous union member jens.maurer at gmx dot net
                   ` (5 preceding siblings ...)
  2022-07-01  6:20 ` jens.maurer at gmx dot net
@ 2022-07-20 21:31 ` redi at gcc dot gnu.org
  2022-07-22  7:23 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2022-07-20 21:31 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #3)
> Here is an example which is valid (after
> https://cplusplus.github.io/CWG/issues/2084.html):
> struct S1 {
>     S1();
> };
> struct S {
>     S();
> };
> union U {
>     S s{};
>     S1 s1;
> } u;
> 
> The check in GCC for this seems to be off, if only the variant s is there,
> GCC (and clang) accepts it.
> 
> So the full check for the defect report was never really done (and it was
> not even mentioned in the defect report commentary either).

Yes, all of gcc, clang, edg and msvc reject cases like this.

It should not matter that S1 does not have a trivial default ctor, because the
default member initializer should make this equivalent to:

union U {
  S s;
  S1 s1;
  U() : s() { }
};

Having to write a user-provided constructor is annoying, because to do it
"right" in a generic std::lib template requires:

  constexpr U() noexcept(is_nothrow_default_constructible_v<S>) requires
default_initializable<S> { }

But that should be approximately how the defaulted default ctor is defined
automatically, without all that verbosity.

This is a dup of PR 98423, where Jakub pointed out the code that needs to
change, and what needs to happen.

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

* [Bug c++/106150] [DR 2084] union with more than one variant and non-trivial constructor is not accepted
  2022-06-30 18:45 [Bug c++/106150] New: Incorrect error for defaulted anonymous union member jens.maurer at gmx dot net
                   ` (6 preceding siblings ...)
  2022-07-20 21:31 ` redi at gcc dot gnu.org
@ 2022-07-22  7:23 ` redi at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: redi at gcc dot gnu.org @ 2022-07-22  7:23 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
   Target Milestone|---                         |13.0
             Status|NEW                         |RESOLVED

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed by r13-1783-gdf118d7ba138cacb17203d4a1b5f27730347cc77 for PR 98423.

*** This bug has been marked as a duplicate of bug 98423 ***

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

end of thread, other threads:[~2022-07-22  7:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-30 18:45 [Bug c++/106150] New: Incorrect error for defaulted anonymous union member jens.maurer at gmx dot net
2022-07-01  1:40 ` [Bug c++/106150] " pinskia at gcc dot gnu.org
2022-07-01  1:43 ` pinskia at gcc dot gnu.org
2022-07-01  1:56 ` pinskia at gcc dot gnu.org
2022-07-01  1:58 ` [Bug c++/106150] [DR 2084] union with more than one variant and non-trivial constructor is not accepted pinskia at gcc dot gnu.org
2022-07-01  2:05 ` pinskia at gcc dot gnu.org
2022-07-01  6:20 ` jens.maurer at gmx dot net
2022-07-20 21:31 ` redi at gcc dot gnu.org
2022-07-22  7:23 ` 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).