This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Confusing error for incorrect struct initialization
- From: Mason <mpeg dot blue at free dot fr>
- To: Martin Sebor <msebor at gmail dot com>, lh mouse <lh_mouse at 126 dot com>
- Cc: GCC help <gcc-help at gcc dot gnu dot org>
- Date: Sat, 18 Jun 2016 23:58:39 +0200
- Subject: Re: Confusing error for incorrect struct initialization
- Authentication-results: sourceware.org; auth=none
- References: <5764F614 dot 7060108 at free dot fr> <7cebec62 dot 21aed9 dot 155627c508f dot Coremail dot lh_mouse at 126 dot com> <5765760A dot 1050200 at free dot fr> <5765B9B6 dot 70204 at gmail dot com>
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.