Bug 1134560

Summary: Component declarations overwrite types of Cray Pointee variables
Product: Red Hat Enterprise Linux 6 Reporter: Fritz Reese <fritz>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED ERRATA QA Contact: Miroslav Franc <mfranc>
Severity: medium Docs Contact:
Priority: unspecified    
Version: 6.4CC: fritz, mcermak, mfranc, mnewsome, mpolacek, ohudlick
Target Milestone: rc   
Target Release: ---   
Hardware: x86_64   
OS: Linux   
URL: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62174
Whiteboard:
Fixed In Version: gcc-4.4.7-12.el6 Doc Type: Bug Fix
Doc Text:
In previous versions of the GNU Fortran compiler, the type specifiers for Cray pointees were incorrectly overwritten by the type specifiers of components with the same name. Consequently, compiling failed with an error message. This bug has been fixed, and the Cray pointers are now handled correctly.
Story Points: ---
Clone Of: Environment:
Last Closed: 2015-07-22 06:48:16 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
Patch and testcase none

Description Fritz Reese 2014-08-27 19:07:49 UTC
User-Agent:       Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0
Build Identifier: 

The typespecs for Cray pointees are overwritten by the typespecs of components with the same name which are declared later. The problem is present as far as I can tell from gcc-4.1.0 all the way up through gcc-4.8.3.

Reproducible: Always

Steps to Reproduce:
Example:

type t1
  integer i
end type t1
type(t1) x

pointer (x_ptr, x)

type t2
  real x ! this declaration overwrites x's type
end type t2

x%i = 0 ! this causes an error; the compiler thinks x is a real

end
Actual Results:  
Running gfortran-4.4.7 shipped with RH6.4:

$ gfortran test.f90
test.f90:12.2:

x%i = 0 ! this causes an error; the compiler thinks x is a real
  1
Error: Unexpected '%' for nonderived-type variable 'x' at (1)


Expected Results:  
There should be no compile errors.

Submitted the bug report to GNU (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=62174) but have not heard back. I have a patch which I will attach.

Comment 2 Fritz Reese 2014-08-27 19:13:52 UTC
Created attachment 931658 [details]
Patch and testcase

Here is a proposed patch and testcase. When variable_decl() finds a declaration which shares a name with a Cray pointee, it should not care if the declaration is actually a component declaration.

2014-08-18  Fritz Reese  <Reese-Fritz>

	* decl.c (variable_decl): Don't overwrite typespecs of Cray pointees
	when matching a component declaration.


diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c
index 4048ac9..7b3c59a 100644
--- a/gcc/fortran/decl.c
+++ b/gcc/fortran/decl.c
@@ -1904,8 +1904,9 @@ variable_decl (int elem)
     }
 
   /*  If this symbol has already shown up in a Cray Pointer declaration,
+      and this is not a component declaration,
       then we want to set the type & bail out.  */
-  if (gfc_option.flag_cray_pointer)
+  if (gfc_option.flag_cray_pointer && gfc_current_state () != COMP_DERIVED)
     {
       gfc_find_symbol (name, gfc_current_ns, 1, &sym);
       if (sym != NULL && sym->attr.cray_pointee)
diff --git a/gcc/testsuite/gfortran.dg/cray_pointers_10.f90 b/gcc/testsuite/gfortran.dg/cray_pointers_10.f90
new file mode 100644
index 0000000..fcc0132
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/cray_pointers_10.f90
@@ -0,0 +1,22 @@
+! { dg-do compile }
+! { dg-options "-fcray-pointer" }
+!
+! Since the introduction of Cray pointers in 4.1.0 as late as 4.8.3,
+! component declarations within derived types would overwrite the typespec of 
+! variables with the same name who were Cray pointees.
+implicit none
+
+type t1
+  integer i
+end type t1
+type(t1) x
+
+pointer (x_ptr, x)
+
+type t2
+  real x ! should not overwrite x's type
+end type t2
+
+x%i = 0 ! should see no error here
+
+end

Comment 9 errata-xmlrpc 2015-07-22 06:48:16 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://rhn.redhat.com/errata/RHBA-2015-1339.html