public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/50011] New: [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing]
@ 2011-08-07 11:10 zsojka at seznam dot cz
  2011-08-07 20:46 ` [Bug c++/50011] " marc.glisse at normalesup dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: zsojka at seznam dot cz @ 2011-08-07 11:10 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: [C++0x] warning: narrowing conversion of 'i' from
                    'short unsigned int' to 'int' inside { } [-Wnarrowing]
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: zsojka@seznam.cz
                CC: jason@gcc.gnu.org


Since fixing PR49577, gcc gives a warning for the following code:

void foo(short unsigned i)
{
  int a[] = {i};
}

$ g++ -std=gnu++0x tst.C
tst.C: In function 'int foo(short unsigned int)':
tst.C:3:14: warning: narrowing conversion of 'i' from 'short unsigned int' to
'int' inside { } [-Wnarrowing]

8.5.4/6 says:
A narrowing conversion is an implicit conversion
...
— from an integer type or unscoped enumeration type to an integer type that
cannot represent all the values of the original type
...

In this case, 'int' can represent all the values of 'short unsigned int', so
unless I am missing something, this should be valid.


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

* [Bug c++/50011] [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing]
  2011-08-07 11:10 [Bug c++/50011] New: [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing] zsojka at seznam dot cz
@ 2011-08-07 20:46 ` marc.glisse at normalesup dot org
  2011-08-08  7:55 ` zsojka at seznam dot cz
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: marc.glisse at normalesup dot org @ 2011-08-07 20:46 UTC (permalink / raw)
  To: gcc-bugs

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

Marc Glisse <marc.glisse at normalesup dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |marc.glisse at normalesup
                   |                            |dot org

--- Comment #1 from Marc Glisse <marc.glisse at normalesup dot org> 2011-08-07 20:45:38 UTC ---
(In reply to comment #0)
> 'int' can represent all the values of 'short unsigned int'

That may be true on your platform, but I don't think it is guaranteed to be
true in general. I don't know if that explains the warning...


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

* [Bug c++/50011] [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing]
  2011-08-07 11:10 [Bug c++/50011] New: [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing] zsojka at seznam dot cz
  2011-08-07 20:46 ` [Bug c++/50011] " marc.glisse at normalesup dot org
@ 2011-08-08  7:55 ` zsojka at seznam dot cz
  2011-08-08 10:08 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: zsojka at seznam dot cz @ 2011-08-08  7:55 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Zdenek Sojka <zsojka at seznam dot cz> 2011-08-08 07:53:54 UTC ---
(In reply to comment #1)
> That may be true on your platform, but I don't think it is guaranteed to be
> true in general. I don't know if that explains the warning...

Yes, I should have mentioned the target is x86_64-unknown-linux-gnu.
Generally, the code added in r177215:

+      if ((TYPE_PRECISION (type) < TYPE_PRECISION (ftype)
+       || TYPE_UNSIGNED (type) != TYPE_UNSIGNED (ftype))

looks like no unsigned type can ever be losslessly converted to a signed type
(even when it's "longer"), which is not true (on the other hand, an unsigned
type can never represent all values of any signed type, because it can't
represent negative numbers). But of course, I am not a language lawyer at all.


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

* [Bug c++/50011] [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing]
  2011-08-07 11:10 [Bug c++/50011] New: [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing] zsojka at seznam dot cz
  2011-08-07 20:46 ` [Bug c++/50011] " marc.glisse at normalesup dot org
  2011-08-08  7:55 ` zsojka at seznam dot cz
@ 2011-08-08 10:08 ` rguenth at gcc dot gnu.org
  2011-08-08 10:13 ` marc.glisse at normalesup dot org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2011-08-08 10:08 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011.08.08 10:02:27
     Ever Confirmed|0                           |1

--- Comment #3 from Richard Guenther <rguenth at gcc dot gnu.org> 2011-08-08 10:02:27 UTC ---
+      if ((TYPE_PRECISION (type) < TYPE_PRECISION (ftype)
+       || TYPE_UNSIGNED (type) != TYPE_UNSIGNED (ftype))

looks wrong.  I guess it should be

      if ((TYPE_PRECISION (type) < TYPE_PRECISION (ftype)
          || (TYPE_PRECISION (type) == TYPE_PRECISION (ftype)
              && TYPE_UNSIGNED (type) != TYPE_UNSIGNED (ftype)))

instead.


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

* [Bug c++/50011] [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing]
  2011-08-07 11:10 [Bug c++/50011] New: [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing] zsojka at seznam dot cz
                   ` (2 preceding siblings ...)
  2011-08-08 10:08 ` rguenth at gcc dot gnu.org
@ 2011-08-08 10:13 ` marc.glisse at normalesup dot org
  2011-08-08 11:04 ` zsojka at seznam dot cz
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: marc.glisse at normalesup dot org @ 2011-08-08 10:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Marc Glisse <marc.glisse at normalesup dot org> 2011-08-08 10:12:58 UTC ---
(In reply to comment #2)
> Yes, I should have mentioned the target is x86_64-unknown-linux-gnu.
> Generally, the code added in r177215:
> 
> +      if ((TYPE_PRECISION (type) < TYPE_PRECISION (ftype)
> +       || TYPE_UNSIGNED (type) != TYPE_UNSIGNED (ftype))
> 
> looks like no unsigned type can ever be losslessly converted to a signed type
> (even when it's "longer"), which is not true (on the other hand, an unsigned
> type can never represent all values of any signed type, because it can't
> represent negative numbers). But of course, I am not a language lawyer at all.

So (if I follow you) g++ is complaining (with a warning, not an error) because
your code is illegal on some other platforms? (I don't think anything forbids
CHAR_BIT==64 and sizeof(long long)==1 in C/C++)


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

* [Bug c++/50011] [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing]
  2011-08-07 11:10 [Bug c++/50011] New: [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing] zsojka at seznam dot cz
                   ` (3 preceding siblings ...)
  2011-08-08 10:13 ` marc.glisse at normalesup dot org
@ 2011-08-08 11:04 ` zsojka at seznam dot cz
  2011-08-08 11:14 ` marc.glisse at normalesup dot org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: zsojka at seznam dot cz @ 2011-08-08 11:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Zdenek Sojka <zsojka at seznam dot cz> 2011-08-08 11:03:29 UTC ---
(In reply to comment #3)
> +      if ((TYPE_PRECISION (type) < TYPE_PRECISION (ftype)
> +       || TYPE_UNSIGNED (type) != TYPE_UNSIGNED (ftype))
> 
> looks wrong.  I guess it should be
> 
>       if ((TYPE_PRECISION (type) < TYPE_PRECISION (ftype)
>           || (TYPE_PRECISION (type) == TYPE_PRECISION (ftype)
>               && TYPE_UNSIGNED (type) != TYPE_UNSIGNED (ftype)))
> 
> instead.

Wouldn't that allow 'signed short' -> 'unsigned int' conversions?

Maybe
       if ((TYPE_PRECISION (type) < TYPE_PRECISION (ftype)
           || (TYPE_PRECISION (type) == TYPE_PRECISION (ftype)
               && TYPE_SIGNED (type) && TYPE_UNSIGNED (ftype))
           || (TYPE_UNSIGNED (type) && TYPE_SIGNED (ftype))
if it means "converting to a smaller type OR converting an unsigned type to a
signed type of the same precision OR convering signed to unsigned". But then
the condition gets quite complicated I would wonder if there isn't a macro for
that check somewhere... (also, it might be wrong)

Maybe simpler would be to just compare type's and ftype's min/max values, if
there is a simple way do to that. (I don't really have GCC sources by hand)


(In reply to comment #4)
> (In reply to comment #2)
> 
> So (if I follow you) g++ is complaining (with a warning, not an error) because
> your code is illegal on some other platforms? (I don't think anything forbids
> CHAR_BIT==64 and sizeof(long long)==1 in C/C++)

It gives a warning probably because of a mistake in the gcc sources.


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

* [Bug c++/50011] [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing]
  2011-08-07 11:10 [Bug c++/50011] New: [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing] zsojka at seznam dot cz
                   ` (4 preceding siblings ...)
  2011-08-08 11:04 ` zsojka at seznam dot cz
@ 2011-08-08 11:14 ` marc.glisse at normalesup dot org
  2011-08-08 14:37 ` jason at gcc dot gnu.org
  2011-08-08 18:09 ` jason at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: marc.glisse at normalesup dot org @ 2011-08-08 11:14 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Marc Glisse <marc.glisse at normalesup dot org> 2011-08-08 11:14:06 UTC ---
