Hi,
This turns out to be a strictly conforming C99 program:
#include <stdlib.h>
int main(void) {
const struct {
int array[1];
} s;
// int *from_lvalue = s.array; // #1
int *from_rvalue = (rand() ? s : s).array; // #2
}
#1, if uncommented, should be rejected with -std=c99 -pedantic-errors, and
quite rightly is. However, in #2, the result of the conditional expression is
not an lvalue. C99 6.7.3p3 states that type qualifiers are not meaningful for
non-lvalues. Because of that, the type of (rand() ? s : s).array is int[1], not
const int[1], and it converts to int*, not const int*. This means the
initialization should be allowed, even though there is nothing you are allowed
to do with from_rvalue afterwards. It is certainly true that this code is
complete rubbish, and this handling by gcc should not cause any problems in any
real-world code, but technically, it's a bug, so I'm reporting it here.
The code is rejected in gcc 4.1 (4.1.2), 4.2 (4.2.2) and 4.3 (4.3-20080208). It
used to be accepted in older versions (3.3.6, 3.4.6, 4.0.4), but in those older
versions, no warning was given even for #1. Because of that, I'm not sure if
this qualifies as a regression.
--
Summary: struct/union rvalue qualifiers must not propagate to
members
Product: gcc
Version: 4.3.0
Status: UNCONFIRMED
Severity: minor
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: truedfx at gentoo dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35235