public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/41425]  New: switch with enums doesn't work
@ 2009-09-21 12:17 Woebbeking at web dot de
  2009-09-21 12:19 ` [Bug c++/41425] " Woebbeking at web dot de
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: Woebbeking at web dot de @ 2009-09-21 12:17 UTC (permalink / raw)
  To: gcc-bugs

If I cast an int to the enum type all values outside the enum triggers the
first case-statement.

The number of enum values is important. It must be a power of 2.

This works in GCC 4.3.4.


-- 
           Summary: switch with enums doesn't work
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Woebbeking at web dot de


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
@ 2009-09-21 12:19 ` Woebbeking at web dot de
  2009-09-21 12:21 ` Woebbeking at web dot de
                   ` (11 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Woebbeking at web dot de @ 2009-09-21 12:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from Woebbeking at web dot de  2009-09-21 12:19 -------
Created an attachment (id=18622)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=18622&action=view)
testcase


-- 


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
  2009-09-21 12:19 ` [Bug c++/41425] " Woebbeking at web dot de
@ 2009-09-21 12:21 ` Woebbeking at web dot de
  2009-09-21 12:22 ` Woebbeking at web dot de
                   ` (10 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Woebbeking at web dot de @ 2009-09-21 12:21 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from Woebbeking at web dot de  2009-09-21 12:21 -------
g++ case.cpp is sufficient to reproduce this.


-- 


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
  2009-09-21 12:19 ` [Bug c++/41425] " Woebbeking at web dot de
  2009-09-21 12:21 ` Woebbeking at web dot de
@ 2009-09-21 12:22 ` Woebbeking at web dot de
  2009-09-21 12:41 ` paolo dot carlini at oracle dot com
                   ` (9 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Woebbeking at web dot de @ 2009-09-21 12:22 UTC (permalink / raw)
  To: gcc-bugs



-- 

Woebbeking at web dot de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |critical


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
                   ` (2 preceding siblings ...)
  2009-09-21 12:22 ` Woebbeking at web dot de
@ 2009-09-21 12:41 ` paolo dot carlini at oracle dot com
  2009-09-21 12:42 ` paolo dot carlini at oracle dot com
                   ` (8 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: paolo dot carlini at oracle dot com @ 2009-09-21 12:41 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from paolo dot carlini at oracle dot com  2009-09-21 12:41 -------
As far as I can see, you are triggering undefined behavior. Per 5.2.9/7: "A
value of integral or enumeration type can be explicitly converted to an
enumeration type. The value is unchanged if the original value is within the
range of the enumeration values (7.2). Otherwise, the resulting enumeration
value is unspecified."


-- 


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
                   ` (3 preceding siblings ...)
  2009-09-21 12:41 ` paolo dot carlini at oracle dot com
@ 2009-09-21 12:42 ` paolo dot carlini at oracle dot com
  2009-09-21 12:44 ` rguenth at gcc dot gnu dot org
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: paolo dot carlini at oracle dot com @ 2009-09-21 12:42 UTC (permalink / raw)
  To: gcc-bugs



-- 

paolo dot carlini at oracle dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|critical                    |normal


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
                   ` (4 preceding siblings ...)
  2009-09-21 12:42 ` paolo dot carlini at oracle dot com
@ 2009-09-21 12:44 ` rguenth at gcc dot gnu dot org
  2009-09-21 12:54 ` Woebbeking at web dot de
                   ` (6 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2009-09-21 12:44 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from rguenth at gcc dot gnu dot org  2009-09-21 12:44 -------
In C++ an enum type only has the minimum number of bits that is required to
store all its values, thus 1 in your case.  So (foo)5 is a truncation.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |critical
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
                   ` (5 preceding siblings ...)
  2009-09-21 12:44 ` rguenth at gcc dot gnu dot org
@ 2009-09-21 12:54 ` Woebbeking at web dot de
  2009-09-21 13:08 ` rguenth at gcc dot gnu dot org
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: Woebbeking at web dot de @ 2009-09-21 12:54 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from Woebbeking at web dot de  2009-09-21 12:53 -------
Paolo, but std::cout << static_cast<foo>(i); prints 5, so it's not the
conversion but the switch statement which is "broken".

Richard, if it's only truncation shouldn't case B be triggered?


-- 


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
                   ` (6 preceding siblings ...)
  2009-09-21 12:54 ` Woebbeking at web dot de
@ 2009-09-21 13:08 ` rguenth at gcc dot gnu dot org
  2009-09-21 14:25 ` schwab at linux-m68k dot org
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2009-09-21 13:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from rguenth at gcc dot gnu dot org  2009-09-21 13:08 -------
Switch assembly is optimized if you handle all valid cases (which you do) into
if (i != 0)
  case B
else
  case A


-- 


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
                   ` (7 preceding siblings ...)
  2009-09-21 13:08 ` rguenth at gcc dot gnu dot org
@ 2009-09-21 14:25 ` schwab at linux-m68k dot org
  2009-09-21 14:46 ` paolo dot carlini at oracle dot com
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: schwab at linux-m68k dot org @ 2009-09-21 14:25 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from schwab at linux-m68k dot org  2009-09-21 14:25 -------
(In reply to comment #3)
> As far as I can see, you are triggering undefined behavior.

There is a big difference between undefined and unspecified behaviour.  With
unspecified behaviour the implementation must chose a consistent behaviour,
although it does not have to document which one it choses.


-- 


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
                   ` (8 preceding siblings ...)
  2009-09-21 14:25 ` schwab at linux-m68k dot org
@ 2009-09-21 14:46 ` paolo dot carlini at oracle dot com
  2009-09-21 16:46 ` Woebbeking at web dot de
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 14+ messages in thread
From: paolo dot carlini at oracle dot com @ 2009-09-21 14:46 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from paolo dot carlini at oracle dot com  2009-09-21 14:45 -------
Agreed, unspecified, as the actual citation says.


-- 


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
                   ` (9 preceding siblings ...)
  2009-09-21 14:46 ` paolo dot carlini at oracle dot com
@ 2009-09-21 16:46 ` Woebbeking at web dot de
  2009-09-22 11:43 ` rguenth at gcc dot gnu dot org
  2010-05-04  4:48 ` jason at gcc dot gnu dot org
  12 siblings, 0 replies; 14+ messages in thread
From: Woebbeking at web dot de @ 2009-09-21 16:46 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #9 from Woebbeking at web dot de  2009-09-21 16:46 -------
So it's ok to change the behavior in a minor release?


-- 


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
                   ` (10 preceding siblings ...)
  2009-09-21 16:46 ` Woebbeking at web dot de
@ 2009-09-22 11:43 ` rguenth at gcc dot gnu dot org
  2010-05-04  4:48 ` jason at gcc dot gnu dot org
  12 siblings, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2009-09-22 11:43 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #10 from rguenth at gcc dot gnu dot org  2009-09-22 11:43 -------
Yes, as part of a wrong-code fix.


-- 


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


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

* [Bug c++/41425] switch with enums doesn't work
  2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
                   ` (11 preceding siblings ...)
  2009-09-22 11:43 ` rguenth at gcc dot gnu dot org
@ 2010-05-04  4:48 ` jason at gcc dot gnu dot org
  12 siblings, 0 replies; 14+ messages in thread
From: jason at gcc dot gnu dot org @ 2010-05-04  4:48 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #11 from jason at gcc dot gnu dot org  2010-05-04 04:48 -------
In G++ 4.6 the confusing optimization will only be performed with
-fstrict-enums.


-- 

jason at gcc dot gnu dot org changed:

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


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


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

end of thread, other threads:[~2010-05-04  4:48 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-09-21 12:17 [Bug c++/41425] New: switch with enums doesn't work Woebbeking at web dot de
2009-09-21 12:19 ` [Bug c++/41425] " Woebbeking at web dot de
2009-09-21 12:21 ` Woebbeking at web dot de
2009-09-21 12:22 ` Woebbeking at web dot de
2009-09-21 12:41 ` paolo dot carlini at oracle dot com
2009-09-21 12:42 ` paolo dot carlini at oracle dot com
2009-09-21 12:44 ` rguenth at gcc dot gnu dot org
2009-09-21 12:54 ` Woebbeking at web dot de
2009-09-21 13:08 ` rguenth at gcc dot gnu dot org
2009-09-21 14:25 ` schwab at linux-m68k dot org
2009-09-21 14:46 ` paolo dot carlini at oracle dot com
2009-09-21 16:46 ` Woebbeking at web dot de
2009-09-22 11:43 ` rguenth at gcc dot gnu dot org
2010-05-04  4:48 ` jason at gcc dot gnu dot org

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).