public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/53745] New: Bitshifted value (1 << 31) within enumerator class is calculated incorrectly during compilation.
@ 2012-06-21 13:04 sbgccbug at yahoo dot co.uk
  2012-06-21 13:35 ` [Bug c++/53745] " schwab@linux-m68k.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: sbgccbug at yahoo dot co.uk @ 2012-06-21 13:04 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 53745
           Summary: Bitshifted value (1 << 31) within enumerator class is
                    calculated incorrectly during compilation.
    Classification: Unclassified
           Product: gcc
           Version: 4.6.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: sbgccbug@yahoo.co.uk


When compiling an 'enum class' (-std=c++0x) that uses bit-shifted values, the
result of "1 << 31" is calculated incorrectly, resulting in -0x80000000
(*negative* 2^31) rather than the correct result of 0x80000000:

// *** BEGIN EXAMPLE CODE (main.cpp) ***

enum class EnumTest : unsigned long int
{
   A = 1 << 30,
   B = 1 << 31
};

int main()
{
   return 0;
}

// *** END EXAMPLE CODE ***

// *** BEGIN gcc-4.6.1 OUTPUT ***

main.cpp|4|error: enumerator value -0x00000000080000000 is too large for
underlying type ‘long unsigned int’|

// *** END gcc-4.6.1 OUTPUT ***


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

* [Bug c++/53745] Bitshifted value (1 << 31) within enumerator class is calculated incorrectly during compilation.
  2012-06-21 13:04 [Bug c++/53745] New: Bitshifted value (1 << 31) within enumerator class is calculated incorrectly during compilation sbgccbug at yahoo dot co.uk
@ 2012-06-21 13:35 ` schwab@linux-m68k.org
  2012-06-21 13:36 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: schwab@linux-m68k.org @ 2012-06-21 13:35 UTC (permalink / raw)
  To: gcc-bugs

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

Andreas Schwab <schwab@linux-m68k.org> changed:

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

--- Comment #1 from Andreas Schwab <schwab@linux-m68k.org> 2012-06-21 13:35:26 UTC ---
1<<31 is an expression of type int, and overflows when int has 32 bits.  This
is independent of the context of its use.  You should write 1UL<<31 instead.


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

* [Bug c++/53745] Bitshifted value (1 << 31) within enumerator class is calculated incorrectly during compilation.
  2012-06-21 13:04 [Bug c++/53745] New: Bitshifted value (1 << 31) within enumerator class is calculated incorrectly during compilation sbgccbug at yahoo dot co.uk
  2012-06-21 13:35 ` [Bug c++/53745] " schwab@linux-m68k.org
@ 2012-06-21 13:36 ` redi at gcc dot gnu.org
  2012-06-21 17:52 ` [Bug c++/53745] [C++11] Poor diagnostic for ill-formed narrowing conversion in enumerator initializer redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2012-06-21 13:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-06-21 13:36:24 UTC ---
The initializer must be converted constant expression of type unsigned long, so
by my reading it's ill-formed because (1<<31) has type int and is negative so
requires a narrowing conversion to unsigned long.


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

* [Bug c++/53745] [C++11] Poor diagnostic for ill-formed narrowing conversion in enumerator initializer
  2012-06-21 13:04 [Bug c++/53745] New: Bitshifted value (1 << 31) within enumerator class is calculated incorrectly during compilation sbgccbug at yahoo dot co.uk
  2012-06-21 13:35 ` [Bug c++/53745] " schwab@linux-m68k.org
  2012-06-21 13:36 ` redi at gcc dot gnu.org
@ 2012-06-21 17:52 ` redi at gcc dot gnu.org
  2013-05-03 23:23 ` paolo.carlini at oracle dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2012-06-21 17:52 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
   Last reconfirmed|                            |2012-06-21
         Resolution|INVALID                     |
            Summary|Bitshifted value (1 << 31)  |[C++11] Poor diagnostic for
                   |within enumerator class is  |ill-formed narrowing
                   |calculated incorrectly      |conversion in enumerator
                   |during compilation.         |initializer
     Ever Confirmed|0                           |1

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-06-21 17:51:43 UTC ---
Re-opening since the diagnostic could be better, the value is not "too large",
the problem is with the sign, not the magnitude.

enum E : unsigned long long { e = -1 };

e.cc:1:26: error: enumerator value -1 is too large for underlying type
'unsigned long long'

Clang gives a more accurate diagnostic, though not necessarily more helpful if
you don't know what a narrowing conversion is:

e.cc:1:25: error: enumerator value evaluates to -1, which cannot be narrowed to
type 'unsigned long long' [-Wc++11-narrowing]


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

* [Bug c++/53745] [C++11] Poor diagnostic for ill-formed narrowing conversion in enumerator initializer
  2012-06-21 13:04 [Bug c++/53745] New: Bitshifted value (1 << 31) within enumerator class is calculated incorrectly during compilation sbgccbug at yahoo dot co.uk
                   ` (2 preceding siblings ...)
  2012-06-21 17:52 ` [Bug c++/53745] [C++11] Poor diagnostic for ill-formed narrowing conversion in enumerator initializer redi at gcc dot gnu.org
@ 2013-05-03 23:23 ` paolo.carlini at oracle dot com
  2013-05-04 13:09 ` paolo.carlini at oracle dot com
  2013-05-05  0:29 ` paolo.carlini at oracle dot com
  5 siblings, 0 replies; 7+ messages in thread
