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

            Bug ID: 98826
           Summary: [gcc vs g++] qualified type of members of anonymous
                    struct
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ndesaulniers at google dot com
                CC: richard-gccbugzilla at metafoo dot co.uk
  Target Milestone: ---

Consider the following code:

struct dummy {
  const struct {
    int a;
    volatile struct { int b; };
  };
  int c;
};

extern void fn(const int *);
extern void fn2(volatile int *);

void test(struct dummy *a) {
  fn(&a->b);
  fn2(&a->b);
  a->a = a->c;
}

I noticed that g++ does not produce any diagnostics for the above, but gcc will
produce 3 diagnostics:

bar.c: In function ‘test’:
bar.c:13:6: warning: passing argument 1 of ‘fn’ discards ‘volatile’ qualifier
from pointer target type [-Wdiscarded-qualifiers]
   13 |   fn(&a->b);
      |      ^~~~~
bar.c:9:16: note: expected ‘const int *’ but argument is of type ‘const
volatile int *’
    9 | extern void fn(const int *);
      |                ^~~~~~~~~~~
bar.c:14:7: warning: passing argument 1 of ‘fn2’ discards ‘const’ qualifier
from pointer target type [-Wdiscarded-qualifiers]
   14 |   fn2(&a->b);
      |       ^~~~~
bar.c:10:17: note: expected ‘volatile int *’ but argument is of type ‘const
volatile int *’
   10 | extern void fn2(volatile int *);
      |                 ^~~~~~~~~~~~~~
bar.c:15:8: error: assignment of member ‘a’ in read-only object
   15 |   a->a = a->c;
      |        ^

So it seems that the members of the anonymous struct are inheriting the
qualifications of the containing anonymous struct? This also seems to apply
recursively for nested anonymous structs.

Is this an intentional difference between C and C++?
  • [Bug c/98826] New: [gcc vs g++... ndesaulniers at google dot com via Gcc-bugs

Reply via email to