llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.48k stars 11.77k forks source link

statement-expressions permitted inside block scope type definitions (GCC rejects) #41557

Open llvmbot opened 5 years ago

llvmbot commented 5 years ago
Bugzilla Link 42212
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @DougGregor,@zygoloid

Extended Description

My clang is 9.0.0, and the code is:

void f() { union { typeof ( ( { unsigned long ptr; (int *)(0); } ) ) val; }; }

clang accepts the code, but icc, gcc and msvc all reject it.

msvc:

(5): error C2059: syntax error: '('

(6): error C2143: syntax error: missing ')' before '{'

(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

(9): warning C4183: 'typeof': missing return type; assumed to be a member function returning 'int'

(10): error C2059: syntax error: ')'

(11): error C2059: syntax error: ')'

(12): error C2238: unexpected token(s) preceding ';'

(13): error C2627: 'typeof': member function not allowed in anonymous union

Compiler returned: 2

icc:

(7): internal error: bad pointer

unsigned long __ptr;

^

compilation aborted for (code 4)

Compiler returned: 4

gcc:

x86-64 gcc (trunk) (Editor #​1, Compiler #​1) C++

x86-64 gcc (trunk) 1

No Results x86-64 gcc (trunk) - cached #​1 with x86-64 gcc (trunk) : In function 'void f()': :5:2: error: statement-expressions are not allowed outside functions nor in template-argument lists 5 | ( | ^ Compiler returned: 1
llvmbot commented 5 years ago

Many thanks for your kind explanation. I will submit the problem to gcc.

msvc:

This code uses multiple GCC extensions. There's no reason to expect MSVC to accept it.

icc:

(7): internal error: bad pointer

It's not our bug if a different compiler crashes. :)

gcc:

: In function 'void f()':

:5:2: error: statement-expressions are not allowed outside functions nor in template-argument lists

This diagnostic is clearly wrong. The statement-expression here is inside a function and not inside a template-argument-list.

It's possible that GCC's behavior is the intended one (statement expressions are a GCC extension, after all), but given that GCC's diagnostic describes Clang's behavior, and not GCC's, there is clearly a bug in GCC somewhere (either in its diagnostic text or in its enforcement of the statement expression rules). (This example also doesn't appeasr to violate any of the rules in https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html)

The best thing to do would be to ask the GCC folks whether they intend to allow statement-expressions inside function-local type definitions or not.

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 5 years ago

msvc:

This code uses multiple GCC extensions. There's no reason to expect MSVC to accept it.

icc:

(7): internal error: bad pointer

It's not our bug if a different compiler crashes. :)

gcc:

: In function 'void f()':

:5:2: error: statement-expressions are not allowed outside functions nor in template-argument lists

This diagnostic is clearly wrong. The statement-expression here is inside a function and not inside a template-argument-list.

It's possible that GCC's behavior is the intended one (statement expressions are a GCC extension, after all), but given that GCC's diagnostic describes Clang's behavior, and not GCC's, there is clearly a bug in GCC somewhere (either in its diagnostic text or in its enforcement of the statement expression rules). (This example also doesn't appeasr to violate any of the rules in https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html)

The best thing to do would be to ask the GCC folks whether they intend to allow statement-expressions inside function-local type definitions or not.