Bug 38152 - [4.4 Regression] procedure pointers as module variables
[4.4 Regression] procedure pointers as module variables
Status: RESOLVED FIXED
Product: gcc
Classification: Unclassified
Component: fortran
4.4.0
: P3 normal
: 4.4.0
Assigned To: janus
: ice-on-valid-code
: 38831 (view as bug list)
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2008-11-16 09:26 UTC by Daniel Kraft
Modified: 2009-04-08 14:11 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2008-11-16 18:59:41


Attachments
Another test case (289 bytes, text/plain)
2009-01-13 19:47 UTC, Daniel Kraft
Details
patch (2.29 KB, patch)
2009-01-15 22:56 UTC, janus
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Kraft 2008-11-16 09:26:39 UTC
The following (possibly invalid) program ICEs:

MODULE m
  IMPLICIT NONE

  PROCEDURE(test), POINTER :: procptr

CONTAINS

  SUBROUTINE test ()
    IMPLICIT NONE

    CALL bar (test)
    procptr => test
  END SUBROUTINE test

END MODULE m

The ICE is however not related to the missing RECURSIVE and can be reproduced with valid code, see below.
Comment 1 Daniel Kraft 2008-11-16 09:27:11 UTC
Daniel Kraft wrote:
> > I'm working out a test-case for PR 37779 and came across the following
> > program, which ICEs for today's trunk gfortran:
> >
> >   SUBROUTINE test ()
> >     IMPLICIT NONE
> >     procptr => test
If one moves the procedure-pointer declaration in the subroutine, it
works. The difference of the -fdump-tree-original/gimple is:

+   void (*<T62>) (void) procptr;

The failing assert in make_decl_rtl is:

  /* A weak alias has TREE_PUBLIC set but not the other bits.  */
  gcc_assert (TREE_CODE (decl) != VAR_DECL
              || TREE_STATIC (decl)
              || TREE_PUBLIC (decl)
              || DECL_EXTERNAL (decl)
              || DECL_REGISTER (decl));


> > Is this already a known bug?  Sorry that I don't have an overview... 
> > If not, I'll file a report.
Please do so, I think it is not known.


By the way, the following also fails:

module m
procedure(), pointer :: procptr
end module m

use m
external foo
procptr => foo
end

with Error: 'procptr' in the pointer assignment at (1) cannot be an
l-value since it is a procedure

I think the "proc_ptr" attribute is not properly saved in the .mod file.


