From: robert.corbett@sun.com
Newsgroups: comp.lang.fortran
Subject: another Fortran gotcha! (INTENT(OUT))
Date: 10 Nov 2006 23:37:56 -0800
Message-ID: <1163230676.571268.230130@f16g2000cwb.googlegroups.com>
I saw a new Fortran gotcha! this week. A program (from
an outside source) that had been working for years
started getting segmentation violations at high optimization
levels with Sun's latest compiler. The program contained
a subroutine similar to the following subroutine:
SUBROUTINE COPY_POINTEE(A, B)
TYPE DT
SEQUENCE
REAL, POINTER :: A(:)
END TYPE
TYPE(DT), INTENT(OUT) :: A
TYPE(DT), INTENT(IN) :: B
A%A = B%A
END
This program violates the Fortran standard in two ways.
First, the value of the dummy argument A is undefined
on entry. Therefore, the part ref A%A is undefined.
Second, because A is INTENT(OUT), A must be defined
in the subroutine.
The program used to work because, in Sun f90/f95,
INTENT(OUT) parameters are passed by reference.
It started to fail because the optimizer became smart
enough to understand that the value of the variable
passed as the actual argument associated with the
dummy argument A was dead at the point of the call
and so did not have to be updated in memory.
The subroutine was used in a generic interface block for
the assignment operator. Such use is allowed, but is poor
programming style.
Bob Corbett

From: nospam@see.signature (Richard Maine)
Newsgroups: comp.lang.fortran
Subject: Re: another Fortran gotcha! (INTENT(OUT))
Date: Sat, 11 Nov 2006 01:21:27 -0800
Message-ID: <1hom5vv.1o26pnlxj73qiN%nospam@see.signature>
<robert.corbett@sun.com> wrote:
> Second, because A is INTENT(OUT), A must be defined
> in the subroutine.
I've seen people make that erroneous statement before. You might even
have read it in a book whose authors made such a mistatement. But I bet
you didn't read it in the standard. There is no such requirement
anywhere in the standard.
If I'm wrong and there actually is such a requirement, I'd love to see a
citation. That would explain why so many people get this wrong. But I
think the explanation is just that some people got it wrong and started
spreading the error.
Intent(out) means that the argument becomes undefined on entry (unless
it is of a type with initalization). That's pretty much all. There is no
requirement that it be defined in the subroutine. Now if you want it to
be defined on return from the subroutine, then you need to define it in
the subroutine, but note the "if" in this sentence. If you don't defne
it in the subroutine, it just remains undefined, with all the
consequences of that. Those consequences could include violations of the
standard if something that tries to reference it while it is undefined,
but just being undefined is not a standard violation per se, as long as
it isn't referenced.
Your other point I agree with. In fact, I once got caught by essentially
the same thing myself. I had an intent(out) that was basically a "typo"
(or something - the intent(out) made no sense in context; it clearly
should have been inout). But the code happened to work until a compiler
update. While that was on a Sun machine, it didn't happen to be Sun's
compiler. You apparently aren't the only ones to have added things like
that to the optimizer.
--
Richard Maine | Good judgement comes from experience;
email: last name at domain . net | experience comes from bad judgement.
domain: summertriangle | -- Mark Twain

From: robert.corbett@sun.com
Newsgroups: comp.lang.fortran
Subject: Re: another Fortran gotcha! (INTENT(OUT))
Date: 11 Nov 2006 01:52:25 -0800
Message-ID: <1163238745.316392.263160@i42g2000cwa.googlegroups.com>
Richard Maine wrote:
> <robert.corbett@sun.com> wrote:
>
> > Second, because A is INTENT(OUT), A must be defined
> > in the subroutine.
>
> I've seen people make that erroneous statement before. You might even
> have read it in a book whose authors made such a mistatement. But I bet
> you didn't read it in the standard. There is no such requirement
> anywhere in the standard.
You are correct. I could not find a reference saying it must be defined.
Thank you. Nonetheless, the gotcha! I pointed out is still valid because
of my first point.
Bob Corbett

