Confusing error for incorrect struct initialization

Mason mpeg.blue@free.fr
Sat Jun 18 21:58:00 GMT 2016


On 18/06/2016 23:14, Martin Sebor wrote:

> On 06/18/2016 10:25 AM, Mason wrote:
>
>> The bug is a request to improve an existing error message.
>>
>> struct xxx { void *p; int a,b,c,d; double x,y; };
>> struct foo { struct xxx *p; int e,f,g; };
>> static struct xxx s = { 0 };
>> struct foo bar = { s }; /*** should be &s ***/
>>
>> $ gcc-6 -c qoi.c
>> qoi.c:4:20: error: initializer element is not constant
>>   struct foo bar = { s }; /*** should be &s ***/
>>                      ^
>> qoi.c:4:20: note: (near initialization for 'bar.p')
>>
>>
>> Do you disagree that this is a confusing error message?
>>
>> Isn't this one better:
>>
>>    incompatible types when initializing type 'struct xxx *' using type 'struct xxx'
> 
> I agree that in this case the error is misleading because even
> if s could somehow be made a constant expression (it can't be
> because its type isn't one of those required of constant
> expressions in initializers) it would not be a valid initializer
> for a pointer.  It seems straightforward to check that the
> initializer type is compatible first, before checking its
> constness, and issue the clearer error message instead. Let
> me finish the patch I prototyped for it and post it for review.

I'm glad to hear that.

> PS Note that this case is different from the one submitted in
> bug 71552 and my patch does not change the error GCC issues
> for it for the reasons explained there.  I.e., initializing
> a pointer with an integer is valid in C and cannot very well
> be rejected.

I think I now understand what you meant.

This code cannot be rejected:

struct foo { const int *p; };
struct foo bar = { 42 };

and the (functionally equivalent) code I originally posted

struct foo { const int *p; };
static const int i = 42;
struct foo bar = { i };

is rejected only because i is not constant per C's rules,
and not because it has an incompatible type.

Regards.



More information about the Gcc-help mailing list