Tobias
Comment 2 janus 2008-11-16 18:59:41 UTC
(In reply to comment #1)
> By the way, the following also fails:
> 
> module m
> procedure(), pointer :: procptr
> end module m
> 
> use m
> external foo
> procptr => foo
> end
> 
> with Error: 'procptr' in the pointer assignment at (1) cannot be an
> l-value since it is a procedure
> 
> I think the "proc_ptr" attribute is not properly saved in the .mod file.

Actually this is not the problem. All attributes are saved properly.
The error is fixed by the following trivial patch:

Index: gcc/fortran/expr.c
===================================================================
--- gcc/fortran/expr.c	(revision 141915)
+++ gcc/fortran/expr.c	(working copy)
@@ -3034,7 +3034,8 @@ gfc_check_pointer_assign (gfc_expr *lval
     }
 
   if (lvalue->symtree->n.sym->attr.flavor == FL_PROCEDURE
-      && lvalue->symtree->n.sym->attr.use_assoc)
+      && lvalue->symtree->n.sym->attr.use_assoc
+      && !lvalue->symtree->n.sym->attr.proc_pointer)
     {
       gfc_error ("'%s' in the pointer assignment at %L cannot be an "
 		 "l-value since it is a procedure",
Comment 3 Tobias Burnus 2008-11-17 00:01:33 UTC
(In reply to comment #2)
> > By the way, the following also fails:
> The error is fixed by the following trivial patch:

Janus, do you plan to submit it?

  * * *

Regarding the ICE, the following patch seems to work; one probably still needs to take care of mangling of the name etc. Also there needs to be a check for use-/host- association (cf. a few lines down and in gfc_finish_var_decl), adding those flags unconditionally is surely wrong.

--- trans-decl.c        (revision 141928)
+++ trans-decl.c        (working copy)
@@ -1021,2 +1021,7 @@ gfc_get_symbol_decl (gfc_symbol * sym)
       decl = gfc_get_extern_function_decl (sym);
+      if (sym->attr.proc_pointer)
+       {
+         TREE_PUBLIC (decl) = 1;
+         TREE_STATIC (decl) = 1;
+        }
       return decl;
Comment 4 Dominique d'Humieres 2008-11-18 19:54:06 UTC
With the patches in comment #2 and #3, compiling the test in comment #0 on i686-apple-darwin9 in 32 bit mode gives:

/var/tmp//ccMx60VC.s:13:non-relocatable subtraction expression, "_procptr" minus "L00000000001$pb"
/var/tmp//ccMx60VC.s:13:symbol: "_procptr" can't be undefined in a subtraction expression

Note that g95 compiles the test while ifort 11 returns:

pr38152.f90(4): error #8169: The specified interface is not declared.   [TEST]
  PROCEDURE(test), POINTER :: procptr
------------^
pr38152.f90(11): error #6437: A subroutine or function is calling itself recursively.   [TEST]
    CALL bar (test)
--------------^
pr38152.f90(12): error #8191: The procedure target must be a procedure or a procedure pointer.   [TEST]
    procptr => test
---------------^
compilation aborted for pr38152.f90 (code 1)

Comment 5 Dominique d'Humieres 2008-11-19 16:29:58 UTC
On powerpc-apple-darwin9 I have the same kind of error as reported in comment #4:

/var/folders/FK/FKCVPmNbH5SNynFQmqGomk+++TI/-Tmp-//cctJDhJw.s:21:non-relocatable subtraction expression, "_procptr" minus "L00000000001$pb"
/var/folders/FK/FKCVPmNbH5SNynFQmqGomk+++TI/-Tmp-//cctJDhJw.s:21:symbol: "_procptr" can't be undefined in a subtraction expression
/var/folders/FK/FKCVPmNbH5SNynFQmqGomk+++TI/-Tmp-//cctJDhJw.s:19:non-relocatable subtraction expression, "_procptr" minus "L00000000001$pb"
/var/folders/FK/FKCVPmNbH5SNynFQmqGomk+++TI/-Tmp-//cctJDhJw.s:19:symbol: "_procptr" can't be undefined in a subtraction expression

in this case for 32 and 64 bit modes. The errors disappear if I compile with -fno-PIC (ppc/intel).

Comment 6 Tobias Burnus 2008-11-22 21:42:33 UTC
See also test cases at:
http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/e0d04d755453a2a5
Comment 7 Daniel Kraft 2009-01-13 19:47:22 UTC
Created attachment 17090 [details]
Another test case

This seems to be yet another test triggering this ICE.
Comment 8 janus 2009-01-15 22:56:44 UTC
Created attachment 17110 [details]
patch

Here is a patch which fixes the testcases in comment #0, #1 and #7.

Dominique: Maybe you could check whether the problems from comment #4 and #5 still persist.

Can we still get this into 4.4? In a way the ICE is a regression, since 4.3 just gave an error message (stating that procedure pointers are not implemented) but no ICE.
Comment 9 Tobias Burnus 2009-01-16 06:50:11 UTC
> Can we still get this into 4.4? In a way the ICE is a regression, since 4.3
> just gave an error message (stating that procedure pointers are not
> implemented) but no ICE.

I think one could; the patch only affects proc pointer and thus even if it breaks, it would not be that bad. Note: There are currently only 102 serious regressions left (6 P1 regressions; goal < 100 P1-P3 regressions, 0 P1 regressions) thus branching will be relatively soon.
Comment 10 janus 2009-01-16 12:04:03 UTC
Subject: Bug 38152

Author: janus
Date: Fri Jan 16 12:03:51 2009
New Revision: 143430

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

	PR fortran/38152
	* expr.c (gfc_check_pointer_assign): Allow use-associated procedure
	pointers as lvalue.
	* trans-decl.c (get_proc_pointer_decl,gfc_create_module_variable):
	Enable procedure pointers as module variables.


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

	PR fortran/38152
	* gfortran.dg/proc_ptr_13.f90: New.

Added:
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_13.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/testsuite/ChangeLog

Comment 11 janus 2009-01-16 12:30:51 UTC
This PR can be closed, provided there are no remaining issues on darwin9 (see comment #4 and #5). I cannot check this myself (since I don't have access to a darwin system), but maybe Dominique can?
Comment 12 Dominique d'Humieres 2009-01-17 13:20:28 UTC
(In reply to comment #11)
> This PR can be closed, provided there are no remaining issues on darwin9 (see
> comment #4 and #5). I cannot check this myself (since I don't have access to a
> darwin system), but maybe Dominique can?

AFAICT all the tests I have run passed, so this PR can probably be closed. If I see anything I'll reopen it and report.  Thanks for the patch.
Comment 13 Tobias Burnus 2009-01-17 13:25:08 UTC
Close as FIXED based on Dominique's remark.
Comment 14 Daniel Kraft 2009-01-25 07:47:10 UTC
*** Bug 38831 has been marked as a duplicate of this bug. ***
Comment 15 janus 2009-04-07 11:40:34 UTC
Reopening.

proc_decl_13.f90 produces a segfault when compiled with "-g".

Backtrace:

#0  0x0000000000f4bee1 in htab_hash_string (p=0x0) at /home/jweil/gcc45/trunk/libiberty/hashtab.c:812
#1  0x00000000005ff979 in lookup_filename (file_name=0x0) at /home/jweil/gcc45/trunk/gcc/dwarf2out.c:15710
#2  0x00000000005f3efd in add_src_coords_attributes (die=0x7f26495edcc0, decl=0x7f26496adaa0) at /home/jweil/gcc45/trunk/gcc/dwarf2out.c:12551
#3  0x00000000005f4089 in add_name_and_src_coords_attributes (die=0x7f26495edcc0, decl=0x7f26496adaa0) at /home/jweil/gcc45/trunk/gcc/dwarf2out.c:12568
#4  0x00000000005f9405 in gen_variable_die (decl=0x7f26496adaa0, origin=0x0, context_die=0x7f26495ed3c0) at /home/jweil/gcc45/trunk/gcc/dwarf2out.c:14036
#5  0x00000000005fe0ba in gen_decl_die (decl=0x7f26496adaa0, origin=0x0, context_die=0x7f26495ed3c0) at /home/jweil/gcc45/trunk/gcc/dwarf2out.c:15309
#6  0x00000000005ff2a4 in dwarf2out_decl (decl=0x7f26496adaa0) at /home/jweil/gcc45/trunk/gcc/dwarf2out.c:15615
#7  0x00000000005fe7c6 in dwarf2out_global_decl (decl=0x7f26496adaa0) at /home/jweil/gcc45/trunk/gcc/dwarf2out.c:15355
#8  0x0000000000907815 in emit_debug_global_declarations (vec=0x2f7ba40, len=1342) at /home/jweil/gcc45/trunk/gcc/toplev.c:902
#9  0x00000000007bffec in write_global_declarations () at /home/jweil/gcc45/trunk/gcc/langhooks.c:334
#10 0x0000000000907dbc in compile_file () at /home/jweil/gcc45/trunk/gcc/toplev.c:987
#11 0x0000000000909c7e in do_compile () at /home/jweil/gcc45/trunk/gcc/toplev.c:2241
#12 0x0000000000909ce2 in toplev_main (argc=3, argv=0x7fff5172fb68) at /home/jweil/gcc45/trunk/gcc/toplev.c:2273
#13 0x00000000005137db in main (argc=3, argv=0x7fff5172fb68) at /home/jweil/gcc45/trunk/gcc/main.c:35
Comment 16 janus 2009-04-07 11:43:42 UTC
(In reply to comment #15) 
> proc_decl_13.f90 produces a segfault when compiled with "-g".

Sorry. Of course I'm talking about proc_ptr_13.f90.
Comment 17 Jakub Jelinek 2009-04-07 12:11:53 UTC
All !DECL_IGNORED_P VAR_DECLs should have DECL_SOURCE_LOCATION set to something other than UNKNOWN_LOCATION.  So, either the FE forgot to set it, or it didn't set DECL_IGNORED_P flag when it should.
Comment 18 janus 2009-04-07 12:41:23 UTC
Patch:

Index: gcc/fortran/trans-decl.c
===================================================================
--- gcc/fortran/trans-decl.c	(Revision 145652)
+++ gcc/fortran/trans-decl.c	(Arbeitskopie)
@@ -1015,10 +1015,12 @@ gfc_get_symbol_decl (gfc_symbol * sym)
   if (sym->backend_decl)
     return sym->backend_decl;
 
-  /* Catch function declarations.  Only used for actual parameters.  */
+  /* Catch function declarations.  Only used for actual parameters and
+     procedure pointers.  */
   if (sym->attr.flavor == FL_PROCEDURE)
     {
       decl = gfc_get_extern_function_decl (sym);
+      gfc_set_decl_location (decl, &sym->declared_at);
       return decl;
     }
Comment 19 janus 2009-04-07 16:24:55 UTC
Subject: Bug 38152

Author: janus
Date: Tue Apr  7 16:24:31 2009
New Revision: 145692

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

	PR fortran/38152
	* trans-decl.c (gfc_get_symbol_decl): Correctly set decl location for
	procedure pointer decls.


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

	PR fortran/38152
	* gfortran.dg/proc_ptr_13.f90: Add "-g" option.


Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_13.f90

Comment 20 janus 2009-04-08 14:03:51 UTC
Subject: Bug 38152

Author: janus
Date: Wed Apr  8 14:03:33 2009
New Revision: 145735

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

	PR fortran/38152
	* trans-decl.c (gfc_get_symbol_decl): Correctly set decl location for
	procedure pointer decls.


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

	PR fortran/38152
	* gfortran.dg/proc_ptr_13.f90: Add "-g" option.


Modified:
    branches/gcc-4_4-branch/gcc/fortran/trans-decl.c
    branches/gcc-4_4-branch/gcc/testsuite/gfortran.dg/proc_ptr_13.f90

Comment 21 janus 2009-04-08 14:11:49 UTC
Fixed in trunk and 4.4. Closing.