From: robert.corbett@sun.com
Newsgroups: comp.lang.fortran
Subject: Re: Assumed-size arrays and intent(out)
Date: 16 Nov 2006 21:11:46 -0800
Message-ID: <1163740306.554482.223660@m7g2000cwm.googlegroups.com>
David Rowe wrote:
> Hello
>
> Here's the question. Our coding style standard says that an intent
> attribute must be provided for every dummy argument in new code.
There is a potential problem for your coding style standard.
The absence of an explicit INTENT is not the same as any
of the explicit INTENTs. In particular, it is not equivalent
to INTENT(INOUT).
Bob Corbett

From: robert.corbett@sun.com
Newsgroups: comp.lang.fortran
Subject: Re: another Fortran gotcha! (INTENT(OUT))
Date: 16 Nov 2006 23:13:18 -0800
Message-ID: <1163747598.268500.143190@m73g2000cwd.googlegroups.com>
Richard Maine wrote:
> <robert.corbett@sun.com> wrote:
>
> > Second, because A is INTENT(OUT), A must be defined
> > in the subroutine.
>
> I've seen people make that erroneous statement before. You might even
> have read it in a book whose authors made such a mistatement. But I bet
> you didn't read it in the standard. There is no such requirement
> anywhere in the standard.
>
> If I'm wrong and there actually is such a requirement, I'd love to see a
> citation. That would explain why so manhy people get this wrong. But I
> think the explanation is just that some people got it wrong and started
> spreading the error.
I found what might have been the cause of my misunderstanding
in this regard. Note 5.14 of the Fortran 2003 standard states
INTENT (OUT) means that the value of the argument after
invoking the procedure is entirely the result of executing that
procedure. If there is any possibility that an argument should
retain its current value rather than being redefined,
INTENT (INOUT) should be used rather than INTENT (OUT),
even if there is no explicit reference to the value of the
dummy argument. Because an INTENT (OUT) variable is
considered undefined on entry to the procedure, any default
initialization specified for its type will be applied.
One could easily interpret the first and second sentences taken
together as saying that the entire dummy argument must be
redefined during execution of the procedure.
Bob Corbett

From: robert.corbett@sun.com
Newsgroups: comp.lang.fortran
Subject: Re: Assumed-size arrays and intent(out)
Date: 17 Nov 2006 01:40:13 -0800
Message-ID: <1163756413.289113.162250@f16g2000cwb.googlegroups.com>
Richard Maine wrote:
> <robert.corbett@sun.com> wrote:
>
> > David Rowe wrote:
> >
> > > Here's the question. Our coding style standard says that an intent
> > > attribute must be provided for every dummy argument in new code.
> >
> > There is a potential problem for your coding style standard.
> > The absence of an explicit INTENT is not the same as any
> > of the explicit INTENTs. In particular, it is not equivalent
> > to INTENT(INOUT).
>
> But he said that this was a standard for new code. The fact that no
> other form is exactly equivalent to the absence of an explicit intent
> does mean that you can't necessarily take old code and add an intent to
> every case where intent is allowed (though you can for such a large
> fraction of the cases that one might not ever happen to see one of the
> exceptions, depending on the style of the old code).
>
> But for new code, I personally think it a quite reasonable style
> guideline to not do the kind of things that need an unspecified intent.
> I don't see the disallowance of such things to be a "problem" of a style
> guideline.
Possibly, if one is careful. Note that the O.P. has already
added an INTENT (OUT) in a case that could cause problems
to one of his old routines. Note also that he is unlikely to get
warnings about misuse from most compilers, especially if the
problems are across separate files.
Bob Corbett

From: robert.corbett@sun.com
Newsgroups: comp.lang.fortran
Subject: Re: Assumed-size arrays and intent(out)
Date: 17 Nov 2006 01:55:07 -0800
Message-ID: <1163757307.616196.245990@h48g2000cwc.googlegroups.com>
robert.corb...@sun.com wrote:
> Possibly, if one is careful. Note that the O.P. has already
> added an INTENT (OUT) in a case that could cause problems
> to one of his old routines. Note also that he is unlikely to get
> warnings about misuse from most compilers, especially if the
> problems are across separate files.
One issue users often misunderstand is that attributes
such as the INTENT attributes are not usually checked by
compilers. They are not mechanisms for getting warnings
or errors. Rather, they are promises the programmer makes
to the compiler. If the programmer does not keep his
promises, the compiler has the right to generate code that
does not work as the user expects without giving a hint that
anything is wrong. The optimization for INTENT (OUT) that
recently caused a problem for a user of Sun f95 is one
such example.
Bob Corbett