On 08/31/17 10:42, Markus Armbruster wrote:
> Marc-André Lureau <marcandre.lur...@redhat.com> writes:
> 
>> Hi
>>
>> ----- Original Message -----
>>> Marc-André Lureau <marcandre.lur...@redhat.com> writes:
>>>
>>>> Hi
>>>>
>>>> ----- Original Message -----
>>>>> Marc-André Lureau <marcandre.lur...@redhat.com> writes:
>>>>>
>>>>>> They are not considered constant expressions in C, producing an error
>>>>>> when compiling a const QLit.
>>>>>
>>>>> A const QLit?  Do you mean a non-const one?
>>>>
>>>> Really a const QLitObject:
>>>>
>>>>
>>>> const QLitObject qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
>>>>              QLIT_QNULL,
>>>>              {}
>>>>          }));
>>>>
>>>> qmp-introspect.c:17:63: error: initializer element is not constant
>>>>   const QLitObject qmp_schema_qlit = QLIT_QLIST(((QLitObject[]) {
>>>>                                                                 ^
>>>> Removing the "compound literals" fixes this error.
>>>
>>> Does QLIT_QLIST(((const QLitObject[]) { ... } work?
>>
>> No. Even if I put "const" all over the place (in member, in compound type 
>> etc).
>>
>> Give it a try, see if you can make it const, I am out of luck.
> 
> The commit message's explanation is wrong.  This isn't about const at
> all, it's about "constant expressions", which are something else
> entirely.
> 
> For what it's worth, clang is cool with the compound literals.  On
> Fedora 26 with a minimized test case (appended):
> 
>     $ clang -c -g -O -Wall compound-lit.c
>     $ gcc -c -g -O -Wall compound-lit.c
>     compound-lit.c:30:37: error: initializer element is not constant
>          .value.qdict = (QLitDictEntry[]){
>                                          ^
>     compound-lit.c:30:37: note: (near initialization for ‘(anonymous).value’)
> 
> GCC bug or not?  A search of the GCC Bugzilla finds
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71713
> 
> Copying a few notorious language lawyers^W^W^Wtrusted advisers.
> 
> Even if this turns out to be a gcc bug, we'll have to work around it.
> But the work-around needs a comment then.
> 
> In any case, the commit message needs fixing.
> 
> 
> 
> enum {
>     QTYPE_NONE, QTYPE_QSTRING, QTYPE_QDICT,
> };
> 
> typedef struct QLitDictEntry QLitDictEntry;
> typedef struct QLitObject QLitObject;
> 
> struct QLitObject {
>     int type;
>     union {
>         const char *qstr;
>         QLitDictEntry *qdict;
>     } value;
> };
> 
> struct QLitDictEntry {
>     const char *key;
>     QLitObject value;
> };
> 
> QLitObject qlit1 = (QLitObject){
>     .type = QTYPE_QDICT,
>     .value.qdict = (QLitDictEntry[]){
>       { "foo", {} },
>       {}
>     }};
> 
> QLitObject qlit2 = (QLitObject){
>     .type = QTYPE_QDICT,
>     .value.qdict = (QLitDictEntry[]){
>       { "foo", (QLitObject){} },
>       {}
>     }};
> 

(1) When discussing standards conformance, please drop the {} construct;
it is a GNUism. Replacing it with { 0 } works in all contexts, and
conforms to the standard. (Not trying to be pedantic here, but it does
elicit extra warnings from my gcc command line

gcc -std=c99 -pedantic -Wall -Wextra -fsyntax-only


(2) Let's see what the standard says:

  6.5.2.5 Compound literals
  Constraints
  3 If the compound literal occurs outside the body of a function, the
    initializer list shall consist of constant expressions.

In the initialization of "qlit1", one element of the initializer list
(namely for .value.qdict) is

[1] (QLitDictEntry[]) {
      { "foo", { 0 } },
      { 0 }
    }

Is this a constant expression?

  6.6 Constant expressions
  7 More latitude is permitted for constant expressions in initializers.
    Such a constant expression shall be, or evaluate to, one of the
    following:
    - an arithmetic constant expression,
    - a null pointer constant,
    - an address constant, or
    - an address constant for an object type plus or minus an integer
      constant expression.

Now, is [1] an address constant?

  6.6 Constant expressions
  9 An address constant is a null pointer, a pointer to an lvalue
    designating an object of static storage duration, or a pointer to a
    function designator; it shall be created explicitly using the unary
    & operator or an integer constant cast to pointer type, or
    implicitly by the use of an expression of array or function type.
    The array-subscript [] and member-access . and -> operators, the
    address & and indirection * unary operators, and pointer casts may
    be used in the creation of an address constant, but the value of an
    object shall not be accessed by use of these operators.

"expression of array [...] type" applies; question is:
- is [1] an lvalue designating an object of static storage duration?

  6.5.2.5 Compound literals
  Semantics
  5 If the type name specifies an array of unknown size, the size is
    determined by the initializer list as specified in 6.7.8, and the
    type of the compound literal is that of the completed array type.
    Otherwise (when the type name specifies an object type), the type
    of the compound literal is that specified by the type name. In
    either case, the result is an lvalue.

So, an lvalue [1] is.

  6 The value of the compound literal is that of an unnamed object
    initialized by the initializer list. If the compound literal occurs
    outside the body of a function, the object has static storage
    duration; otherwise, it has automatic storage duration associated
    with the enclosing block.

Static storage duration is therefore also proven; the initializer [1]
that we provide for ".value.qdict" *is* a constant expression.


(3) However, on my side at least -- RHEL-7.4 --, the initializer for
".value.qdict" is not what gcc complains about, in the initialization of
"qlit1"!

The problem is the *outer* compound literal. *That* is indeed not a
constant expression; if you review 6.6p7 above, it does not fit any of
the allowed cases.

However, the outer compound literal doesn't buy us anything! If you
change the code like this, it compiles without a hitch:

--- xx.c        2017-08-31 17:23:05.145481557 +0200
+++ yy.c        2017-08-31 17:25:14.839088894 +0200
@@ -18,7 +18,7 @@
     QLitObject value;
 };

-QLitObject qlit1 = (QLitObject){
+QLitObject qlit1 = {
     .type = QTYPE_QDICT,
     .value.qdict = (QLitDictEntry[]){
       { "foo", { 0 } },


(I ignored "qlit2" for this discussion.)

Thanks
Laszlo

Reply via email to