diff mbox

[fortran] Warn about integer**(negative integer)

Message ID f1212af8-c5a4-ae0e-247e-df273784a991@netcologne.de
State New
Headers show

Commit Message

Thomas Koenig Jan. 22, 2017, 9:47 a.m. UTC
Hello world,

a reacent thread on c.l.f showed another common error - expecting
10**(-3) to expect something different from zero.  The attached
patch warns about this result if -Winteger-division is active.

Why -Winteger-division?  Two reasons:  First, 10**(-3) ist just
a shorthand for integer division.  Second, I did not want to add
yet another warning flag.

Regression-tested.  OK for trunk?

	Thomas

2017-01-22  Thomas Koenig  <tkoenig@netcologne.de>

         * arith.c (arith_power):  If simplifying integer power expression
         to zero, warn if -Winteger-division is given.

2017-01-22  Thomas Koenig  <tkoenig@netcologne.de>

         * gfortran.dg/integer_exponentiation_7.f90:  New test.

Comments

FX Coudert Jan. 22, 2017, 9:26 p.m. UTC | #1
> 2017-01-22  Thomas Koenig  <tkoenig@netcologne.de>
> 
>        * arith.c (arith_power):  If simplifying integer power expression
>        to zero, warn if -Winteger-division is given.
> 
> 2017-01-22  Thomas Koenig  <tkoenig@netcologne.de>
> 
>        * gfortran.dg/integer_exponentiation_7.f90:  New test.

The idea seems reasonable, but I am not sure about the wording (“truncated” in particular). “Integer exponentiation truncated to constant 0”? Why not “Negative integer exponent has result value zero”?

Other than that, OK to commit.

FX
diff mbox

Patch

Index: arith.c
===================================================================
--- arith.c	(Revision 244747)
+++ arith.c	(Arbeitskopie)
@@ -874,6 +874,11 @@  arith_power (gfc_expr *op1, gfc_expr *op2, gfc_exp
 		  {
 		    /* if op2 < 0, op1**op2 == 0  because abs(op1) > 1.  */
 		    mpz_set_si (result->value.integer, 0);
+		    if (warn_integer_division)
+		      gfc_warning_now (OPT_Winteger_division, "Integer "
+				       "exponentiation truncated to constant "
+				       "%qs at %L", "0", &result->where);
+				       
 		  }
 		else if (gfc_extract_int (op2, &power))
 		  {