public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Steve Kargl <sgk@troutmask.apl.washington.edu>
To: Andre Vehreschild <vehre@gmx.de>
Cc: GCC-Patches-ML <gcc-patches@gcc.gnu.org>,
	       GCC-Fortran-ML <fortran@gcc.gnu.org>
Subject: Re: [RFC, Fortran, (pr66775)] Allocatable function result
Date: Thu, 09 Jul 2015 17:50:00 -0000	[thread overview]
Message-ID: <20150709175047.GA70209@troutmask.apl.washington.edu> (raw)
In-Reply-To: <20150709122518.08388506@vepi2>

On Thu, Jul 09, 2015 at 12:25:18PM +0200, Andre Vehreschild wrote:
> 
> I need your help on how to interpret the standard(s) or how to
> implement handling an allocatable function's result, when that
> result is not allocated by the function.  Imagine the simple
> (albeit artificial) case:
> 
> integer function read_input()
>   read_input = ...
> end function
> 
> integer function getNext()
>   allocatable :: getNext
>   if (more_input_available ()) getNext = read_input()
> end function
> 
> where the function getNext () returns an (automatically)
> allocated result when more_input_available() returns .true..
> Otherwise getNext () returns an unallocated object, i.e.,
> the result's allocation status is .false.. I don't want to
> argue about this design's quality (considering it poor myself). I
> suppose that this code is legal, right?

Code is both valid and invalid.  As you point out, it
depends on the return value of more_input_available().
Also, note, it is always invalid under -std=f95 as it
is using automatic (re-)allocation of the LHS.

> Unfortunately gfortran can not handle it currently.

Whatever gfortran does is "correct", because the code is
invalid in the more_input_available() = .false. case.  It is
the responsible of the programmer to ensure that getNext() has
an allocated and assigned value before it returns.  IHMO,
I think that gfortran should not try to guess what the
programmer might have intended.

Yes, the compiled code may dereference a possibly invalid pointer.
The compiled program should segfault, and the programmer should
fix the Fortran code.

function getNext()
   allocatable :: getNext
   if (more_input_available ())
      getNext = read_input()
   else
      allocate(getNext, source=some_error_code?)
   end if
end function

or

function getNext()
   allocatable :: getNext
   allocate(getNext, source=some_error_code?)
   if (more_input_available ()) getNext = read_input()
end function

-- 
Steve

  reply	other threads:[~2015-07-09 17:50 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-09 10:25 Andre Vehreschild
2015-07-09 17:50 ` Steve Kargl [this message]
2015-07-09 18:59   ` Andre Vehreschild
2015-07-09 19:41     ` Steve Kargl
2015-07-10  9:44       ` Andre Vehreschild
2015-07-10 13:41         ` Steve Kargl
2015-07-10 16:20           ` Mikael Morin
2015-07-10 16:44             ` Andre Vehreschild
2015-07-10 18:57             ` Steve Kargl
2015-07-11 10:37               ` Andre Vehreschild
2015-07-11 11:06                 ` Mikael Morin
2015-07-11 11:58                 ` Dan Nagle
2015-07-13 17:27                   ` Mike Stump
2015-07-11 10:54               ` Mikael Morin
2015-07-11 10:58                 ` Andre Vehreschild
2015-07-11 15:37                 ` Steve Kargl

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=20150709175047.GA70209@troutmask.apl.washington.edu \
    --to=sgk@troutmask.apl.washington.edu \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=vehre@gmx.de \
    /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).