Bug 71939 - sole flexible array member in an anonymous structure rejected
Summary: sole flexible array member in an anonymous structure rejected
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-07-20 02:55 UTC by Martin Sebor
Modified: 2016-07-29 15:37 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2016-07-20 02:55:57 UTC
While testing a fix for bug 71912 and comparing the C++ front end results to those of the C front end I came across the following test case that's accepted in C++ but rejected in C.  By my reading of C11 the test case is valid because "members of an anonymous structure or union are considered to be members of the containing structure or union" and so struct S should be treated as if it had been defined as:

  struct S {
    int i;
    int a[];
  };

I note that both Clang and EDG eccp 4.11 also reject the code in C, Clang accepts in C++ mode with -Wflexible-array-extensions.

$ cat xyz.c && /build/gcc-71912/gcc/xgcc -B /build/gcc-71912/gcc -Wall -Wextra -Wpedantic xyz.c
struct S {
  int i;
  struct { int a[]; };
};
xyz.c:3:16: error: flexible array member in otherwise empty struct
   struct { int a[]; };
                ^
Comment 1 joseph@codesourcery.com 2016-07-26 21:28:11 UTC
On Wed, 20 Jul 2016, msebor at gcc dot gnu.org wrote:

> While testing a fix for bug 71912 and comparing the C++ front end results to
> those of the C front end I came across the following test case that's accepted
> in C++ but rejected in C.  By my reading of C11 the test case is valid because
> "members of an anonymous structure or union are considered to be members of the
> containing structure or union" and so struct S should be treated as if it had
> been defined as:

I think the anonymous structures/unions feature is and always has been 
intended to be (in the MS and Plan 9 extensions from which the feature 
came, as well as in C++ and C11) purely a matter of a shorthand for naming 
members of a nested structure or union.  So the nested anonymous structure 
or union is subject to all the constraints applicable to one that is not 
anonymous or not nested inside another structure or union (the case in 
point here), and layout is exactly as if all those structures or unions 
were given names (the point that apparently confused some people enough to 
result in the filing of DR#499).  I.e., there is no bug here.
Comment 2 Marek Polacek 2016-07-28 17:27:46 UTC
Closing thus.
Comment 3 Martin Sebor 2016-07-29 15:37:49 UTC
For the record, I brought this up on the C committee's mailing list for discussion.  Since the standard can be read (at least) two different ways here I'll submit a proposal into the next mailing to clarify the intent.

I've also tested IBM XLC, Intel ICC, and Sun c99, and they all reject the code like GCC does, so unless some new arguments in favor of the other interpretation surface I'll go with Joseph's interpretation.

Unfortunately, since G++ has implemented the more permissive rule (as has Clang and ICC in C++ modes, for compatibility with G++), I think it needs to be kept the way it is to avoid breaking code that relies on it.