c++/7387: gcc 3.1 accepts if (int x = f()) {int x;}

dobrynin@bigfoot.com dobrynin@bigfoot.com
Tue Jul 23 13:01:00 GMT 2002

>Number:         7387
>Category:       c++
>Synopsis:       gcc 3.1 accepts   if (int x = f()) {int x;}
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          accepts-illegal
>Submitter-Id:   net
>Arrival-Date:   Tue Jul 23 11:56:00 PDT 2002
>Originator:     Nickolai Dobrynin, University of WI-Milwaukee
>Release:        gcc version 3.1.1 20020714 (prerelease)
1) Cygwin, Windows 2000, Pentium III:

Reading specs from /usr/lib/gcc-lib/i686-pc-cygwin/3.1.1/specs
Configured with: /netrel/src/gcc-3.1.1-3/configure --enable-languages=c,c++,f77,
java --enable-libgcj --enable-threads=posix --with-system-zlib --enable-nls --wi
thout-included-gettext --disable-shared --enable-interpreter --disable-sjlj-exce
ptions --disable-version-specific-runtime-libs --build=i686-pc-linux --host=i686
-pc-cygwin --target=i686-pc-cygwin --enable-haifa --prefix=/usr --exec-prefix=/u
sr --sysconfdir=/etc --libdir=/usr/lib --includedir=/nonexistent/include --libex
Thread model: posix
gcc version 3.1.1 20020714 (prerelease)


2) Also Mingw on Windows 2000, Pentium III:

Reading specs from C:/progra~1/gcc3.1/bin/../lib/gcc-lib/mingw32/3.1/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as --host=
mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable
-languages=f77,c++,objc,ada --disable-win32-registry --disable-shared
Thread model: win32
gcc version 3.1
In the following code,

   if (int x = f()) {
     int x; //erroneously accepted

"int x = f()" introduces a new variable 'x'. According to
[6.4], "if the name is re-declared in the outermost block
of a substatement controlled by the condition, the
declaration that re-declares the name is ill-formed".

$ g++ -Wall 6_4bug.cpp
gccbug.cpp: In function `int main()':
gccbug.cpp:6: warning: unused variable `int x'
gccbug.cpp:9: warning: unused variable `int x'
gccbug.cpp:5: warning: unused variable `int x'
int f();

int main()
   if (int x = f()) {
     int x;   // ill-formed, redeclaration of 'x'
   else {
     int x;   // ill-formed, redeclaration of 'x'

   return 0;

int f() {return 0;}

// Compile with  g++ -Wall 6_4bug.cpp

Content-Type: application/octet-stream; name="6_4bug.cpp"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="6_4bug.cpp"


More information about the Gcc-bugs mailing list