diff mbox

[Fortran] Add diagnostic / check for passing coarrays to allocatable dummies

Message ID 4DE12CB7.2080601@net-b.de
State New
Headers show

Commit Message

Tobias Burnus May 28, 2011, 5:11 p.m. UTC
Allocatable coarrays have to be (de)allocated collectively. Thus, it is 
doubtful if one passes a coarray as actual argument to an allocatable 
dummy argument, which is not a coarray (unless the dummy is INTENT(IN)). 
However, as long as the allocation status is not modified, the code is 
valid.

If the dummy is INTENT(OUT), we know that the allocation status is 
modified - thus, we know the code is invalid.

OK for the trunk?

Tobias

Comments

Steve Kargl May 29, 2011, 5:22 p.m. UTC | #1
On Sat, May 28, 2011 at 07:11:19PM +0200, Tobias Burnus wrote:
> Allocatable coarrays have to be (de)allocated collectively. Thus, it is 
> doubtful if one passes a coarray as actual argument to an allocatable 
> dummy argument, which is not a coarray (unless the dummy is INTENT(IN)). 
> However, as long as the allocation status is not modified, the code is 
> valid.
> 
> If the dummy is INTENT(OUT), we know that the allocation status is 
> modified - thus, we know the code is invalid.
> 
> OK for the trunk?
> 

OK.
diff mbox

Patch

2011-05-28  Tobias Burnus  <burnus@net-b.de>

	PR fortran/18918
	* interface.c (compare_parameter): Add check for passing coarray
	to allocatable noncoarray dummy.

2011-05-28  Tobias Burnus  <burnus@net-b.de>

	PR fortran/18918
	* gfortran.dg/coarray_24.f90: New.

diff --git a/gcc/fortran/interface.c b/gcc/fortran/interface.c
index 732a0c5..6575fbe 100644
--- a/gcc/fortran/interface.c
+++ b/gcc/fortran/interface.c
@@ -1645,6 +1645,24 @@  compare_parameter (gfc_symbol *formal, gfc_expr *actual,
       return 0;
     }
 
+  if (formal->attr.allocatable && !formal->attr.codimension
+      && gfc_expr_attr (actual).codimension)
+    {
+      if (formal->attr.intent == INTENT_OUT)
+	{
+	  if (where)
+	    gfc_error ("Passing coarray at %L to allocatable, noncoarray, "
+		       "INTENT(OUT) dummy argument '%s'", &actual->where,
+		       formal->name);
+	    return 0;
+	}
+      else if (gfc_option.warn_surprising && where
+	       && formal->attr.intent != INTENT_IN)
+	gfc_warning ("Passing coarray at %L to allocatable, noncoarray dummy "
+		     "argument '%s', which is invalid if the allocation status"
+		     " is modified",  &actual->where, formal->name);
+    }
+
   if (symbol_rank (formal) == actual->rank)
     return 1;
 
--- /dev/null	2011-05-27 20:00:22.465849134 +0200
+++ gcc/gcc/testsuite/gfortran.dg/coarray_24.f90	2011-05-28 18:55:28.000000000 +0200
@@ -0,0 +1,26 @@ 
+! { dg-do compile }
+! { dg-options "-fcoarray=single -Wall" }
+!
+! This program is perfectly valid; however, passing an (allocatable) coarray
+! as actual argument to a non-coarray allocatable dummy is doubtful as
+! reallocation is not allowed. Thus, an intent(out) dummy should be always
+! wrong.
+!
+
+integer, allocatable :: myCaf(:)[:]
+
+allocate(myCaf(1)[*])
+
+call doubtful_valid(myCaf)  ! { dg-warning "to allocatable, noncoarray dummy" }
+call invalid(myCaf)         ! { dg-error "to allocatable, noncoarray, INTENT.OUT. dummy" }
+contains
+  subroutine doubtful_valid(x)
+    integer, allocatable :: x(:)
+    ! Valid as x's allocation status is not touched.
+    x(1) = 7
+  end subroutine doubtful_valid
+  subroutine invalid(y)
+    integer, allocatable, intent(out) :: y(:)
+    allocate (y(1))
+  end subroutine invalid
+end