diff mbox series

[part1] PR fortran/49278 - ICE when combining DATA with default initialization

Message ID trinity-cddbd93e-8610-4563-9318-431617ec4006-1614636926236@3c-app-gmx-bs32
State New
Headers show
Series [part1] PR fortran/49278 - ICE when combining DATA with default initialization | expand

Commit Message

Harald Anlauf March 1, 2021, 10:15 p.m. UTC
Dear all,

the present PR has two issues.  The first one, addressed by this patch,
was about accepting invalid code where a variable appeared both in a
declaration with PARAMETER as well as in a DATA statement, which could
lead to an ICE.  We now reject this.

(There is a separate issue about combining default initialization with
DATA leading to a wrong constructor and thus wrong code.  I'd prefer to
have this addressed separately.)

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald


PR fortran/49278 - ICE when combining DATA with default initialization

A variable with the PARAMETER attribute may not appear in a DATA statement.

gcc/fortran/ChangeLog:

	PR fortran/49278
	* data.c (gfc_assign_data_value): Reject variable with PARAMETER
	attribute in DATA statement.

gcc/testsuite/ChangeLog:

	PR fortran/49278
	* gfortran.dg/parameter_data.f90: New test.
diff mbox series

Patch

diff --git a/gcc/fortran/data.c b/gcc/fortran/data.c
index 13e3506dd1e..25e97930169 100644
--- a/gcc/fortran/data.c
+++ b/gcc/fortran/data.c
@@ -244,6 +244,13 @@  gfc_assign_data_value (gfc_expr *lvalue, gfc_expr *rvalue, mpz_t index,
 		    "array-element nor a scalar-structure-component";

   symbol = lvalue->symtree->n.sym;
+  if (symbol->attr.flavor == FL_PARAMETER)
+    {
+      gfc_error ("PARAMETER %qs shall not appear in a DATA statement at %L",
+		 symbol->name, &lvalue->where);
+      return false;
+    }
+
   init = symbol->value;
   last_ts = &symbol->ts;
   last_con = NULL;
diff --git a/gcc/testsuite/gfortran.dg/parameter_data.f90 b/gcc/testsuite/gfortran.dg/parameter_data.f90
new file mode 100644
index 00000000000..b95f9c90696
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/parameter_data.f90
@@ -0,0 +1,13 @@ 
+! { dg-do compile }
+! PR fortran/49278 - ICE when combining DATA with default initialization
+
+program p
+  implicit none
+  type t
+     real :: a
+  end type t
+  integer, parameter :: b = 42
+  type(t), parameter :: z = t(4.0)
+  data b   / 666 / ! { dg-error "shall not appear in a DATA statement" }
+  data z%a / 3.0 / ! { dg-error "shall not appear in a DATA statement" }
+end