public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/35852]  New: -Wconversion rejects bitwise negation and assignment, cannot cast around
@ 2008-04-06 22:55 fang at csl dot cornell dot edu
  2008-04-18 15:08 ` [Bug c++/35852] " manu at gcc dot gnu dot org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: fang at csl dot cornell dot edu @ 2008-04-06 22:55 UTC (permalink / raw)
  To: gcc-bugs

g++-4.3.0 -ansi -pedantic-errors -std=c++0x -Wall -Wconversion
issues the following diagnostic on the following code:

// test case:
typedef unsigned short  ushort;

enum {
        FOO = 0x13
};

template <typename T>
inline
void
andnot(T& lv, T&& rv) {
        lv &= ~rv;
}

template <typename T>
inline
void
andnot(T& lv, const T& rv) {
        lv &= ~rv;
}

int
main(int, char*[]) {
        ushort x = 99;
        x = ~FOO;
        x &= ~FOO;              // -Wconversion
        x = x & ~FOO;           // -Wconversion
        x = x & ushort(~FOO);   // -Wconversion
        x = x & ushort(~ushort(FOO));   // -Wconversion
        x &= static_cast<ushort>(~FOO);         // -Wconversion
        x &= ~x;
        // andnot(x, FOO);              // no match
        andnot(x, ushort(FOO)); // -Wconversion
        return x;
}
// end test case.

../../../src/conv.cc: In function 'int main(int, char**)':
../../../src/conv.cc:25: warning: conversion to 'ushort' from 'int' may alter
its value
../../../src/conv.cc:26: warning: conversion to 'ushort' from 'int' may alter
its value
../../../src/conv.cc:27: warning: conversion to 'ushort' from 'int' may alter
its value
../../../src/conv.cc:28: warning: conversion to 'ushort' from 'int' may alter
its value
../../../src/conv.cc:29: warning: conversion to 'ushort' from 'int' may alter
its value
../../../src/conv.cc: In function 'void andnot(T&, T&&) [with T = ushort]':
../../../src/conv.cc:32:   instantiated from here
../../../src/conv.cc:11: warning: conversion to 'short unsigned int' from 'int'
may alter its value

All warnings disappear if -Wconversion is omitted.  
I don't understand the rationale why some of the above lines are diagnostic
free (assignments), and others trigger the diagnostic, so I cannot claim which
are valid w.r.t. -Wconversion.  I did however expect a function-style
(ctor-style) cast or static_cast to suppress the diagnostic.  
Is there a bug, or proper "workaround" to locally suppress the diagnostic?

keywords: diagnostic


-- 
           Summary: -Wconversion rejects bitwise negation and assignment,
                    cannot cast around
           Product: gcc
           Version: 4.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: fang at csl dot cornell dot edu


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


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

* [Bug c++/35852] -Wconversion rejects bitwise negation and assignment, cannot cast around
  2008-04-06 22:55 [Bug c++/35852] New: -Wconversion rejects bitwise negation and assignment, cannot cast around fang at csl dot cornell dot edu
@ 2008-04-18 15:08 ` manu at gcc dot gnu dot org
  2008-08-01 10:09 ` manu at gcc dot gnu dot org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-04-18 15:08 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from manu at gcc dot gnu dot org  2008-04-18 15:08 -------
The C/C++ front-end plays games for bitwise operators, so it may drop the casts
but then use the original type or surround the whole operation by a conversion
to int. Part of the problem is fixed in mainline. However, other parts remain
(PR35701 and PR34389). I don't have time to work on this at the moment but
there was an initial attempt was in the thread originated here:

http://gcc.gnu.org/ml/gcc-patches/2008-02/msg00611.html


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu dot org
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|0                           |1
   Last reconfirmed|0000-00-00 00:00:00         |2008-04-18 15:08:04
               date|                            |


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


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

* [Bug c++/35852] -Wconversion rejects bitwise negation and assignment, cannot cast around
  2008-04-06 22:55 [Bug c++/35852] New: -Wconversion rejects bitwise negation and assignment, cannot cast around fang at csl dot cornell dot edu
  2008-04-18 15:08 ` [Bug c++/35852] " manu at gcc dot gnu dot org
@ 2008-08-01 10:09 ` manu at gcc dot gnu dot org
  2008-08-01 10:23 ` paolo dot carlini at oracle dot com
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-08-01 10:09 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from manu at gcc dot gnu dot org  2008-08-01 10:08 -------
Could you provide a testcase that does not need -std=c++0x ?


