Bug 7508 - GCC refuses to compile a declartion right after a 'case' in switch
Summary: GCC refuses to compile a declartion right after a 'case' in switch
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.1.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
: 21689 23365 23895 25779 26742 (view as bug list)
Depends on:
Blocks:
 
Reported: 2002-08-06 09:36 UTC by ddcc
Modified: 2006-03-18 15:18 UTC (History)
6 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description ddcc 2002-08-06 09:36:02 UTC
GCC refuses to compile a declaration that follows immediately after a 'case' in a 'switch', even in C99 mode:

int main()
	{
	int x = 1;
	switch (x) { case 1: int y = 7; }
	return 0;
	}

Says GCC: In function `main': parse error before "int".

However, if I add a dummy statement before the declaration, GCC compiles it fine:

int main()
	{
	int x = 1;
	switch (x) { case 1: 4 + 5; int y = 7; }
	return 0;
	}

Release:
GCC 3.1.1

Environment:
Seen on MinGW, but probably problem on all platforms

How-To-Repeat:
Compile the first example in the report.
Comment 1 Joseph S. Myers 2002-08-06 19:53:58 UTC
From: "Joseph S. Myers" <jsm28@cam.ac.uk>
To: <ddcc@email.com>
Cc: <gcc-gnats@gcc.gnu.org>,  <gcc-bugs@gcc.gnu.org>
Subject: Re: c/7508: GCC refuses to compile a declartion right after a 'case'
 in switch
Date: Tue, 6 Aug 2002 19:53:58 +0100 (BST)

 On 6 Aug 2002 ddcc@email.com wrote:
 
 > GCC refuses to compile a declaration that follows immediately after a
 > 'case' in a 'switch', even in C99 mode:
 
 Although C99 allows declarations and statements to be mixed, it does not
 make declarations into statements.  Labels can only be applied to
 statements, not to declarations.  As to allowing this as an extension, see
 the comment in c-parse.in explaining why we don't:
 
    NOTE: we don't allow labels on declarations; this might seem like a
    natural extension, but there would be a conflict between attributes
    on the label and prefix attributes on the declaration.  */
 
 -- 
 Joseph S. Myers
 jsm28@cam.ac.uk
 
Comment 2 Joseph S. Myers 2002-09-15 13:31:31 UTC
State-Changed-From-To: open->closed
State-Changed-Why: As explained, we deliberately do not implement this extension.
Comment 3 Joseph S. Myers 2005-08-12 20:52:09 UTC
Reopening to mark as INVALID.
Comment 4 Joseph S. Myers 2005-08-12 20:52:40 UTC
Marked as INVALID.
Comment 5 Joseph S. Myers 2005-08-12 20:54:24 UTC
*** Bug 23365 has been marked as a duplicate of this bug. ***
Comment 6 Andrew Pinski 2005-09-15 14:22:52 UTC
*** Bug 23895 has been marked as a duplicate of this bug. ***
Comment 7 Andrew Pinski 2005-09-15 14:23:52 UTC
*** Bug 21689 has been marked as a duplicate of this bug. ***
Comment 8 price 2005-09-15 18:44:39 UTC
To get it to compile, use curly brackets after the "case":

  case 1:
    {
        int y = 7;
    }


I would suggest that the existence of several duplicates argue for a clearer
error message.
Comment 9 Andrew Pinski 2006-01-13 03:03:48 UTC
*** Bug 25779 has been marked as a duplicate of this bug. ***
Comment 10 Andrew Pinski 2006-03-18 15:18:00 UTC
*** Bug 26742 has been marked as a duplicate of this bug. ***