(In reply to comment #5)
> It gives a warning probably because of a mistake in the gcc sources.

Or not. In your example, remove short and replace int with long. Now you are
asking for a warning that would be there on x86 but not on x86_64, that doesn't
help write portable code.

Now your suggestion may be right, but I want to stress that it is not obvious.


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

* [Bug c++/50011] [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing]
  2011-08-07 11:10 [Bug c++/50011] New: [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing] zsojka at seznam dot cz
                   ` (5 preceding siblings ...)
  2011-08-08 11:14 ` marc.glisse at normalesup dot org
@ 2011-08-08 14:37 ` jason at gcc dot gnu.org
  2011-08-08 18:09 ` jason at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2011-08-08 14:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jason Merrill <jason at gcc dot gnu.org> 2011-08-08 14:36:27 UTC ---
Author: jason
Date: Mon Aug  8 14:36:22 2011
New Revision: 177565

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=177565
Log:
    PR c++/50011
    * typeck2.c (check_narrowing): Fix integer logic.

Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/typeck2.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/cpp0x/initlist5.C


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

* [Bug c++/50011] [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing]
  2011-08-07 11:10 [Bug c++/50011] New: [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing] zsojka at seznam dot cz
                   ` (6 preceding siblings ...)
  2011-08-08 14:37 ` jason at gcc dot gnu.org
@ 2011-08-08 18:09 ` jason at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2011-08-08 18:09 UTC (permalink / raw)
  To: gcc-bugs

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

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED
   Target Milestone|---                         |4.7.0

--- Comment #8 from Jason Merrill <jason at gcc dot gnu.org> 2011-08-08 18:09:13 UTC ---
Fixed.


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

end of thread, other threads:[~2011-08-08 18:09 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-07 11:10 [Bug c++/50011] New: [C++0x] warning: narrowing conversion of 'i' from 'short unsigned int' to 'int' inside { } [-Wnarrowing] zsojka at seznam dot cz
2011-08-07 20:46 ` [Bug c++/50011] " marc.glisse at normalesup dot org
2011-08-08  7:55 ` zsojka at seznam dot cz
2011-08-08 10:08 ` rguenth at gcc dot gnu.org
2011-08-08 10:13 ` marc.glisse at normalesup dot org
2011-08-08 11:04 ` zsojka at seznam dot cz
2011-08-08 11:14 ` marc.glisse at normalesup dot org
2011-08-08 14:37 ` jason at gcc dot gnu.org
2011-08-08 18:09 ` jason at gcc dot gnu.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).