Bug 40450 - [F03] procedure pointer as actual argument
Summary: [F03] procedure pointer as actual argument
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: ---
Assignee: janus
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2009-06-15 18:41 UTC by janus
Modified: 2009-06-19 08:16 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2009-06-18 20:27:30


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description janus 2009-06-15 18:41:39 UTC
The following program gives a segfault at runtime:


MODULE m
 ABSTRACT INTERFACE
 SUBROUTINE sub()
 END SUBROUTINE sub
 END INTERFACE

CONTAINS

 SUBROUTINE passf(f)
   PROCEDURE(sub), POINTER:: f
   CALL callf(f)
 END SUBROUTINE passf

 SUBROUTINE callf(f)
   PROCEDURE(sub), POINTER :: f
   PRINT*, 'calling f'
   CALL f()
 END SUBROUTINE callf
END MODULE m


PROGRAM prog
 USE m
 PROCEDURE(sub), POINTER :: f
 f => s
 CALL passf(f)

CONTAINS

 SUBROUTINE s
   PRINT*, 'sub'
 END SUBROUTINE s
END PROGRAM prog


-fdump-tree-original shows that the problem lies in 'passf':

passf (void (*<T63>) (void) * f)
{
  callf (&f);
}
Comment 1 janus 2009-06-18 20:27:29 UTC
Here is a preliminary patch which fixes the test case:

Index: gcc/fortran/trans-expr.c
===================================================================
--- gcc/fortran/trans-expr.c	(revision 148651)
+++ gcc/fortran/trans-expr.c	(working copy)
@@ -2646,7 +2646,9 @@ gfc_conv_procedure_call (gfc_se * se, gf
 		  if (fsym && e->expr_type != EXPR_NULL
 		      && ((fsym->attr.pointer
 			   && fsym->attr.flavor != FL_PROCEDURE)
-			  || fsym->attr.proc_pointer))
+			  || (fsym->attr.proc_pointer
+			      && !(e->expr_type == EXPR_VARIABLE
+			      && e->symtree->n.sym->attr.dummy))))
 		    {
 		      /* Scalar pointer dummy args require an extra level of
 			 indirection. The null pointer already contains
Comment 2 Tobias Burnus 2009-06-19 07:31:18 UTC
(In reply to comment #1)
> Here is a preliminary patch which fixes the test case:

>                   if (fsym && e->expr_type != EXPR_NULL
>                       && ((fsym->attr.pointer
>                            && fsym->attr.flavor != FL_PROCEDURE)
> -                         || fsym->attr.proc_pointer))
> +                         || (fsym->attr.proc_pointer
> +                             && !(e->expr_type == EXPR_VARIABLE
> +                             && e->symtree->n.sym->attr.dummy))))

The patch looks OK.
Comment 3 janus 2009-06-19 08:11:41 UTC
Subject: Bug 40450

Author: janus
Date: Fri Jun 19 08:11:21 2009
New Revision: 148690

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

	PR fortran/40450
	* trans-expr.c (gfc_conv_procedure_call): Only add an extra addr_expr
	to a procedure pointer actual argument, if it is not itself a
	dummy arg.

2009-06-19  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/40450
	* gfortran.dg/proc_ptr_20.f90: New.


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

Comment 4 janus 2009-06-19 08:16:15 UTC
Fixed with r148690. Closing.