Clang accepts parenthesized aggregate initializer for comma expression
What code / commands /steps will reproduce the problem? Compile the below sample code.
struct { char a[100]; char b[100]; int c; } explodes[] = { ("", "", 1) };int main(void) { return 0; }
Compiler Explorer link: https://godbolt.org/z/x4rbjKPG1
What is the expected result? Compilation should fail.
What happens instead? The initializer uses parentheses ("", "", 1), so it is parsed as a single comma-expression of type int (value 1), not as a brace-enclosed initializer for the struct. As per the standard:
A declarator can specify an initial value for the identifier being declared. The identifier designates a variable being initialized. The process of initialization described in the remainder of 11.6 applies also to initializations specified by other syntactic contexts, such as the initialization of function parameters (8.2.2) or the initialization of return values (9.6.3). initializer: brace-or-equal-initializer ( expression-list ) brace-or-equal-initializer: = initializer-clause braced-init-list initializer-clause: assignment-expression braced-init-list initializer-list: initializer-clause ...opt initializer-list , initializer-clause ...opt braced-init-list: { initializer-list ,opt } {} expr-or-braced-init-list: expression braced-init-list
GCC fails compilation.
Clang just emits warning.
@llvm/issue-subscribers-clang-frontend
Author: Vrukesh Panse (vrukesh)
struct { char a[100]; char b[100]; int c; } explodes[] = { ("", "", 1) };int main(void) { return 0; }
Compiler Explorer link: https://godbolt.org/z/x4rbjKPG1
What is the expected result? Compilation should fail.
What happens instead? The initializer uses parentheses ("", "", 1), so it is parsed as a single comma-expression of type int (value 1), not as a brace-enclosed initializer for the struct. As per the standard:
A declarator can specify an initial value for the identifier being declared. The identifier designates a variable being initialized. The process of initialization described in the remainder of 11.6 applies also to initializations specified by other syntactic contexts, such as the initialization of function parameters (8.2.2) or the initialization of return values (9.6.3). initializer: brace-or-equal-initializer ( expression-list ) brace-or-equal-initializer: = initializer-clause braced-init-list initializer-clause: assignment-expression braced-init-list initializer-list: initializer-clause ...opt initializer-list , initializer-clause ...opt braced-init-list: { initializer-list ,opt } {} expr-or-braced-init-list: expression braced-init-list
GCC fails compilation. <source>:6:5: error: initializer element is not constant 6 | ("", "", 1) | ^ <source>:6:5: note: (near initialization for 'explodes[0].a[0]') Compiler returned: 1
Clang just emits warning. <source>:6:6: warning: left operand of comma operator has no effect [-Wunused-value] 6 | ("", "", 1) | ^~ <source>:6:10: warning: left operand of comma operator has no effect [-Wunused-value] 6 | ("", "", 1) | ^~ 2 warnings generated. ASM generation compiler returned: 0 <source>:6:6: warning: left operand of comma operator has no effect [-Wunused-value] 6 | ("", "", 1) | ^~ <source>:6:10: warning: left operand of comma operator has no effect [-Wunused-value] 6 | ("", "", 1) | ^~ 2 warnings generated. Execution build compiler returned: 0 Program returned: 0
I don't think Clang is parsing anything incorrectly. ("", "", 1) is being parsed as a single expression.
The question is only whether or not the expression is a constant expression as required for static storage duration object initialization.
I don't think it is required to be a constant expression because of the use of a comma operator, but implementations are free to permit additional expressions as constant expressions.
For example Clang also permits at file scope
int x = ("", 1);
while GCC does not, but I don't think either compiler is non-conforming.
Closing because this is behaving by design and is not a bug; C allows implementations to extend what is or is not a constant expression (C23 6.6p14) and Clang treats that as a constant expression because none of the operands to the comma expression have side effects. If one of the operands does have side effects, we emit the diagnostic about it not being a constant initializer (https://godbolt.org/z/8Gj4j9xrh).