Bug 51286 - function with print statement hangs when called as argument of a print statement
Summary: function with print statement hangs when called as argument of a print statement
Status: RESOLVED DUPLICATE of bug 30617
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.6.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-23 17:49 UTC by Stefano Borini
Modified: 2013-02-27 18:18 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stefano Borini 2011-11-23 17:49:45 UTC
This code hangs due to the "whatever" printing statement inside the function, when called as an argument to print.

Wild guess is that it tries to get some lock during the Squared print, which is already taken by the caller, but it's pure speculation. 

Tested on ifort, works as expected (no hang, two prints).

module Moo
contains
   subroutine main()
      
      print *, Squared(5)

   end subroutine

   function Squared(v)
      integer, intent(in) :: v
      integer :: Squared
      Squared = v*v
      print *, "whatever"
   end function
end module
program foo
   use Moo
   call main()
end program
Comment 1 kargls 2011-11-23 18:06:09 UTC
Your code is invalid, because it is doing trying to do 
recursive IO.  From the Fortran 2003 standard:


An input/output statement that is executed while another
input/output statement is being executed is called a
recursive input/output statement.

A recursive input/output statement shall not identify an
external unit except that a child data transfer statement
may identify its parent data transfer statement external unit.

A data transfer statement that includes a derived-type
list item and that causes a user-defined derived-type
input/output procedure to be invoked is called a parent
data transfer statement.  A data transfer statement that
is executed while a parent data transfer statement is being
processed and that specifies the unit passed into a
user-defined derived-type input/output procedure is called
a child data transfer statement.

You are invoking undefined behavior, and are clearly 
getting an acceptable result.
Comment 2 Dominique d'Humieres 2011-11-23 18:26:41 UTC
Aren't you using some Mac OSX? If so, it is a duplicate of pr30617.
Comment 3 Dominique d'Humieres 2013-02-27 18:18:37 UTC
> Aren't you using some Mac OSX? If so, it is a duplicate of pr30617.

No answer, so closing as duplicate.

*** This bug has been marked as a duplicate of bug 30617 ***