From: paolo.carlini at oracle dot com @ 2013-05-03 23:23 UTC (permalink / raw)
  To: gcc-bugs


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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |vvnic.holas at gmail dot
                   |                            |com

--- Comment #4 from Paolo Carlini <paolo.carlini at oracle dot com> 2013-05-03 23:23:15 UTC ---
*** Bug 57164 has been marked as a duplicate of this bug. ***


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

* [Bug c++/53745] [C++11] Poor diagnostic for ill-formed narrowing conversion in enumerator initializer
  2012-06-21 13:04 [Bug c++/53745] New: Bitshifted value (1 << 31) within enumerator class is calculated incorrectly during compilation sbgccbug at yahoo dot co.uk
                   ` (3 preceding siblings ...)
  2013-05-03 23:23 ` paolo.carlini at oracle dot com
@ 2013-05-04 13:09 ` paolo.carlini at oracle dot com
  2013-05-05  0:29 ` paolo.carlini at oracle dot com
  5 siblings, 0 replies; 7+ messages in thread
From: paolo.carlini at oracle dot com @ 2013-05-04 13:09 UTC (permalink / raw)
  To: gcc-bugs


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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |ASSIGNED
         AssignedTo|unassigned at gcc dot       |paolo.carlini at oracle dot
                   |gnu.org                     |com

--- Comment #5 from Paolo Carlini <paolo.carlini at oracle dot com> 2013-05-04 13:09:37 UTC ---
I'm going to propose changing the message to "... outside the range...",
similarly to ICC: it seems a good compromise, less misleading than the current
message, less technical than clang's.


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

* [Bug c++/53745] [C++11] Poor diagnostic for ill-formed narrowing conversion in enumerator initializer
  2012-06-21 13:04 [Bug c++/53745] New: Bitshifted value (1 << 31) within enumerator class is calculated incorrectly during compilation sbgccbug at yahoo dot co.uk
                   ` (4 preceding siblings ...)
  2013-05-04 13:09 ` paolo.carlini at oracle dot com
@ 2013-05-05  0:29 ` paolo.carlini at oracle dot com
  5 siblings, 0 replies; 7+ messages in thread
From: paolo.carlini at oracle dot com @ 2013-05-05  0:29 UTC (permalink / raw)
  To: gcc-bugs


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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |4.9.0

--- Comment #6 from Paolo Carlini <paolo.carlini at oracle dot com> 2013-05-05 00:29:13 UTC ---
Fixed for 4.9.0.


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

end of thread, other threads:[~2013-05-05  0:29 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-21 13:04 [Bug c++/53745] New: Bitshifted value (1 << 31) within enumerator class is calculated incorrectly during compilation sbgccbug at yahoo dot co.uk
2012-06-21 13:35 ` [Bug c++/53745] " schwab@linux-m68k.org
2012-06-21 13:36 ` redi at gcc dot gnu.org
2012-06-21 17:52 ` [Bug c++/53745] [C++11] Poor diagnostic for ill-formed narrowing conversion in enumerator initializer redi at gcc dot gnu.org
2013-05-03 23:23 ` paolo.carlini at oracle dot com
2013-05-04 13:09 ` paolo.carlini at oracle dot com
2013-05-05  0:29 ` paolo.carlini at oracle 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).