public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* gcc 4.3.0, -Wconversion: assignment-by operators for shorter types
@ 2008-05-26 17:37 Andriy Gapon
  2008-05-27 10:51 ` Manuel López-Ibáñez
  0 siblings, 1 reply; 9+ messages in thread
From: Andriy Gapon @ 2008-05-26 17:37 UTC (permalink / raw)
  To: gcc


If I write something like the following:
uint8_t x;
uint8_t y;
x ^= y;

and compile with gcc 4.3.0 with  -Wconversion option, then I get the
following warning:
warning: conversion to ‘unsigned char’ from ‘int’ may alter its value
[-Wconversion]

While technically this warning is correct (because x and y are
implicitly advanced to "int"before actual xor operation), it seems that
it doesn't make much practical sense.

I think that this applies to all "assignment-by" operators working on
types shorter than int.

OTOH, proper implementation should be smart enough to warn about the
following:
uint8_t x;
uint16_t y;
x ^= y;


-- 
Andriy Gapon

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

* Re: gcc 4.3.0, -Wconversion: assignment-by operators for shorter types
  2008-05-26 17:37 gcc 4.3.0, -Wconversion: assignment-by operators for shorter types Andriy Gapon
@ 2008-05-27 10:51 ` Manuel López-Ibáñez
  2008-05-27 18:30   ` Andriy Gapon
  0 siblings, 1 reply; 9+ messages in thread
From: Manuel López-Ibáñez @ 2008-05-27 10:51 UTC (permalink / raw)
  To: Andriy Gapon; +Cc: gcc

Please, open a bug report: http://gcc.gnu.org/bugzilla/

Thanks,

Manuel.

2008/5/26 Andriy Gapon <avg@icyb.net.ua>:
>
> If I write something like the following:
> uint8_t x;
> uint8_t y;
> x ^= y;
>
> and compile with gcc 4.3.0 with  -Wconversion option, then I get the
> following warning:
> warning: conversion to 'unsigned char' from 'int' may alter its value
> [-Wconversion]
>
> While technically this warning is correct (because x and y are
> implicitly advanced to "int"before actual xor operation), it seems that
> it doesn't make much practical sense.
>
> I think that this applies to all "assignment-by" operators working on
> types shorter than int.
>
> OTOH, proper implementation should be smart enough to warn about the
> following:
> uint8_t x;
> uint16_t y;
> x ^= y;
>
>
> --
> Andriy Gapon
>

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

* Re: gcc 4.3.0, -Wconversion: assignment-by operators for shorter  types
  2008-05-27 10:51 ` Manuel López-Ibáñez
@ 2008-05-27 18:30   ` Andriy Gapon
  2008-05-27 18:48     ` Andrew Pinski
  0 siblings, 1 reply; 9+ messages in thread
From: Andriy Gapon @ 2008-05-27 18:30 UTC (permalink / raw)
  To: Manuel López-Ibáñez; +Cc: gcc

on 27/05/2008 13:50 Manuel López-Ibáñez said the following:
> Please, open a bug report: http://gcc.gnu.org/bugzilla/

Manuel,

I looked through open GCC bugs and it seems that the following bugs are 
very similar to what I report:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34389
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=32643

Should I still open a new one or maybe it's better to follow up on one 
of those two.

And just in case, here's a better description of what I am reporting 
(demonstration by comparison):
**********************
int main()
{
         int int_x;
         int int_y;
         short short_x;
         short short_y;

         short_x += short_y;
         short_x = short_x + short_y;

         int_x += int_y;
         int_x = int_x + int_y;

         return 0;
}
**********************

$ g++ -Wconversion test-conv.cc -o test-conv
test-conv.cc: In function ‘int main()’:
test-conv.cc:8: warning: conversion to ‘short int’ from ‘int’ may alter 
its value
test-conv.cc:9: warning: conversion to ‘short int’ from ‘int’ may alter 
its value

$ g++ -dumpversion
4.3.0


> 2008/5/26 Andriy Gapon <avg@icyb.net.ua>:
>> If I write something like the following:
>> uint8_t x;
>> uint8_t y;
>> x ^= y;
>>
>> and compile with gcc 4.3.0 with  -Wconversion option, then I get the
>> following warning:
>> warning: conversion to 'unsigned char' from 'int' may alter its value
>> [-Wconversion]
>>
>> While technically this warning is correct (because x and y are
>> implicitly advanced to "int"before actual xor operation), it seems that
>> it doesn't make much practical sense.
>>
>> I think that this applies to all "assignment-by" operators working on
>> types shorter than int.
>>
>> OTOH, proper implementation should be smart enough to warn about the
>> following:
>> uint8_t x;
>> uint16_t y;
>> x ^= y;
>>
>>
>> --
>> Andriy Gapon
>>


