This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/48956] New: -Wconversion should warn when a complex value is assigned to a real result


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)


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]