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

Tobias Burnus <burnus at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |burnus at gcc dot gnu.org

--- Comment #3 from Tobias Burnus <burnus at gcc dot gnu.org> 2012-08-12 
19:21:41 UTC ---
(In reply to comment #1)
> The Fortran standard explicitly states that CMPLX returns
> a complex number with default real kind if the KIND= optional
> argument is not used.

Well, if it weren't valid, gfortran shouldn't warn but print an error. I
recently only found a bug in our program because of a warning about assigning
an REAL to an INTEGER, which should have been a REAL – thus, such warnings can
be helpful.


(In reply to comment #2)
> This is certainly a mistake made by lots of people,
> so I think a warning would be appropriate.

That's also the reason for suggesting this. The question is whether
-Wconversion (which is enabled by -Wall) or -Wconversion-extra (which is hidden
in the manual) is the better choice. It seems to be a somewhat common problem.
The case that one wants to have the default kind but uses a higher-precision
argument, seems to be rather rare; hence, I am inclined to warn already with
-Wconversion/-Wall.

(Side note: REAL always confuses me – for a complex number it returns the kind
of the argument - but for integer/real the default-kind.)


Untested patch – for -Wconversion-extra
--- a/gcc/fortran/check.c
+++ b/gcc/fortran/check.c
@@ -1278,6 +1278,17 @@ gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr
*kind)
   if (kind_check (kind, 2, BT_COMPLEX) == FAILURE)
     return FAILURE;

+  if (y && !kind && gfc_option.warn_conversion_extra
+      && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
+    gfc_warning_now ("Conversion from %s to default-kind COMPLEX(%d) at %L "
+                    "might loose precision, consider using the KIND argument",
+                    gfc_typename (&x->ts), gfc_default_real_kind, &x->where);
+  else if (y && !kind && gfc_option.warn_conversion_extra
+          && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
+    gfc_warning_now ("Conversion from %s to default-kind COMPLEX(%d) at %L "
+                    "might loose precision, consider using the KIND argument",
+                    gfc_typename (&y->ts), gfc_default_real_kind, &y->where);
+
   return SUCCESS;
 }

Reply via email to