public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/40335]  New: The implement of Switch statment is against with C++ standard
@ 2009-06-04  8:13 shenrfen at gmail dot com
  2009-06-04  9:04 ` [Bug c++/40335] " rguenth at gcc dot gnu dot org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: shenrfen at gmail dot com @ 2009-06-04  8:13 UTC (permalink / raw)
  To: gcc-bugs

Source code: 1.cpp
#include <stdio.h>
static int i;

int
main (void)
{
  i = -1; 
  switch ((signed char) i) {
  case 255:
    printf("255\n");
    break;
  default:
    printf("default\n");
    break;
  }
}

Compiling command  : g++ 1.cpp && ./a.out
result             : 255
The expected result: default  

According to C++ standard, an integral promotion of expression "(signed char)
i" should be performed firstly, the result of control expression should be
0xffff; then the label of case statment will be converted to int-type. So the
expected result should be default in my opinion. 

Thanks very much.

C++ standard:
The condition shall be of integral type, enumeration type, or of a class type
for which a single conversion function to integral or enumeration type exists
(12.3). If the condition is of class type, the condition is converted by
calling that conversion function, and the result of the conversion is used in
place of the original condition for the remainder of this section. 
Integral promotions are performed.
Any statement within the switch statement can be labeled with one or more case
labels as follows:
case constant-expression :
where the constant-expression shall be an integral constant-expression. The
integral constant-expression (5.19) is implicitly converted to the promoted
type of the switch condition.


-- 
           Summary: The implement of Switch statment is against with C++
                    standard
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: critical
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: shenrfen at gmail dot com
 GCC build triplet: x86
  GCC host triplet: x86
GCC target triplet: x86


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


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

* [Bug c++/40335] The implement of Switch statment is against with C++ standard
  2009-06-04  8:13 [Bug c++/40335] New: The implement of Switch statment is against with C++ standard shenrfen at gmail dot com
@ 2009-06-04  9:04 ` rguenth at gcc dot gnu dot org
  2009-06-04  9:09 ` shenrfen at gmail dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2009-06-04  9:04 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from rguenth at gcc dot gnu dot org  2009-06-04 09:04 -------
Integral promotion is performed on the switch argument, thus signed char -1
is sign-extended to int -1.  You probably want (unsigned char) i instead.


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID
            Summary|The implement of Switch     |The implement of Switch
                   |statment is against with C++|statment is against with C++
                   |standard                    |standard


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


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

* [Bug c++/40335] The implement of Switch statment is against with C++ standard
  2009-06-04  8:13 [Bug c++/40335] New: The implement of Switch statment is against with C++ standard shenrfen at gmail dot com
  2009-06-04  9:04 ` [Bug c++/40335] " rguenth at gcc dot gnu dot org
@ 2009-06-04  9:09 ` shenrfen at gmail dot com
  2009-06-04  9:47 ` shenrfen at gmail dot com
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: shenrfen at gmail dot com @ 2009-06-04  9:09 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from shenrfen at gmail dot com  2009-06-04 09:09 -------
The expected result should be -1, not 255.
But the result is 255 when I use g++ to compiling this code.


-- 

shenrfen at gmail dot com changed:

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


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


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

* [Bug c++/40335] The implement of Switch statment is against with C++ standard
  2009-06-04  8:13 [Bug c++/40335] New: The implement of Switch statment is against with C++ standard shenrfen at gmail dot com
  2009-06-04  9:04 ` [Bug c++/40335] " rguenth at gcc dot gnu dot org
  2009-06-04  9:09 ` shenrfen at gmail dot com
@ 2009-06-04  9:47 ` shenrfen at gmail dot com
  2009-06-04 10:05 ` rguenth at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: shenrfen at gmail dot com @ 2009-06-04  9:47 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from shenrfen at gmail dot com  2009-06-04 09:47 -------
I have debug the C++ front-end of gcc3.3.5.
In function finish_switch_cond:

      if (cond != error_mark_node)
        {
          cond = default_conversion (cond);
          cond = fold (build1 (CLEANUP_POINT_EXPR, TREE_TYPE (cond), cond));
        }

SRF: the type of cond is "int"

      if (cond != error_mark_node)
        {
          index = get_unwidened (cond, NULL_TREE);
          /* We can't strip a conversion from a signed type to an unsigned,
             because if we did, int_fits_type_p would do the wrong thing
             when checking case values for being in range,
             and it's too hard to do the right thing.  */
          if (TREE_UNSIGNED (TREE_TYPE (cond))
              == TREE_UNSIGNED (TREE_TYPE (index)))
            cond = index;
        }

SRF: bug the type of cond is back to "signed char" here.....
     Maybe there is something error in function "get_unwidened" 


-- 


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


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

