This PR complains about bogus -Wsign-conversion warning even with an explicit cast, which the documentation says should suppress the warning. Pretty similar to c++/86190 which I fixed here <https://gcc.gnu.org/ml/gcc-patches/2018-07/msg00115.html>.
The problem only happens when using a typedef, because in cp_build_binary_op we're comparing the types using ==, which I think only works for canonical types. So use same_type_p to avoid this problem. Bootstrapped/regtested on x86_64-linux, ok for trunk? I think it makes sense to fix in in 9.3 too. 2019-08-08 Marek Polacek <pola...@redhat.com> PR c++/87519 - bogus warning with -Wsign-conversion. * typeck.c (cp_build_binary_op): Use same_type_p instead of comparing the types directly. * g++.dg/warn/Wsign-conversion-5.C: New test. diff --git gcc/cp/typeck.c gcc/cp/typeck.c index 7e4ea3bee75..4cc0ee0128d 100644 --- gcc/cp/typeck.c +++ gcc/cp/typeck.c @@ -5509,9 +5509,9 @@ cp_build_binary_op (const op_location_t &location, if (! converted) { warning_sentinel w (warn_sign_conversion, short_compare); - if (TREE_TYPE (op0) != result_type) + if (!same_type_p (TREE_TYPE (op0), result_type)) op0 = cp_convert_and_check (result_type, op0, complain); - if (TREE_TYPE (op1) != result_type) + if (!same_type_p (TREE_TYPE (op1), result_type)) op1 = cp_convert_and_check (result_type, op1, complain); if (op0 == error_mark_node || op1 == error_mark_node) diff --git gcc/testsuite/g++.dg/warn/Wsign-conversion-5.C gcc/testsuite/g++.dg/warn/Wsign-conversion-5.C new file mode 100644 index 00000000000..ff384164901 --- /dev/null +++ gcc/testsuite/g++.dg/warn/Wsign-conversion-5.C @@ -0,0 +1,18 @@ +// PR c++/87519 - bogus warning with -Wsign-conversion. +// { dg-options "-Wsign-conversion" } + +typedef unsigned long int uint64_t; + +void f(unsigned long int a, int q) +{ + a += a + q; // { dg-warning "may change the sign" } + + // Explicit cast should disable the warning. + a = a + static_cast<uint64_t>(q); + a = a + (uint64_t) q; + a = a + uint64_t(q); + a = a + static_cast<const uint64_t>(q); + a = a + (const uint64_t) q; + a = a + static_cast<unsigned long int>(q); + a = a + static_cast<const unsigned long int>(q); +}