public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "sgk at troutmask dot apl dot washington dot edu" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/42112] overloaded function with allocatable result problem
Date: Fri, 20 Nov 2009 00:20:00 -0000	[thread overview]
Message-ID: <20091120002023.31213.qmail@sourceware.org> (raw)
In-Reply-To: <bug-42112-17129@http.gcc.gnu.org/bugzilla/>



------- Comment #2 from sgk at troutmask dot apl dot washington dot edu  2009-11-20 00:20 -------
Subject: Re:  overloaded function with allocatable result problem

If the code is compiled with -fdump-tree-original one
immediately see the cause of the runtime error.  Eliminating
the common code in the dumps, I find this code

  pure function f() result(i)
    integer :: i
    integer, allocatable :: loc_ar(:)
    allocate(loc_ar(1))
    loc_ar = gen_g() ! does not work
    deallocate(loc_ar)
    i = 1
  end function f

generates

  f ()
  {
    g (&loc_ar);
  }

loc_ar has been allocated and the pointer to it is passed
to g() where is becomes associated with the result variable j.
You then try to allocate j, which is already allocated.


When you call g() directly in 

   pure function f() result(i)
    integer :: i
    integer, allocatable :: loc_ar(:)
    allocate(loc_ar(1))
    loc_ar = g() ! no problem here
    deallocate(loc_ar)
    i = 1
   end function f

the dump shows

  f ()
  {
    {
      integer(kind=8) D.1398;
      struct array1_integer(kind=4) atmp.1;
      integer(kind=8) D.1393;
      integer(kind=8) D.1392;
      integer(kind=8) D.1391;
      integer(kind=4)[0:] * restrict D.1390;

      D.1390 = (integer(kind=4)[0:] * restrict) loc_ar.data;
      D.1391 = loc_ar.offset;
      D.1392 = loc_ar.dim[0].lbound;
      D.1393 = loc_ar.dim[0].ubound;
      atmp.1.dtype = 265;
      atmp.1.data = 0B;
      atmp.1.offset = 0;
      g (&atmp.1);

g() is called with a unallocated temporary variable, so
your allocation in g() works.  The next several lines
copy atmp into loc_ar.

      D.1398 = NON_LVALUE_EXPR <D.1392>;
      {
      integer(kind=8) S.2;

      S.2 = 0;
      while (1)
        {
          if (atmp.1.dim[0].ubound - atmp.1.dim[0].lbound < S.2) goto L.2;
          (*D.1390)[(S.2 + D.1398) + D.1391]
                   = (*(integer(kind=4)[0:] * restrict) atmp.1.data)[S.2];
          S.2 = S.2 + 1;
        }
      L.2:;
    }

Here, the temporary variable is destroyed.

    {
      void * D.1397;

      D.1397 = (void *) atmp.1.data;
      if (D.1397 != 0B)
        {
          __builtin_free (D.1397);
        }
    }
  }
}

I need to go read the Fortran standard to determine the
semantics of a function returning an allocatable entity.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42112


  parent reply	other threads:[~2009-11-20  0:20 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-19 22:20 [Bug fortran/42112] New: " mrestelli at gmail dot com
2009-11-19 23:56 ` [Bug fortran/42112] " kargl at gcc dot gnu dot org
2009-11-20  0:20 ` sgk at troutmask dot apl dot washington dot edu [this message]
2009-11-20 14:23 ` burnus at gcc dot gnu dot org
     [not found] <bug-42112-4@http.gcc.gnu.org/bugzilla/>
2010-11-21 12:19 ` pault at gcc dot gnu.org
2010-11-25  8:02 ` pault at gcc dot gnu.org
2010-11-25  8:54 ` burnus at gcc dot gnu.org
2010-11-25  8:59 ` burnus at gcc dot gnu.org
2010-11-28 12:46 ` pault at gcc dot gnu.org
2015-04-17 17:49 ` dominiq at lps dot ens.fr
2015-08-07 20:46 ` mikael at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20091120002023.31213.qmail@sourceware.org \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).