* [Bug c++/40335] The implement of Switch statment is against with C++ standard
  2009-06-04  8:13 [Bug c++/40335] New: The implement of Switch statment is against with C++ standard shenrfen at gmail dot com
                   ` (2 preceding siblings ...)
  2009-06-04  9:47 ` shenrfen at gmail dot com
@ 2009-06-04 10:05 ` rguenth at gcc dot gnu dot org
  2009-06-04 10:24 ` shenrfen at gmail dot com
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2009-06-04 10:05 UTC (permalink / raw)
  To: gcc-bugs

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



------- Comment #4 from rguenth at gcc dot gnu dot org  2009-06-04 10:05 -------
GCC 3.3 is very old and no longer maintained.

> g++-4.4 -o t t.C
t.C: In function ‘int main()’:
t.C:9: warning: case label value exceeds maximum value for type

which seems indeed bogus (but hints at what happens).

The C frontend correctly dispatches to the default case but still warns:

> gcc-4.4 -o t t.c -Wall
t.c: In function ‘main’:
t.c:9: warning: case label value exceeds maximum value for type
t.c:16: warning: control reaches end of non-void function


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
  GCC build triplet|x86                         |
   GCC host triplet|x86                         |
 GCC target triplet|x86                         |
           Keywords|                            |diagnostic, wrong-code
      Known to fail|                            |3.3.6 4.3.3 4.4.0 4.5.0
      Known to work|                            |2.95.4
   Last reconfirmed|0000-00-00 00:00:00         |2009-06-04 10:05:43
               date|                            |
            Version|unknown                     |4.4.0


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


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

* [Bug c++/40335] The implement of Switch statment is against with C++ standard
  2009-06-04  8:13 [Bug c++/40335] New: The implement of Switch statment is against with C++ standard shenrfen at gmail dot com
                   ` (3 preceding siblings ...)
  2009-06-04 10:05 ` rguenth at gcc dot gnu dot org
@ 2009-06-04 10:24 ` shenrfen at gmail dot com
  2009-06-04 10:46 ` paolo dot carlini at oracle dot com
  2009-06-04 11:16 ` rguenth at gcc dot gnu dot org
  6 siblings, 0 replies; 8+ messages in thread
From: shenrfen at gmail dot com @ 2009-06-04 10:24 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from shenrfen at gmail dot com  2009-06-04 10:24 -------
Thanks very much.
Waiting for your patch. the patch of gcc3.3.5 is also expected if you have
enough time to do it. it should be similar with gcc4.**
Thanks agian. 


-- 


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


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

* [Bug c++/40335] The implement of Switch statment is against with C++ standard
  2009-06-04  8:13 [Bug c++/40335] New: The implement of Switch statment is against with C++ standard shenrfen at gmail dot com
                   ` (4 preceding siblings ...)
  2009-06-04 10:24 ` shenrfen at gmail dot com
@ 2009-06-04 10:46 ` paolo dot carlini at oracle dot com
  2009-06-04 11:16 ` rguenth at gcc dot gnu dot org
  6 siblings, 0 replies; 8+ messages in thread
From: paolo dot carlini at oracle dot com @ 2009-06-04 10:46 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from paolo dot carlini at oracle dot com  2009-06-04 10:45 -------
Note that 3_4-branch, 4_0-branch, 4_1-branch, 4_2-branch are all closed.


-- 


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


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

* [Bug c++/40335] The implement of Switch statment is against with C++ standard
  2009-06-04  8:13 [Bug c++/40335] New: The implement of Switch statment is against with C++ standard shenrfen at gmail dot com
                   ` (5 preceding siblings ...)
  2009-06-04 10:46 ` paolo dot carlini at oracle dot com
@ 2009-06-04 11:16 ` rguenth at gcc dot gnu dot org
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu dot org @ 2009-06-04 11:16 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from rguenth at gcc dot gnu dot org  2009-06-04 11:16 -------
Oh, 4.4 and 4.5 already work.

*** This bug has been marked as a duplicate of 39371 ***


-- 

rguenth at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
      Known to fail|3.3.6 4.3.3 4.4.0 4.5.0     |3.3.6 4.3.3
         Resolution|                            |DUPLICATE


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


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

end of thread, other threads:[~2009-06-04 11:16 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-06-04  8:13 [Bug c++/40335] New: The implement of Switch statment is against with C++ standard shenrfen at gmail dot com
2009-06-04  9:04 ` [Bug c++/40335] " rguenth at gcc dot gnu dot org
2009-06-04  9:09 ` shenrfen at gmail dot com
2009-06-04  9:47 ` shenrfen at gmail dot com
2009-06-04 10:05 ` rguenth at gcc dot gnu dot org
2009-06-04 10:24 ` shenrfen at gmail dot com
2009-06-04 10:46 ` paolo dot carlini at oracle dot com
2009-06-04 11:16 ` rguenth 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).