public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/37231]  New: GCC does not compile code with label statements that are followed by a declaration
@ 2008-08-25  9:51 colding at 42tools dot com
  2008-08-25 10:44 ` [Bug c/37231] " paolo dot carlini at oracle dot com
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: colding at 42tools dot com @ 2008-08-25  9:51 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1373 bytes --]

Hi,

The following simple code does not build using GCC 3.3 or 4.x:

###### main.c ########
int
main(int argc, char *argv[])
{
        goto out;
out:
        int retv = 0;
        return retv;
}
#######################

GCC tells me:

####### GCC output ########
main.c: In function ‘main’:
main.c:7: error: syntax error before ‘int’
main.c:8: error: ‘retv’ undeclared (first use in this function)
main.c:8: error: (Each undeclared identifier is reported only once
main.c:8: error: for each function it appears in.)
###########################

I'm not aware that the above code should be invalid so I think this is a
compiler bug. Putting a ';' after the label will make the code compile just
fine:

###### working main.c ########
int
main(int argc, char *argv[])
{
        goto out;
out: ;
        int retv = 0;
        return retv;
}
##############################

Best regards,
  jules


-- 
           Summary: GCC does not compile code with label statements that are
                    followed by a declaration
           Product: gcc
           Version: 4.0.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: colding at 42tools dot com
  GCC host triplet: i686-apple-darwin9


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37231


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/37231] GCC does not compile code with label statements that are followed by a declaration
  2008-08-25  9:51 [Bug c/37231] New: GCC does not compile code with label statements that are followed by a declaration colding at 42tools dot com
@ 2008-08-25 10:44 ` paolo dot carlini at oracle dot com
  2008-08-25 18:56 ` manu at gcc dot gnu dot org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: paolo dot carlini at oracle dot com @ 2008-08-25 10:44 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from paolo dot carlini at oracle dot com  2008-08-25 10:43 -------
I think the improved error message in mainline makes clear this is invalid (the
EDG front-end agrees, by the way):

37231.c:6: error: a label can only be part of a statement and a declaration is
not a statement

And the "work-around" is indeed Ok.


-- 

paolo dot carlini at oracle dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37231


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/37231] GCC does not compile code with label statements that are followed by a declaration
  2008-08-25  9:51 [Bug c/37231] New: GCC does not compile code with label statements that are followed by a declaration colding at 42tools dot com
  2008-08-25 10:44 ` [Bug c/37231] " paolo dot carlini at oracle dot com
@ 2008-08-25 18:56 ` manu at gcc dot gnu dot org
  2009-07-08 19:36 ` aapo dot rantalainen at gmail dot com
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-08-25 18:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from manu at gcc dot gnu dot org  2008-08-25 18:55 -------
I think this was fixed already in GCC 4.3.


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37231


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/37231] GCC does not compile code with label statements that are followed by a declaration
  2008-08-25  9:51 [Bug c/37231] New: GCC does not compile code with label statements that are followed by a declaration colding at 42tools dot com
  2008-08-25 10:44 ` [Bug c/37231] " paolo dot carlini at oracle dot com
  2008-08-25 18:56 ` manu at gcc dot gnu dot org
@ 2009-07-08 19:36 ` aapo dot rantalainen at gmail dot com
  2009-07-08 19:43 ` pinskia at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: aapo dot rantalainen at gmail dot com @ 2009-07-08 19:36 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from aapo dot rantalainen at gmail dot com  2009-07-08 19:36 -------
Above code doesn't compile:
int main(int argc, char *argv[])
{
  int a=1;
  switch (a)
    {
    case 1:
    int b=2;
    break;
    }
return 0;
}

Error "a label can only be part of a statement and a declaration is not a
statement"

Same workaround:  add ; before int b=2;

gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.3.3-5ubuntu4'
--with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3
--program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug
--enable-objc-gc --enable-mpfr --with-tune=generic --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4)


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37231


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/37231] GCC does not compile code with label statements that are followed by a declaration
  2008-08-25  9:51 [Bug c/37231] New: GCC does not compile code with label statements that are followed by a declaration colding at 42tools dot com
                   ` (2 preceding siblings ...)
  2009-07-08 19:36 ` aapo dot rantalainen at gmail dot com
@ 2009-07-08 19:43 ` pinskia at gcc dot gnu dot org
  2009-07-08 21:34 ` manu at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-07-08 19:43 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from pinskia at gcc dot gnu dot org  2009-07-08 19:42 -------
(In reply to comment #3)
> Above code doesn't compile:

Yes it should not be compile.  The error message has been improved to tell you
what the problem is (that is what Manu was saying in his comment #2).


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37231


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/37231] GCC does not compile code with label statements that are followed by a declaration
  2008-08-25  9:51 [Bug c/37231] New: GCC does not compile code with label statements that are followed by a declaration colding at 42tools dot com
                   ` (3 preceding siblings ...)
  2009-07-08 19:43 ` pinskia at gcc dot gnu dot org
@ 2009-07-08 21:34 ` manu at gcc dot gnu dot org
  2009-07-09  9:25 ` manu at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: manu at gcc dot gnu dot org @ 2009-07-08 21:34 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from manu at gcc dot gnu dot org  2009-07-08 21:34 -------
(In reply to comment #4)
> (In reply to comment #3)
> > Above code doesn't compile:
> 
> Yes it should not be compile.  The error message has been improved to tell you
> what the problem is (that is what Manu was saying in his comment #2).

Indeed.

Aapo, what is difficult to understand from the the current error message? I
would like to improve it as much as possible.


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |aapo dot rantalainen at
                   |                            |gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37231


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/37231] GCC does not compile code with label statements that are followed by a declaration
  2008-08-25  9:51 [Bug c/37231] New: GCC does not compile code with label statements that are followed by a declaration colding at 42tools dot com
                   ` (4 preceding siblings ...)
  2009-07-08 21:34 ` manu at gcc dot gnu dot org
@ 2009-07-09  9:25 ` manu at gcc dot gnu dot org
  2010-05-09 10:30 ` alex dot pyattaev at gmail dot com
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: manu at gcc dot gnu dot org @ 2009-07-09  9:25 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from manu at gcc dot gnu dot org  2009-07-09 09:25 -------
Is this mandated to be an error by the standard? Perhaps GCC could just pedwarn
and add the ';' itself, and compile just fine.

Otherwise, I might add a hint:

note: either add ';' or open a block after the label.

Thoughts?


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37231


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/37231] GCC does not compile code with label statements that are followed by a declaration
  2008-08-25  9:51 [Bug c/37231] New: GCC does not compile code with label statements that are followed by a declaration colding at 42tools dot com
                   ` (5 preceding siblings ...)
  2009-07-09  9:25 ` manu at gcc dot gnu dot org
@ 2010-05-09 10:30 ` alex dot pyattaev at gmail dot com
  2010-05-09 15:27 ` manu at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: alex dot pyattaev at gmail dot com @ 2010-05-09 10:30 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from alex dot pyattaev at gmail dot com  2010-05-09 10:29 -------
I have encountered the bug with switch construct. And the problem is not the
error message, but the fact that it is still compiling just fine this code:
switch (chan_prop.model_type)
        {
                case CM_STATIC:
                        return static_channel_model(src, ntransmitters, dst,
                                                    &chan_prop);
                case CM_FRISS:
                        return friss_channel_model(src, ntransmitters, dst,
                                                   &chan_prop);
                default:
                        return -1;
        }
There should be some consistency at least! I'd like to see this fixed properly.
The compiler should be perfectly predictable, and not like "Sorry, i don't like
this construct here... put a semicolon please".

I'd like this bug to be reopened and FIXED PROPERLY

gcc --version =  (Gentoo 4.4.3-r2 p1.2) 4.4.3


-- 

alex dot pyattaev at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |alex dot pyattaev at gmail
                   |                            |dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37231


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/37231] GCC does not compile code with label statements that are followed by a declaration
  2008-08-25  9:51 [Bug c/37231] New: GCC does not compile code with label statements that are followed by a declaration colding at 42tools dot com
                   ` (6 preceding siblings ...)
  2010-05-09 10:30 ` alex dot pyattaev at gmail dot com
@ 2010-05-09 15:27 ` manu at gcc dot gnu dot org
  2010-05-09 19:07 ` alex dot pyattaev at gmail dot com
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: manu at gcc dot gnu dot org @ 2010-05-09 15:27 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from manu at gcc dot gnu dot org  2010-05-09 15:26 -------
(In reply to comment #7)
> There should be some consistency at least! I'd like to see this fixed properly.
> The compiler should be perfectly predictable, and not like "Sorry, i don't like

It is predictable: a label can only be part of a statement and a declaration is
not a statement.  Now "return x;" is a statement, so it compiles fine.  This is
mandated by the C standard, it is not the compiler's decision. Does it work
different with any other compiler?

Please, tell us what is difficult to understand in the error message and how
could it be better explained.

On the other hand, I don't know whether if GCC adds an empty statement before
the declaration and just emits a warning with -pedantic, then it still complies
with the C standard. I don't have time to investigate this, sorry.


> this construct here... put a semicolon please".
> 
> I'd like this bug to be reopened and FIXED PROPERLY
> 
> gcc --version =  (Gentoo 4.4.3-r2 p1.2) 4.4.3
> 


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37231


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/37231] GCC does not compile code with label statements that are followed by a declaration
  2008-08-25  9:51 [Bug c/37231] New: GCC does not compile code with label statements that are followed by a declaration colding at 42tools dot com
                   ` (7 preceding siblings ...)
  2010-05-09 15:27 ` manu at gcc dot gnu dot org
@ 2010-05-09 19:07 ` alex dot pyattaev at gmail dot com
  2010-05-09 23:25 ` dougsemler at gmail dot com
  2010-05-09 23:29 ` alex dot pyattaev at gmail dot com
  10 siblings, 0 replies; 12+ messages in thread
From: alex dot pyattaev at gmail dot com @ 2010-05-09 19:07 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from alex dot pyattaev at gmail dot com  2010-05-09 19:07 -------
(In reply to comment #7)
> I have encountered the bug with switch construct. And the problem is not the
> error message, but the fact that it is still compiling just fine this code:
> switch (chan_prop.model_type)
>         {
>                 case CM_STATIC:
>                         return static_channel_model(src, ntransmitters, dst,
>                                                     &chan_prop);
>                 case CM_FRISS:
>                         return friss_channel_model(src, ntransmitters, dst,
>                                                    &chan_prop);
>                 default:
>                         return -1;
>         }
> There should be some consistency at least! I'd like to see this fixed properly.
> The compiler should be perfectly predictable, and not like "Sorry, i don't like
> this construct here... put a semicolon please".
> 
> I'd like this bug to be reopened and FIXED PROPERLY
> 
> gcc --version =  (Gentoo 4.4.3-r2 p1.2) 4.4.3
> 

A good example of seemingly normal code is following:
 switch (whatever)
        {
                case 1:
                        int i=0;
                        i++;
                        printf("%d",i);                   
                        break;
        }
It is technically perfectly legal to declare i as a local variable. However,
the compiler issues an error. And it is absolutely not clear why "int i=0;" is
not a statement. That is probably the only really annoying case. Moving
declaration of i into a code block solves the issue. I spent like 10 minutes
trying to find out why perfectly legal code would not compile.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37231


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/37231] GCC does not compile code with label statements that are followed by a declaration
  2008-08-25  9:51 [Bug c/37231] New: GCC does not compile code with label statements that are followed by a declaration colding at 42tools dot com
                   ` (8 preceding siblings ...)
  2010-05-09 19:07 ` alex dot pyattaev at gmail dot com
@ 2010-05-09 23:25 ` dougsemler at gmail dot com
  2010-05-09 23:29 ` alex dot pyattaev at gmail dot com
  10 siblings, 0 replies; 12+ messages in thread
From: dougsemler at gmail dot com @ 2010-05-09 23:25 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from dougsemler at gmail dot com  2010-05-09 23:25 -------
(In reply to comment #9)
> 
> A good example of seemingly normal code is following:
>  switch (whatever)
>         {
>                 case 1:
>                         int i=0;
>                         i++;
>                         printf("%d",i);                   
>                         break;
>         }
> It is technically perfectly legal to declare i as a local variable. However,
> the compiler issues an error. And it is absolutely not clear why "int i=0;" is
> not a statement. That is probably the only really annoying case. Moving
> declaration of i into a code block solves the issue. I spent like 10 minutes
> trying to find out why perfectly legal code would not compile.
> 

Not to beat a dead horse here but...

Because that is *not* perfectly legal C code, and will not be valid C++ code if
you add another case label after it (jump over initialization)?  A declaration
("int i") is not a statement.

That fails on both MSVC and gcc.  In fact, MSVC will still barf even after
adding a semicolon after the label to make it a statement. (MSVC error is
"error 2143: missing ';' before type")


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37231


^ permalink raw reply	[flat|nested] 12+ messages in thread

* [Bug c/37231] GCC does not compile code with label statements that are followed by a declaration
  2008-08-25  9:51 [Bug c/37231] New: GCC does not compile code with label statements that are followed by a declaration colding at 42tools dot com
                   ` (9 preceding siblings ...)
  2010-05-09 23:25 ` dougsemler at gmail dot com
@ 2010-05-09 23:29 ` alex dot pyattaev at gmail dot com
  10 siblings, 0 replies; 12+ messages in thread
From: alex dot pyattaev at gmail dot com @ 2010-05-09 23:29 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #11 from alex dot pyattaev at gmail dot com  2010-05-09 23:29 -------
(In reply to comment #10)
> (In reply to comment #9)
> > 
> > A good example of seemingly normal code is following:
> >  switch (whatever)
> >         {
> >                 case 1:
> >                         int i=0;
> >                         i++;
> >                         printf("%d",i);                   
> >                         break;
> >         }
> > It is technically perfectly legal to declare i as a local variable. However,
> > the compiler issues an error. And it is absolutely not clear why "int i=0;" is
> > not a statement. That is probably the only really annoying case. Moving
> > declaration of i into a code block solves the issue. I spent like 10 minutes
> > trying to find out why perfectly legal code would not compile.
> > 
> 
> Not to beat a dead horse here but...
> 
> Because that is *not* perfectly legal C code, and will not be valid C++ code if
> you add another case label after it (jump over initialization)?  A declaration
> ("int i") is not a statement.
> 
> That fails on both MSVC and gcc.  In fact, MSVC will still barf even after
> adding a semicolon after the label to make it a statement. (MSVC error is
> "error 2143: missing ';' before type")
> 
Ok, lets have pity for dead horse. Let it be as it is=)


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37231


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2010-05-09 23:29 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-08-25  9:51 [Bug c/37231] New: GCC does not compile code with label statements that are followed by a declaration colding at 42tools dot com
2008-08-25 10:44 ` [Bug c/37231] " paolo dot carlini at oracle dot com
2008-08-25 18:56 ` manu at gcc dot gnu dot org
2009-07-08 19:36 ` aapo dot rantalainen at gmail dot com
2009-07-08 19:43 ` pinskia at gcc dot gnu dot org
2009-07-08 21:34 ` manu at gcc dot gnu dot org
2009-07-09  9:25 ` manu at gcc dot gnu dot org
2010-05-09 10:30 ` alex dot pyattaev at gmail dot com
2010-05-09 15:27 ` manu at gcc dot gnu dot org
2010-05-09 19:07 ` alex dot pyattaev at gmail dot com
2010-05-09 23:25 ` dougsemler at gmail dot com
2010-05-09 23:29 ` alex dot pyattaev at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).