-- 


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


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

* [Bug c++/35852] -Wconversion rejects bitwise negation and assignment, cannot cast around
  2008-04-06 22:55 [Bug c++/35852] New: -Wconversion rejects bitwise negation and assignment, cannot cast around fang at csl dot cornell dot edu
  2008-04-18 15:08 ` [Bug c++/35852] " manu at gcc dot gnu dot org
  2008-08-01 10:09 ` manu at gcc dot gnu dot org
@ 2008-08-01 10:23 ` paolo dot carlini at oracle dot com
  2008-08-01 17:55 ` manu at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: paolo dot carlini at oracle dot com @ 2008-08-01 10:23 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from paolo dot carlini at oracle dot com  2008-08-01 10:22 -------
If you just comment out the andnot(T&, T&&) overload, and compile without
-std=c++0x, I think the warnings at issue are all still there.


-- 


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


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

* [Bug c++/35852] -Wconversion rejects bitwise negation and assignment, cannot cast around
  2008-04-06 22:55 [Bug c++/35852] New: -Wconversion rejects bitwise negation and assignment, cannot cast around fang at csl dot cornell dot edu
                   ` (2 preceding siblings ...)
  2008-08-01 10:23 ` paolo dot carlini at oracle dot com
@ 2008-08-01 17:55 ` manu at gcc dot gnu dot org
  2008-08-22 13:55 ` manu at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-08-01 17:55 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from manu at gcc dot gnu dot org  2008-08-01 17:54 -------
To be honest, I am not sure what deserves a warning and what not in this
testcase.

FOO is 19, ~FOO is -20. Therefore 
ushort x = ~FOO seems to deserve a warning (with -Wsing-conversion at least).

x = x & -20 is handled as x = (int)x & -20. If x == 0xFFFF, this seems like the
case above and probably deserves a warning.

lv = lv & ~rv is seen as:

 <bit_and_expr 
    type <integer_type int public SI
        size <integer_cst constant 32>
        unit size <integer_cst  constant 4>
    arg 0 <nop_expr 0x2aaaab238940 type <integer_type  int>

        arg 0 <indirect_ref  type <integer_type  short unsigned int>
            arg 0 <parm_decl  lv>>>
    arg 1 <bit_not_expr  type <integer_type  int>
        arg 0 <nop_expr  type <integer_type  int>
            arg 0 <indirect_ref  type <integer_type  short unsigned int>
                tree_0 arg 0 <parm_decl  rv>>>>>

That is, we would need to peek inside each operand and recursively check
whether the expressions/conversions are safe. I don't know if this is feasible
at all and I personally don't know at this moment how to implement it properly.

On the other hand, in mainline, I don't get any warnings for:

        x = x & ushort(~FOO);   // -Wconversion
        x = x & ushort(~ushort(FOO));   // -Wconversion
        x &= static_cast<ushort>(~FOO);         // -Wconversion

So that seems fixed.


-- 


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


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

* [Bug c++/35852] -Wconversion rejects bitwise negation and assignment, cannot cast around
  2008-04-06 22:55 [Bug c++/35852] New: -Wconversion rejects bitwise negation and assignment, cannot cast around fang at csl dot cornell dot edu
                   ` (3 preceding siblings ...)
  2008-08-01 17:55 ` manu at gcc dot gnu dot org
@ 2008-08-22 13:55 ` manu at gcc dot gnu dot org
  2008-08-29  3:39 ` manu at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-08-22 13:55 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from manu at gcc dot gnu dot org  2008-08-22 13:53 -------
This is the current situation as of revision 139373.

typedef unsigned short  ushort;

enum {
        FOO = 0x13
};

template <typename T>
inline
void
andnot(T& lv, const T& rv) {
  lv &= ~rv; // -Wconversion  int(lv) & ~(int(rv))
}

int
main(int, char*[]) {
  ushort x = 99;
  x = ~FOO;               // -Wsign-conversion -20 -> unsigned short
  x &= ~FOO;              // -Wconversion int(x) & ~(int(19)) -> unsigned short
  x = x & ~FOO;           // -Wconversion int(x) & ~(int(19)) -> unsigned short
  x = x & ushort(~FOO);   // no warning
  x = x & ushort(~ushort(FOO));   // no warning
  x &= static_cast<ushort>(~FOO); // no warning
  x &= ~x;                        // no warning
  andnot(x, ushort(FOO)); // instantiated from here
  return x;
}

I don't see what is wrong with the warnings. Would you mind elaborating?