-- 
Andriy Gapon

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

* Re: gcc 4.3.0, -Wconversion: assignment-by operators for shorter types
  2008-05-27 18:30   ` Andriy Gapon
@ 2008-05-27 18:48     ` Andrew Pinski
  2008-05-27 18:56       ` Andriy Gapon
  0 siblings, 1 reply; 9+ messages in thread
From: Andrew Pinski @ 2008-05-27 18:48 UTC (permalink / raw)
  To: Andriy Gapon; +Cc: Manuel López-Ibáñez, gcc

On Tue, May 27, 2008 at 11:29 AM, Andriy Gapon <avg@icyb.net.ua> wrote:
> Should I still open a new one or maybe it's better to follow up on one of
> those two.
>
> And just in case, here's a better description of what I am reporting
> (demonstration by comparison):
The warnings with the below code is correct because of how C/C++
define promotions.  It is ^/|/& where the warnings are not correct and
should be solved.

Thanks,
Andrew Pinski

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

* Re: gcc 4.3.0, -Wconversion: assignment-by operators for shorter  types
  2008-05-27 18:48     ` Andrew Pinski
@ 2008-05-27 18:56       ` Andriy Gapon
  2008-05-27 19:01         ` Andrew Pinski
  0 siblings, 1 reply; 9+ messages in thread
From: Andriy Gapon @ 2008-05-27 18:56 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: Manuel López-Ibáñez, gcc

on 27/05/2008 21:48 Andrew Pinski said the following:
> On Tue, May 27, 2008 at 11:29 AM, Andriy Gapon <avg@icyb.net.ua> wrote:
>> Should I still open a new one or maybe it's better to follow up on one of
>> those two.
>>
>> And just in case, here's a better description of what I am reporting
>> (demonstration by comparison):
> The warnings with the below code is correct because of how C/C++
> define promotions.  It is ^/|/& where the warnings are not correct and
> should be solved.

Thank you for the explanation! I didn't realize the difference.

OTOH, do you think that those arithmetic warnings are practical (as 
opposed to being correct)?
I understand the rules of the language(s) but honestly I expect the same 
behavior for short and for int in expressions like those. And in cases 
where I don't expect it I'll be careful about result type anyway.


-- 
Andriy Gapon

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

* Re: gcc 4.3.0, -Wconversion: assignment-by operators for shorter types
  2008-05-27 18:56       ` Andriy Gapon
@ 2008-05-27 19:01         ` Andrew Pinski
  2008-05-28  6:49           ` Andriy Gapon
  2008-07-30  9:38           ` Manuel López-Ibáñez
  0 siblings, 2 replies; 9+ messages in thread
From: Andrew Pinski @ 2008-05-27 19:01 UTC (permalink / raw)
  To: Andriy Gapon; +Cc: Manuel López-Ibáñez, gcc

On Tue, May 27, 2008 at 11:56 AM, Andriy Gapon <avg@icyb.net.ua> wrote:
> Thank you for the explanation! I didn't realize the difference.
>
> OTOH, do you think that those arithmetic warnings are practical (as opposed
> to being correct)?

I think so as the short int case has a defined overflow of the signed
short type that is SHRT_MAX + 1 is defined while INT_MAX + 1 is not.

-- Pinski

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

* Re: gcc 4.3.0, -Wconversion: assignment-by operators for shorter  types
  2008-05-27 19:01         ` Andrew Pinski
@ 2008-05-28  6:49           ` Andriy Gapon
  2008-07-30  9:24             ` Manuel López-Ibáñez
  2008-07-30  9:38           ` Manuel López-Ibáñez
  1 sibling, 1 reply; 9+ messages in thread
From: Andriy Gapon @ 2008-05-28  6:49 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: Manuel López-Ibáñez, gcc

on 27/05/2008 22:00 Andrew Pinski said the following:
> On Tue, May 27, 2008 at 11:56 AM, Andriy Gapon <avg@icyb.net.ua> wrote:
>> Thank you for the explanation! I didn't realize the difference.
>>
>> OTOH, do you think that those arithmetic warnings are practical (as opposed
>> to being correct)?
> 
> I think so as the short int case has a defined overflow of the signed
> short type that is SHRT_MAX + 1 is defined while INT_MAX + 1 is not.

