This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/48956] New: -Wconversion should warn when a complex value is assigned to a real result
- From: "stevenj at alum dot mit.edu" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 11 May 2011 00:10:51 +0000
- Subject: [Bug c/48956] New: -Wconversion should warn when a complex value is assigned to a real result
- Auto-submitted: auto-generated
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)