Fortran : accepts pointer initialization of DT dummy args PR45337
authorMark Eggleston <markeggleston@gcc.gnu.org>
Wed, 10 Jun 2020 06:22:50 +0000 (07:22 +0100)
committerMark Eggleston <markeggleston@gcc.gnu.org>
Mon, 13 Jul 2020 15:38:07 +0000 (16:38 +0100)
Initialisation of a variable results in an implicit save attribute
being added to the variable.  The save attribute is not allowed for
variables with the dummy attribute set.  Initialisation should be
rejected for dummy variables.

2020-07-13  Mark Eggleston  <markeggleston@gcc.gnu.org>

gcc/fortran/

PR fortran/45337
* resolve.c (resolve_fl_variable): Remove type and intent
checks from the check for dummy.

2020-07-13  Mark Eggleston  <markeggleston@gcc.gnu.org>

gcc/testsuite/

PR fortran/45337
* gfortran.dg/pr45337_1.f90: New test.
* gfortran.dg/pr45337_2.f90: New test.

gcc/fortran/resolve.c
gcc/testsuite/gfortran.dg/pr45337_1.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/pr45337_2.f90 [new file with mode: 0644]

index b1238c8..2751c0c 100644 (file)
@@ -12927,8 +12927,7 @@ resolve_fl_variable (gfc_symbol *sym, int mp_flag)
       else if (sym->attr.external)
        gfc_error ("External %qs at %L cannot have an initializer",
                   sym->name, &sym->declared_at);
-      else if (sym->attr.dummy
-       && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
+      else if (sym->attr.dummy)
        gfc_error ("Dummy %qs at %L cannot have an initializer",
                   sym->name, &sym->declared_at);
       else if (sym->attr.intrinsic)
diff --git a/gcc/testsuite/gfortran.dg/pr45337_1.f90 b/gcc/testsuite/gfortran.dg/pr45337_1.f90
new file mode 100644 (file)
index 0000000..2bb8ff2
--- /dev/null
@@ -0,0 +1,14 @@
+! { dg-do compile }
+
+module ptrmod
+contains
+subroutine lengthX(x, i) ! { dg-error "Dummy 'x' at .1. cannot have an initializer" }
+   implicit none
+   real, pointer, intent(out) :: x(:)=>null()
+   integer :: i
+   x=>null()
+   allocate(x(i))
+   x=i
+end subroutine
+end module
+
diff --git a/gcc/testsuite/gfortran.dg/pr45337_2.f90 b/gcc/testsuite/gfortran.dg/pr45337_2.f90
new file mode 100644 (file)
index 0000000..ca7a6f5
--- /dev/null
@@ -0,0 +1,18 @@
+! { dg-do compile }
+
+type t
+end type t
+type t2
+  integer :: j = 7
+end type t2
+contains
+  subroutine x(a, b, c)
+    intent(out) :: a, b, c
+    type(t) :: a = t()
+    type(t2) :: b = t2()
+    type(t2) :: c
+  end subroutine x
+end
+
+! { dg-error "Dummy .a. at .1. cannot have an initializer" " " { target *-*-* } 9 }
+! { dg-error "Dummy .b. at .1. cannot have an initializer" " " { target *-*-* } 9 }