I still feel like disagreeing.
Consider this:
*************
int main()
{
         short short_x;

         short_x = short_x + 1;
         short_x += 1;
         short_x++;
         ++short_x;

         return 0;
}
*************

$ gcc43 -Wconversion test-conv2.c -o test-conv
test-conv2.cc: In function 'int main()':
test-conv2.cc:5: warning: conversion to 'short int' from 'int' may alter 
its value
test-conv2.cc:6: warning: conversion to 'short int' from 'int' may alter 
its value

I thought that in C all 4 forms were equivalent and this was purely a 
style choice. Now they are different.

-- 
Andriy Gapon

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

* Re: gcc 4.3.0, -Wconversion: assignment-by operators for shorter types
  2008-05-28  6:49           ` Andriy Gapon
@ 2008-07-30  9:24             ` Manuel López-Ibáñez
  0 siblings, 0 replies; 9+ messages in thread
From: Manuel López-Ibáñez @ 2008-07-30  9:24 UTC (permalink / raw)
  To: Andriy Gapon; +Cc: Andrew Pinski, gcc

2008/5/28 Andriy Gapon <avg@icyb.net.ua>:
> on 27/05/2008 22:00 Andrew Pinski said the following:
>>
>> On Tue, May 27, 2008 at 11:56 AM, Andriy Gapon <avg@icyb.net.ua> wrote:
>>>
>>> Thank you for the explanation! I didn't realize the difference.
>>>
>>> OTOH, do you think that those arithmetic warnings are practical (as
>>> opposed
>>> to being correct)?
>>
>> I think so as the short int case has a defined overflow of the signed
>> short type that is SHRT_MAX + 1 is defined while INT_MAX + 1 is not.
>
> I still feel like disagreeing.
> Consider this:
> *************
> int main()
> {
>        short short_x;
>
>        short_x = short_x + 1;
>        short_x += 1;
>        short_x++;
>        ++short_x;
>
>        return 0;
> }
> *************
>
> $ gcc43 -Wconversion test-conv2.c -o test-conv
> test-conv2.cc: In function 'int main()':
> test-conv2.cc:5: warning: conversion to 'short int' from 'int' may alter its
> value
> test-conv2.cc:6: warning: conversion to 'short int' from 'int' may alter its
> value
>
> I thought that in C all 4 forms were equivalent and this was purely a style
> choice. Now they are different.
>

Please open a bug report (http://gcc.gnu.org/bugs.html) with this
issue and add manu@gcc.gnu.org to the CC list. Otherwise this will be
forgotten.

Cheers,

Manuel.

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

* Re: gcc 4.3.0, -Wconversion: assignment-by operators for shorter types
  2008-05-27 19:01         ` Andrew Pinski
  2008-05-28  6:49           ` Andriy Gapon
@ 2008-07-30  9:38           ` Manuel López-Ibáñez
  1 sibling, 0 replies; 9+ messages in thread
From: Manuel López-Ibáñez @ 2008-07-30  9:38 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: Andriy Gapon, gcc

2008/5/27 Andrew Pinski <pinskia@gmail.com>:
> On Tue, May 27, 2008 at 11:56 AM, Andriy Gapon <avg@icyb.net.ua> wrote:
>> Thank you for the explanation! I didn't realize the difference.
>>
>> OTOH, do you think that those arithmetic warnings are practical (as opposed
>> to being correct)?
>
> I think so as the short int case has a defined overflow of the signed
> short type that is SHRT_MAX + 1 is defined while INT_MAX + 1 is not.

Andrew, I don't understand what you mean by "SHRT_MAX + 1 is defined
while INT_MAX + 1 is not".

Is there actually any difference in the following cases given   short
short_x = SHRT_MAX ?

       short_x = short_x + 1;
       short_x += 1;
       short_x++;
       ++short_x;

What about unsigned short ushort_x = USHRT_MAX ?

       ushort_x = ushort_x + 1;
       ushort_x += 1;
       ushort_x++;
       ++ushort_x;

Cheers,

Manuel.

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

end of thread, other threads:[~2008-07-30  8:50 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-26 17:37 gcc 4.3.0, -Wconversion: assignment-by operators for shorter types Andriy Gapon
2008-05-27 10:51 ` Manuel López-Ibáñez
2008-05-27 18:30   ` Andriy Gapon
2008-05-27 18:48     ` Andrew Pinski
2008-05-27 18:56       ` Andriy Gapon
2008-05-27 19:01         ` Andrew Pinski
2008-05-28  6:49           ` Andriy Gapon
2008-07-30  9:24             ` Manuel López-Ibáñez
2008-07-30  9:38           ` Manuel López-Ibáñez

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