Bug 41719 - [OOP] invalid: Intrinsic assignment involving polymorphic variables
[OOP] invalid: Intrinsic assignment involving polymorphic variables
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
4.5.0
: P3 normal
: ---
Assigned To: janus
: accepts-invalid
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2009-10-15 20:13 UTC by Tobias Burnus
Modified: 2009-10-16 21:12 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-10-16 21:11:39


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2009-10-15 20:13:23 UTC
R734 assignment-stmt is variable = expr

"An intrinsic assignment statement is an assignment statement that is not a defined assignment statement (7.4.1.4). In an intrinsic assignment statement, variable shall not be polymorphic"



 implicit none

 type t1
   integer :: a
 end type

 type, extends(t1) :: t2
   integer :: b
 end type

 class(t1),pointer :: cp
 type(t2) :: x

 x = t2(45,478) ! INVALID
 allocate(t2 :: cp)
 cp = x
 select type (cp)
   type is (t2)
     print *, cp%a, cp%b
 end select
end
Comment 1 Tobias Burnus 2009-10-15 20:16:09 UTC
(In reply to comment #0)
>  x = t2(45,478) ! INVALID
>  cp = x

The "x = " is of cause valid while the "cp = x" is invalid.

The opposite "type = class" is already rejected.
Comment 2 janus 2009-10-16 18:44:14 UTC
Preliminary patch:

Index: gcc/fortran/resolve.c
===================================================================
--- gcc/fortran/resolve.c	(Revision 152915)
+++ gcc/fortran/resolve.c	(Arbeitskopie)
@@ -7629,6 +7629,14 @@ resolve_ordinary_assign (gfc_code *code, gfc_names
 	}
     }
 
+  /* F03:7.4.1.2.  */
+  if (lhs->ts.type == BT_CLASS)
+    {
+      gfc_error ("Variable must not be polymorphic in assignment at %L",
+		 &lhs->where);
+      return false;
+    }
+
   gfc_check_assign (lhs, rhs, 1);
   return false;
 }
Comment 3 janus 2009-10-16 18:55:46 UTC
Actually the following two test cases are invalid according to this PR:

typebound_operator_2.f03
typebound_operator_4.f03

Both include an intrinsic assignment with a polymorphic (dummy) variable.
Comment 4 janus 2009-10-16 19:10:48 UTC
Note: It seems this will be legal again in F08.


7.2.1.2 Intrinsic assignment statement
 An intrinsic assignment statement is an assignment statement that is not a defined assignment statement (7.2.1.4).
In an intrinsic assignment statement,
(1) if the variable is polymorphic it shall be allocatable and not a coarray,
...
(4) if the variable is polymorphic it shall be type compatible with expr ; otherwise the declared types of the variable and expr shall conform as specified in Table 7.10,
...
Comment 5 janus 2009-10-16 19:19:42 UTC
(In reply to comment #4)
> Note: It seems this will be legal again in F08.

That is: for certain cases (ALLOCATABLE). The example in comment #0 is still illegal.
Comment 6 Tobias Burnus 2009-10-16 20:25:59 UTC
(In reply to comment #5)
> (In reply to comment #4)
> > Note: It seems this will be legal again in F08.
> 
> That is: for certain cases (ALLOCATABLE). The example in comment #0 is still
> illegal.

"if the variable is polymorphic it shall be type compatible with expr"

That's obvious. If I have "CLASS(t) :: c", I cannot do an intrinsic assignment such as "c = .true." - or  "c = t2" (assuming that "t2" does not extend "t").


Regarding:
"if the variable is polymorphic it shall be allocatable and not a coarray,"

Allocatables are special as for  "A = B", A is (re)allocated on the fly iff A is unallocated or the shape/length type parameters does not match the RHS. (If they do match, no reallocation is happening - which matters if "A" has the TARGET attribute and is associated with a pointer.)
Cf. "7.2.1.3 Interpretation of intrinsic assignments".

I suggest to ignore F2008 and defer it until we have the realloc on assignment implemented. Cf. PR 35810.
Comment 7 janus 2009-10-16 21:10:56 UTC
Subject: Bug 41719

Author: janus
Date: Fri Oct 16 21:10:43 2009
New Revision: 152919

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=152919
Log:
2009-10-16  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/41719
	* resolve.c (resolve_ordinary_assign): Reject intrinsic assignments
	to polymorphic variables.


2009-10-16  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/41719
	* gfortran.dg/class_5.f03: New test case.
	* gfortran.dg/typebound_operator_2.f03: Fixing invalid test case.
	* gfortran.dg/typebound_operator_4.f03: Ditto.

Added:
    trunk/gcc/testsuite/gfortran.dg/class_5.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/resolve.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/typebound_operator_2.f03
    trunk/gcc/testsuite/gfortran.dg/typebound_operator_4.f03

Comment 8 janus 2009-10-16 21:12:10 UTC
Fixed with r152919. Closing.