public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/48956] New: -Wconversion should warn when a complex value is assigned to a real result
@ 2011-05-11  3:26 stevenj at alum dot mit.edu
  2011-05-11  0:11 ` [Bug c/48956] " stevenj at alum dot mit.edu
                   ` (15 more replies)
  0 siblings, 16 replies; 17+ messages in thread
From: stevenj at alum dot mit.edu @ 2011-05-11  3:26 UTC (permalink / raw)
  To: gcc-bugs

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

           Summary: -Wconversion should warn when a complex value is
                    assigned to a real result
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: stevenj@alum.mit.edu


Consider the following C snippet:

  #include <complex.h>
  double foo(complex double x) {
     return x;
  }

This is valid C code: it assigns the complex "x" value to the real result of
"foo", which implicitly returns the real part of "x".  However, the implicit
conversion from complex to real has altered a value by discarding the imaginary
part of x.  Therefore, I would expect -Wconversion to issue a warning.

Compiling this with "gcc -c -Wconversion foo.c" issues no warning, however, nor
can I find any other -W option that causes a warning to be emitted.

Please consider issuing a warning when implicitly converting complex to real
(NOT the reverse) on -Wconversion.  (In fact, -Wconversion already does this in
Fortran as noted below.)

A warning for this type of conversion would be very helpful, since silently
discarding the imaginary part in this way often indicates a bug in my
experience.  (One can come up with many similar examples, e.g. writing sqrt(x)
rather than csqrt(x) currently silently succeeds for complex x, again
discarding the imaginary part.)  If the programmer meant to take the real part,
she probably would have written "creal(x)" explicitly.

Regards,
Steven G. Johnson

PS. Note -Wconversion already DOES emit a warning for conversion to complex to
real in gfortran.  e.g.

      subroutine foo(z)
      complex z
      real x
      x = z
      write(*,*) x
      return
      end

compiles without error with gfortran -c, but gfortran -c -Wconversion emits:

      x = z                                                             
         1
Warning: Conversion from COMPLEX(4) to REAL(4) at (1)


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

end of thread, other threads:[~2015-06-24 22:13 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-11  3:26 [Bug c/48956] New: -Wconversion should warn when a complex value is assigned to a real result stevenj at alum dot mit.edu
2011-05-11  0:11 ` [Bug c/48956] " stevenj at alum dot mit.edu
2011-05-11  8:27 ` manu at gcc dot gnu.org
2011-05-11  8:50 ` manu at gcc dot gnu.org
2011-05-11 20:34 ` stevenj at alum dot mit.edu
2011-05-11 22:08 ` manu at gcc dot gnu.org
2011-05-11 22:39 ` manu at gcc dot gnu.org
2011-05-12 23:11 ` stevenj at alum dot mit.edu
2011-05-12 23:18 ` manu at gcc dot gnu.org
2012-03-24 19:15 ` manu at gcc dot gnu.org
2014-11-07 10:14 ` paolo.carlini at oracle dot com
2015-01-01 18:15 ` maltsevm at gmail dot com
2015-05-15 18:03 ` miyuki at gcc dot gnu.org
2015-05-15 18:47 ` manu at gcc dot gnu.org
2015-06-24 21:53 ` matt at bitbashing dot io
2015-06-24 22:08 ` manu at gcc dot gnu.org
2015-06-24 22:13 ` matt at bitbashing dot io

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