[Bug c++/69864] Fix various Wnarrowing minor issues

manu at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Feb 19 11:22:00 GMT 2016


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69864

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic, documentation,
                   |                            |easyhack
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|DUPLICATE                   |---
            Summary|Narrowing conversion        |Fix various Wnarrowing
                   |warning instead of an error |minor issues

--- Comment #8 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #6)
> Because int b{2.3} absolutely definitely loses precision, and since it's a
> constant you can just fix the code.
> 
> For {u} the compiler doesn't know whether it really narrows or not, since it
> depends on un time values, so it gives you the benefit of the doubt.

I updated the FAQ, but I actually agree with mgsergio that this behavior is
inconsistent and not at all how other -Wflags behave. The fact that we have to
do ugly kludges in the code to support this makes it only more obvious.

I'm actually going to reopen this to propose some improvements that perhaps
someone would decide to tackle as easy hacks:

* Document that -Wnarrowing also emits errors in doc/invoke.texi

* Make the message for the error and the warning different, for example:

narrowing conversion of constant expression from 'double' to 'int' inside { }

* Do not pretty-print expressions! '2.2999999999999998e+0' is very ugly and I'm
sure one can easily trick GCC to print absolute non-sense. That is, remove %qE
and use proper locations/ranges.

* Create a new function in diagnostic.c, e.g., 

extern diagnostic_t pederror (location_t, int, const char *, ...)
     ATTRIBUTE_GCC_DIAG(3,4);

and replace the kludge in cp/typeck2.c with it (possibly fixing PR69872 along
the way). Document when some warnings might be errors (like we have precise
definitions for permerror/pedwarn).


More information about the Gcc-bugs mailing list