Bug 39688 - IMPORT of derived type fails
Summary: IMPORT of derived type fails
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.4.0
: P3 normal
Target Milestone: ---
Assignee: janus
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2009-04-08 12:46 UTC by Tobias Burnus
Modified: 2009-04-25 08:13 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-04-24 19:20:15


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2009-04-08 12:46:56 UTC
Reported by Bob Corbett of SUN to the J3 mailing list (who was initially not fully convinced that the code is valid). The following program is regarded as valid but it fails with sunf95 and gfortran (4.3/4.4/4.5, no IMPORT exists in 4.2) to compile. (It works with NAG f95, ifort, g95, xlf90.) gfortran's error message is:

             TYPE(T3) X
                       1
Error: the type of 'x' at (1) has not been declared within the interface


"The problem, of course, is the definition of the derived
 type T2 following the interface block.  Section 12.3.2.1
 of the Fortran 2003 standard says regarding the IMPORT
 statement

      If an entity that is made accessible by this means
      is accessed by host association and is defined in
      the host scoping unit, it shall be explicitly
      declared prior to the interface body.

 That clearly requires T1 and T3 to be defined prior to the
 interface body, but it does not require T2 to be defined
 prior to the interface body.  Therefore, the program appears
 to be standard conforming."


       MODULE MOD
         TYPE T1
           SEQUENCE
           TYPE(T2), POINTER :: P
         END TYPE
         TYPE T2
           SEQUENCE
           INTEGER I
         END TYPE
       END

       PROGRAM MAIN
         USE MOD, T3 => T1, T4 => T2
         TYPE T1
           SEQUENCE
           TYPE(T2), POINTER :: P
         END TYPE
         INTERFACE SUBR
           SUBROUTINE SUBR1(X)
             IMPORT T3
             TYPE(T3) X
           END SUBROUTINE
           SUBROUTINE SUBR2(X)
             IMPORT T1
             TYPE(T1) X
           END SUBROUTINE
         END INTERFACE
         TYPE T2
           SEQUENCE
           REAL X
         END TYPE
       END

       SUBROUTINE SUBR1(X)
         USE MOD
         TYPE(T1) X
       END

       SUBROUTINE SUBR2(X)
         TYPE T1
           SEQUENCE
           TYPE(T2), POINTER :: P
         END TYPE
         TYPE T2
           SEQUENCE
           REAL X
         END TYPE
         TYPE(T1) X
       END
Comment 1 janus 2009-04-24 17:59:23 UTC
Confirmed. Here is a reduced test case, which does not have the problem with T2 discussed in comment #0, but still fails with the same error message:

MODULE MOD
  TYPE T1
    TYPE(T1), POINTER :: P
  END TYPE
END

PROGRAM MAIN
  USE MOD, T3 => T1
  INTERFACE SUBR
    SUBROUTINE SUBR1(X)
      IMPORT T3
      TYPE(T3) X
    END SUBROUTINE
  END INTERFACE
END

The error only appears if the imported type is renamed in the USE statement.
Comment 2 janus 2009-04-24 19:20:15 UTC
Mine. The fix is completely trivial:

Index: gcc/fortran/decl.c
===================================================================
--- gcc/fortran/decl.c	(Revision 146676)
+++ gcc/fortran/decl.c	(Arbeitskopie)
@@ -2741,7 +2741,7 @@
 	      goto next_item;
 	    }
 
-	  st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
+	  st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
 	  st->n.sym = sym;
 	  sym->refs++;
 	  sym->attr.imported = 1;
Comment 3 janus 2009-04-25 08:12:05 UTC
Subject: Bug 39688

Author: janus
Date: Sat Apr 25 08:11:48 2009
New Revision: 146762

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

	PR fortran/39688
	* decl.c (gfc_match_import): Use 'sym->name' instead of 'name'.
	They differ if the symbol has been use-renamed.


2009-04-25  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/39688
	* gfortran.dg/import7.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/import7.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/decl.c
    trunk/gcc/testsuite/ChangeLog

Comment 4 janus 2009-04-25 08:13:46 UTC
Fixed in r146762. Closing.