-- 


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


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

* [Bug c++/35852] -Wconversion rejects bitwise negation and assignment, cannot cast around
  2008-04-06 22:55 [Bug c++/35852] New: -Wconversion rejects bitwise negation and assignment, cannot cast around fang at csl dot cornell dot edu
                   ` (4 preceding siblings ...)
  2008-08-22 13:55 ` manu at gcc dot gnu dot org
@ 2008-08-29  3:39 ` manu at gcc dot gnu dot org
  2008-08-29  4:56 ` fang at csl dot cornell dot edu
  2008-10-23  1:50 ` manu at gcc dot gnu dot org
  7 siblings, 0 replies; 9+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-08-29  3:39 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from manu at gcc dot gnu dot org  2008-08-29 03:38 -------
This is waiting for feedback.


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |WAITING


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


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

* [Bug c++/35852] -Wconversion rejects bitwise negation and assignment, cannot cast around
  2008-04-06 22:55 [Bug c++/35852] New: -Wconversion rejects bitwise negation and assignment, cannot cast around fang at csl dot cornell dot edu
                   ` (5 preceding siblings ...)
  2008-08-29  3:39 ` manu at gcc dot gnu dot org
@ 2008-08-29  4:56 ` fang at csl dot cornell dot edu
  2008-10-23  1:50 ` manu at gcc dot gnu dot org
  7 siblings, 0 replies; 9+ messages in thread
From: fang at csl dot cornell dot edu @ 2008-08-29  4:56 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from fang at csl dot cornell dot edu  2008-08-29 04:54 -------
(In reply to comment #5)
> This is the current situation as of revision 139373.
> 
> typedef unsigned short  ushort;
> 
> enum {
>         FOO = 0x13
> };
> 
> template <typename T>
> inline
> void
> andnot(T& lv, const T& rv) {
>   lv &= ~rv; // -Wconversion  int(lv) & ~(int(rv))
> }
> 
> int
> main(int, char*[]) {
>   ushort x = 99;
>   x = ~FOO;               // -Wsign-conversion -20 -> unsigned short
>   x &= ~FOO;              // -Wconversion int(x) & ~(int(19)) -> unsigned short
>   x = x & ~FOO;           // -Wconversion int(x) & ~(int(19)) -> unsigned short
>   x = x & ushort(~FOO);   // no warning
>   x = x & ushort(~ushort(FOO));   // no warning
>   x &= static_cast<ushort>(~FOO); // no warning
>   x &= ~x;                        // no warning
>   andnot(x, ushort(FOO)); // instantiated from here
>   return x;
> }
> 
> I don't see what is wrong with the warnings. Would you mind elaborating?

The situation then seems to have improved since I first reported it. 
Originally, none of the cases with explicit casts would pass, but comment #4
suggests that those have been fixed.  I expect the cast-less cases to have
warnings.  (I would consider this fixed.)


-- 


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


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

* [Bug c++/35852] -Wconversion rejects bitwise negation and assignment, cannot cast around
  2008-04-06 22:55 [Bug c++/35852] New: -Wconversion rejects bitwise negation and assignment, cannot cast around fang at csl dot cornell dot edu
                   ` (6 preceding siblings ...)
  2008-08-29  4:56 ` fang at csl dot cornell dot edu
@ 2008-10-23  1:50 ` manu at gcc dot gnu dot org
  7 siblings, 0 replies; 9+ messages in thread
From: manu at gcc dot gnu dot org @ 2008-10-23  1:50 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #8 from manu at gcc dot gnu dot org  2008-10-23 01:48 -------
Marked as FIXED then. Thanks for the report.


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|WAITING                     |RESOLVED
         Resolution|                            |FIXED


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


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

end of thread, other threads:[~2008-10-23  1:50 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-06 22:55 [Bug c++/35852] New: -Wconversion rejects bitwise negation and assignment, cannot cast around fang at csl dot cornell dot edu
2008-04-18 15:08 ` [Bug c++/35852] " manu at gcc dot gnu dot org
2008-08-01 10:09 ` manu at gcc dot gnu dot org
2008-08-01 10:23 ` paolo dot carlini at oracle dot com
2008-08-01 17:55 ` manu at gcc dot gnu dot org
2008-08-22 13:55 ` manu at gcc dot gnu dot org
2008-08-29  3:39 ` manu at gcc dot gnu dot org
2008-08-29  4:56 ` fang at csl dot cornell dot edu
2008-10-23  1:50 ` manu 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).