diff mbox

fortran/67802 -- Numeric constant character length must ...

Message ID 20151001185454.GA7728@troutmask.apl.washington.edu
State New
Headers show

Commit Message

Steve Kargl Oct. 1, 2015, 6:54 p.m. UTC
be an integer expression.

The attached patch checks that a numerical constant in
a CHARACTER(LEN=x) declaration is an integer.  Regression
tested on x86_64-*-freebsd*.  OK to commit?

2015-10-01  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/67802
	* decl.c (add_init_expr_to_sym): Numeric constant for character
	length must be an INTEGER.

2015-10-01  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/67802
	* gfortran.dg/pr67802.f90: New test.

Comments

FX Coudert Oct. 1, 2015, 7:19 p.m. UTC | #1
> 2015-10-01  Steven G. Kargl  <kargl@gcc.gnu.org>
> 
> 	PR fortran/67802
> 	* decl.c (add_init_expr_to_sym): Numeric constant for character
> 	length must be an INTEGER.
> 
> 2015-10-01  Steven G. Kargl  <kargl@gcc.gnu.org>
> 
> 	PR fortran/67802
> 	* gfortran.dg/pr67802.f90: New test.

OK, but not with that error message. We currently don’t use the “shorthand” standard notation (like "scalar-int-expr”) in our error messages, and we should keep that consistent. So I would go with “character length should be of integer type” or something like that.

FX
Steve Kargl Oct. 1, 2015, 7:45 p.m. UTC | #2
On Thu, Oct 01, 2015 at 09:19:15PM +0200, FX wrote:
> > 2015-10-01  Steven G. Kargl  <kargl@gcc.gnu.org>
> > 
> > 	PR fortran/67802
> > 	* decl.c (add_init_expr_to_sym): Numeric constant for character
> > 	length must be an INTEGER.
> > 
> > 2015-10-01  Steven G. Kargl  <kargl@gcc.gnu.org>
> > 
> > 	PR fortran/67802
> > 	* gfortran.dg/pr67802.f90: New test.
> 
> OK, but not with that error message. We currently don???t use
> the ???shorthand??? standard notation (like "scalar-int-expr???)
> in our error messages, and we should keep that consistent.
> So I would go with ???character length should be of integer
> type??? or something like that.
> 

Well, ahem, gfortran does have several error messages that use the
standard notation.  I know.  I wrote some of them. :-)

I'll simply change it to "Expecting an INTEGER at %L"
FX Coudert Oct. 1, 2015, 8:49 p.m. UTC | #3
> Well, ahem, gfortran does have several error messages that use the
> standard notation.  I know.  I wrote some of them. :-)
> 
> I'll simply change it to "Expecting an INTEGER at %L”

Thanks. I have no objections to using the full standard terminology (scalar integer expression), but not the shorthand (scalar-int-expr) which few people outside the language lawyers know :)

FX
diff mbox

Patch

Index: fortran/decl.c
===================================================================
--- fortran/decl.c	(revision 228306)
+++ fortran/decl.c	(working copy)
@@ -1439,7 +1439,16 @@  add_init_expr_to_sym (const char *name, 
 	  /* Update initializer character length according symbol.  */
 	  else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
 	    {
-	      int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
+	      int len;
+
+	      if (sym->ts.u.cl->length->ts.type != BT_INTEGER)
+		{
+		  gfc_error ("Expecting an scalar-int-expr at %L",
+			     &sym->ts.u.cl->length->where);
+		  return false;
+		}
+
+	      len = mpz_get_si (sym->ts.u.cl->length->value.integer);
 
 	      if (init->expr_type == EXPR_CONSTANT)
 		gfc_set_constant_character_len (len, init, -1);
Index: testsuite/gfortran.dg/pr67802.f90
===================================================================
--- testsuite/gfortran.dg/pr67802.f90	(revision 0)
+++ testsuite/gfortran.dg/pr67802.f90	(working copy)
@@ -0,0 +1,9 @@ 
+! { dg-do compile }
+! PR fortran/67802
+! Original code contribute by gerhard.steinmetz.fortran at t-online.de
+program p
+   character(1.) :: c1 = ' '      ! { dg-error "Expecting an scalar-int-expr" }
+   character(1d1) :: c2 = ' '     ! { dg-error "Expecting an scalar-int-expr" }
+   character((0.,1.)) :: c3 = ' ' ! { dg-error "Expecting an scalar-int-expr" }
+   character(.true.) :: c4 = ' '  ! { dg-error "Expecting an scalar-int-expr" }
+end program p