public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [Patch] Fortran: Avoid var initialization in interfaces [PR54753]
@ 2021-09-29  8:53 Tobias Burnus
  2021-10-02 18:01 ` Sandra Loosemore
  0 siblings, 1 reply; 8+ messages in thread
From: Tobias Burnus @ 2021-09-29  8:53 UTC (permalink / raw)
  To: gcc-patches, fortran

[-- Attachment #1: Type: text/plain, Size: 1854 bytes --]

Found when looking at F2018:C839 / PR54753.

For INTENT(OUT) the dummy variable (might) also be default initialized
or deallocated. However, with assumed rank, that causes issues, which
C839 prevents. In the current GCC implementation, missing C839 constraint
diagnostic, but also rejects-valid/ice-on-valid appears.

There are three issues, this patch solves the first:

* reject-valid issue due to adding the initializer also to a dummy
   argument which is in an INTERFACE block. Having initializers in
   INTERFACE blocks is pointless and causes for the attached testcase
   the bogus error:
   "Assumed-rank variable y at (1) may only be used as actual argument"

(Except for wasting resources and this error, they should be ignored
in trans*.c and usually do not cause any further harm.)


I think Sandra has a nearly ready patch to do the C839 constraint
diagnostic, which needs the attached patch to do the checks.

The third issue is that GCC currently gives either an ICE or the
above error message when declaring a procedure with a valid
assumed-rank intent(out) dummy. This has still to be solved as well.
But first I wanted to unblock Sandra's C839 work with this patch :-)


Regarding the patch, '!= IFSRC_IFBODY' has to be used; "== IFSRC_DECL"
won't work as the the generatedy ENTRY master function has IFSRC_UNKNOWN.

OK for mainline?

Tobias

PS: Some patch reviews are that fast that it is impossible to send the OK;
at least, I did not manage to do for Harald's last two - for the last one
I was at least 4min too late. ;-)

-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955

[-- Attachment #2: intent-out.diff --]
[-- Type: text/x-patch, Size: 3185 bytes --]

Fortran: Avoid var initialization in interfaces [PR54753]

Intent(out) implies deallocation/default initialization; however, it is
pointless to do this for dummy-arguments symbols of procedures which are
inside an INTERFACE block. – This also fixes a bogus error for the attached
included testcase, but fixing the non-interface version still has to be done.

	PR fortran/54753

gcc/fortran/ChangeLog:

	* resolve.c (can_generate_init, resolve_fl_variable_derived,
	resolve_symbol): Only do initialization with intent(out) if not
	inside of an interface block.

gcc/testsuite/ChangeLog:

	* gfortran.dg/assumed_rank_23.f90: New test.

 gcc/fortran/resolve.c                         | 11 ++++++++---
 gcc/testsuite/gfortran.dg/assumed_rank_23.f90 | 16 ++++++++++++++++
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 30b96b2f597..5d2478d9b96 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -12676,7 +12676,8 @@ can_generate_init (gfc_symbol *sym)
     || a->cray_pointer
     || sym->assoc
     || (!a->referenced && !a->result)
-    || (a->dummy && a->intent != INTENT_OUT)
+    || (a->dummy && (a->intent != INTENT_OUT
+		     || sym->ns->proc_name->attr.if_source != IFSRC_DECL))
     || (a->function && sym != sym->result)
   );
 }
@@ -12913,7 +12914,9 @@ resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
 
   /* Assign default initializer.  */
   if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
-      && (!no_init_flag || sym->attr.intent == INTENT_OUT))
+      && (!no_init_flag
+	  || (sym->attr.intent == INTENT_OUT
+	      && sym->ns->proc_name->attr.if_source == IFSRC_DECL)))
     sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
 
   return true;
@@ -16154,7 +16157,8 @@ resolve_symbol (gfc_symbol *sym)
 		    || sym->ts.u.derived->attr.alloc_comp
 		    || sym->ts.u.derived->attr.pointer_comp))
 	   && !(a->function && sym != sym->result))
-	  || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
+	  || (a->dummy && !a->pointer && a->intent == INTENT_OUT
+	      && sym->ns->proc_name->attr.if_source == IFSRC_DECL))
 	apply_default_init (sym);
       else if (a->function && sym->result && a->access != ACCESS_PRIVATE
 	       && (sym->ts.u.derived->attr.alloc_comp
@@ -16166,6 +16170,7 @@ resolve_symbol (gfc_symbol *sym)
 
   if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
       && sym->attr.dummy && sym->attr.intent == INTENT_OUT
+      && sym->ns->proc_name->attr.if_source == IFSRC_DECL
       && !CLASS_DATA (sym)->attr.class_pointer
       && !CLASS_DATA (sym)->attr.allocatable)
     apply_default_init (sym);
diff --git a/gcc/testsuite/gfortran.dg/assumed_rank_23.f90 b/gcc/testsuite/gfortran.dg/assumed_rank_23.f90
new file mode 100644
index 00000000000..c83aa7de1a3
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/assumed_rank_23.f90
@@ -0,0 +1,16 @@
+! { dg-do compile }
+!
+! PR fortran/54753
+! TS29113:C535c
+! F2018:C839
+!
+module m
+
+  interface
+    subroutine s1 (x, y)
+      class(*) :: x(..)
+      class(*), intent (out) :: y(..)
+    end subroutine
+  end interface
+
+end module 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Patch] Fortran: Avoid var initialization in interfaces [PR54753]
  2021-09-29  8:53 [Patch] Fortran: Avoid var initialization in interfaces [PR54753] Tobias Burnus
@ 2021-10-02 18:01 ` Sandra Loosemore
  2021-10-02 18:29   ` Tobias Burnus
  0 siblings, 1 reply; 8+ messages in thread
From: Sandra Loosemore @ 2021-10-02 18:01 UTC (permalink / raw)
  To: Tobias Burnus, gcc-patches, fortran

On 9/29/21 2:53 AM, Tobias Burnus wrote:
> Found when looking at F2018:C839 / PR54753.
> 
> For INTENT(OUT) the dummy variable (might) also be default initialized
> or deallocated. However, with assumed rank, that causes issues, which
> C839 prevents. In the current GCC implementation, missing C839 constraint
> diagnostic, but also rejects-valid/ice-on-valid appears.
> 
> There are three issues, this patch solves the first:
> 
> * reject-valid issue due to adding the initializer also to a dummy
>    argument which is in an INTERFACE block. Having initializers in
>    INTERFACE blocks is pointless and causes for the attached testcase
>    the bogus error:
>    "Assumed-rank variable y at (1) may only be used as actual argument"
> 
> (Except for wasting resources and this error, they should be ignored
> in trans*.c and usually do not cause any further harm.)
> 
> 
> I think Sandra has a nearly ready patch to do the C839 constraint
> diagnostic, which needs the attached patch to do the checks.
> 
> The third issue is that GCC currently gives either an ICE or the
> above error message when declaring a procedure with a valid
> assumed-rank intent(out) dummy. This has still to be solved as well.
> But first I wanted to unblock Sandra's C839 work with this patch :-)

This has indeed allowed me to make progress on adding the diagnostic, 
but I'm seeing some test regressions on x86_64-linux-gnu that are due to 
this patch alone:

FAIL: gfortran.dg/default_initialization_3.f90   -O0  execution test
FAIL: gfortran.dg/default_initialization_3.f90   -O1  execution test
FAIL: gfortran.dg/default_initialization_3.f90   -O2  execution test
FAIL: gfortran.dg/default_initialization_3.f90   -O3 
-fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer 
-finline-functions  execution test
FAIL: gfortran.dg/default_initialization_3.f90   -O3 -g  execution test
FAIL: gfortran.dg/default_initialization_3.f90   -Os  execution test

It is failing with STOP 5, so I guess it is over-eagerly removing 
initialization in something that is not an interface.

BTW, I think it would be better to open a new issue for this and the 
"third issue" you describe for this than to tag them with PR54753, since 
these initialization bugs aren't really related to the missing 
diagnostic in that issue except so far as the bogus error/crashes make 
it impossible to test for the missing diagnostic.  Personally, I find it 
really confusing to track which bugs have really been fixed when 
multiple bugs that need different fixes are lumped together in the same 
issue...  there might be patches committed for such a mashed-up issue, 
but which parts have been fixed and which not, and when is it safe to 
close the issue?  :-S

-Sandra

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Patch] Fortran: Avoid var initialization in interfaces [PR54753]
  2021-10-02 18:01 ` Sandra Loosemore
@ 2021-10-02 18:29   ` Tobias Burnus
  2021-10-02 19:56     ` Tobias Burnus
  0 siblings, 1 reply; 8+ messages in thread
From: Tobias Burnus @ 2021-10-02 18:29 UTC (permalink / raw)
  To: Sandra Loosemore, Tobias Burnus, gcc-patches, fortran

On 02.10.21 20:01, Sandra Loosemore wrote:
> On 9/29/21 2:53 AM, Tobias Burnus wrote:
>> There are three issues, this patch solves the first:
>> * reject-valid issue due to adding the initializer also to a dummy
>>    argument which is in an INTERFACE block. Having initializers in
>>    INTERFACE blocks is pointless and causes for the attached testcase
>>    the bogus error:
>>    "Assumed-rank variable y at (1) may only be used as actual argument"
>> ...
> This has indeed allowed me to make progress on adding the diagnostic, 
> but I'm seeing some test regressions on x86_64-linux-gnu that are due 
> to this patch alone:
>
> FAIL: gfortran.dg/default_initialization_3.f90   -O0  execution test

I do not see this error. Can you double check that you indeed use the 
posted patch:

https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html – 
and nothing else, e.g., an earlier draft?

(I still have to commit it; it was approved by Harald, but he also he 
preferred to have a second view, I decided to wait until tomorrow/Monday 
before committing it.)

Tobias


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Patch] Fortran: Avoid var initialization in interfaces [PR54753]
  2021-10-02 18:29   ` Tobias Burnus
@ 2021-10-02 19:56     ` Tobias Burnus
  2021-10-02 20:16       ` Harald Anlauf
  2021-10-02 20:28       ` Harald Anlauf
  0 siblings, 2 replies; 8+ messages in thread
From: Tobias Burnus @ 2021-10-02 19:56 UTC (permalink / raw)
  To: Sandra Loosemore, Tobias Burnus, gcc-patches, fortran, Harald Anlauf

[-- Attachment #1: Type: text/plain, Size: 1051 bytes --]

Hi Harald,

unfortunately, your email did not arrive at fortran@gcc.gnu.org – nor at my private address.

I copied it from https://gcc.gnu.org/pipermail/gcc-patches/2021-October/580794.html

You wrote:
> >/I do not see this error. Can you double check that you indeed use the />/posted patch: />//>/https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html 
> <https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html> – />/and nothing else, e.g., an earlier draft? /
> Sandra is right.  Actually I do see that regression, too.
> The default initialization is missing in F1, see dump-tree:
Look as if I had attached the first/interim version of the patch –
which lacked what I wrote in the patch submission at:
https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html

Namely, I wrote:
> Regarding the patch, '!= IFSRC_IFBODY' has to be used; "== IFSRC_DECL"
> won't work as the the generatedy ENTRY master function has IFSRC_UNKNOWN.
Thus, no surprise that it passes here – while you see the fail.

Tobias


[-- Attachment #2: intent-out.diff --]
[-- Type: text/x-patch, Size: 3193 bytes --]

Fortran: Avoid var initialization in interfaces [PR54753]

Intent(out) implies deallocation/default initialization; however, it is
pointless to do this for dummy-arguments symbols of procedures which are
inside an INTERFACE block. – This also fixes a bogus error for the attached
included testcase, but fixing the non-interface version still has to be done.

	PR fortran/54753

gcc/fortran/ChangeLog:

	* resolve.c (can_generate_init, resolve_fl_variable_derived,
	resolve_symbol): Only do initialization with intent(out) if not
	inside of an interface block.

gcc/testsuite/ChangeLog:

	* gfortran.dg/assumed_rank_23.f90: New test.

 gcc/fortran/resolve.c                         | 11 ++++++++---
 gcc/testsuite/gfortran.dg/assumed_rank_23.f90 | 16 ++++++++++++++++
 2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index 30b96b2f597..511fe3a5e55 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -12676,7 +12676,8 @@ can_generate_init (gfc_symbol *sym)
     || a->cray_pointer
     || sym->assoc
     || (!a->referenced && !a->result)
-    || (a->dummy && a->intent != INTENT_OUT)
+    || (a->dummy && (a->intent != INTENT_OUT
+		     || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
     || (a->function && sym != sym->result)
   );
 }
@@ -12913,7 +12914,9 @@ resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
 
   /* Assign default initializer.  */
   if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
-      && (!no_init_flag || sym->attr.intent == INTENT_OUT))
+      && (!no_init_flag
+	  || (sym->attr.intent == INTENT_OUT
+	      && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
     sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
 
   return true;
@@ -16154,7 +16157,8 @@ resolve_symbol (gfc_symbol *sym)
 		    || sym->ts.u.derived->attr.alloc_comp
 		    || sym->ts.u.derived->attr.pointer_comp))
 	   && !(a->function && sym != sym->result))
-	  || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
+	  || (a->dummy && !a->pointer && a->intent == INTENT_OUT
+	      && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
 	apply_default_init (sym);
       else if (a->function && sym->result && a->access != ACCESS_PRIVATE
 	       && (sym->ts.u.derived->attr.alloc_comp
@@ -16166,6 +16170,7 @@ resolve_symbol (gfc_symbol *sym)
 
   if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
       && sym->attr.dummy && sym->attr.intent == INTENT_OUT
+      && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
       && !CLASS_DATA (sym)->attr.class_pointer
       && !CLASS_DATA (sym)->attr.allocatable)
     apply_default_init (sym);
diff --git a/gcc/testsuite/gfortran.dg/assumed_rank_23.f90 b/gcc/testsuite/gfortran.dg/assumed_rank_23.f90
new file mode 100644
index 00000000000..c83aa7de1a3
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/assumed_rank_23.f90
@@ -0,0 +1,16 @@
+! { dg-do compile }
+!
+! PR fortran/54753
+! TS29113:C535c
+! F2018:C839
+!
+module m
+
+  interface
+    subroutine s1 (x, y)
+      class(*) :: x(..)
+      class(*), intent (out) :: y(..)
+    end subroutine
+  end interface
+
+end module 

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Patch] Fortran: Avoid var initialization in interfaces [PR54753]
  2021-10-02 19:56     ` Tobias Burnus
@ 2021-10-02 20:16       ` Harald Anlauf
  2021-10-02 20:28       ` Harald Anlauf
  1 sibling, 0 replies; 8+ messages in thread
From: Harald Anlauf @ 2021-10-02 20:16 UTC (permalink / raw)
  To: fortran; +Cc: gcc-patches

Hi Tobias,

the corrected attached patch fixes the regression for testcase
default_initialization_3.f90 for me now, and as a bonus matches
the description.

Am 02.10.21 um 21:56 schrieb Tobias Burnus:
> Hi Harald,
> 
> unfortunately, your email did not arrive at fortran@gcc.gnu.org – nor at 
> my private address.
> 
> I copied it from 
> https://gcc.gnu.org/pipermail/gcc-patches/2021-October/580794.html
> 
> You wrote:
>> >/I do not see this error. Can you double check that you indeed use 
>> the />/posted patch: 
>> />//>/https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html 
>> <https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html> 
>> – />/and nothing else, e.g., an earlier draft? /
>> Sandra is right.  Actually I do see that regression, too.
>> The default initialization is missing in F1, see dump-tree:
> Look as if I had attached the first/interim version of the patch –
> which lacked what I wrote in the patch submission at:
> https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html
> 
> Namely, I wrote:
>> Regarding the patch, '!= IFSRC_IFBODY' has to be used; "== IFSRC_DECL"
>> won't work as the the generatedy ENTRY master function has IFSRC_UNKNOWN.
> Thus, no surprise that it passes here – while you see the fail.
> 
> Tobias
> 

Harald

[I had trouble posting via gmane lately, and I haven't found out
yet what is causing it.]


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Patch] Fortran: Avoid var initialization in interfaces [PR54753]
  2021-10-02 19:56     ` Tobias Burnus
  2021-10-02 20:16       ` Harald Anlauf
@ 2021-10-02 20:28       ` Harald Anlauf
  2021-10-02 20:28         ` Harald Anlauf
  2021-10-02 22:40         ` Sandra Loosemore
  1 sibling, 2 replies; 8+ messages in thread
From: Harald Anlauf @ 2021-10-02 20:28 UTC (permalink / raw)
  To: fortran; +Cc: gcc-patches

Hi Tobias,

the corrected attached patch fixes the regression for testcase
default_initialization_3.f90 for me now, and as a bonus matches
the description.

Am 02.10.21 um 21:56 schrieb Tobias Burnus:
> Hi Harald,
> 
> unfortunately, your email did not arrive at fortran@gcc.gnu.org – nor at 
> my private address.
> 
> I copied it from 
> https://gcc.gnu.org/pipermail/gcc-patches/2021-October/580794.html
> 
> You wrote:
>> >/I do not see this error. Can you double check that you indeed use 
>> the />/posted patch: 
>> />//>/https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html 
>> <https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html> 
>> – />/and nothing else, e.g., an earlier draft? /
>> Sandra is right.  Actually I do see that regression, too.
>> The default initialization is missing in F1, see dump-tree:
> Look as if I had attached the first/interim version of the patch –
> which lacked what I wrote in the patch submission at:
> https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html
> 
> Namely, I wrote:
>> Regarding the patch, '!= IFSRC_IFBODY' has to be used; "== IFSRC_DECL"
>> won't work as the the generatedy ENTRY master function has IFSRC_UNKNOWN.
> Thus, no surprise that it passes here – while you see the fail.
> 
> Tobias
> 

Harald

[I had trouble posting via gmane lately, and I haven't found out
yet what is causing it.]


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Patch] Fortran: Avoid var initialization in interfaces [PR54753]
  2021-10-02 20:28       ` Harald Anlauf
@ 2021-10-02 20:28         ` Harald Anlauf
  2021-10-02 22:40         ` Sandra Loosemore
  1 sibling, 0 replies; 8+ messages in thread
From: Harald Anlauf @ 2021-10-02 20:28 UTC (permalink / raw)
  To: Tobias Burnus, Sandra Loosemore, Tobias Burnus, gcc-patches, fortran

Hi Tobias,

the corrected attached patch fixes the regression for testcase
default_initialization_3.f90 for me now, and as a bonus matches
the description.

Am 02.10.21 um 21:56 schrieb Tobias Burnus:
> Hi Harald,
>
> unfortunately, your email did not arrive at fortran@gcc.gnu.org – nor at
> my private address.
>
> I copied it from
> https://gcc.gnu.org/pipermail/gcc-patches/2021-October/580794.html
>
> You wrote:
>> >/I do not see this error. Can you double check that you indeed use
>> the />/posted patch:
>> />//>/https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html
>> <https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html>
>> – />/and nothing else, e.g., an earlier draft? /
>> Sandra is right.  Actually I do see that regression, too.
>> The default initialization is missing in F1, see dump-tree:
> Look as if I had attached the first/interim version of the patch –
> which lacked what I wrote in the patch submission at:
> https://gcc.gnu.org/pipermail/gcc-patches/2021-September/580483.html
>
> Namely, I wrote:
>> Regarding the patch, '!= IFSRC_IFBODY' has to be used; "== IFSRC_DECL"
>> won't work as the the generatedy ENTRY master function has IFSRC_UNKNOWN.
> Thus, no surprise that it passes here – while you see the fail.
>
> Tobias
>

Harald

[I had trouble posting via gmane lately, and I haven't found out
yet what is causing it.]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [Patch] Fortran: Avoid var initialization in interfaces [PR54753]
  2021-10-02 20:28       ` Harald Anlauf
  2021-10-02 20:28         ` Harald Anlauf
@ 2021-10-02 22:40         ` Sandra Loosemore
  1 sibling, 0 replies; 8+ messages in thread
From: Sandra Loosemore @ 2021-10-02 22:40 UTC (permalink / raw)
  To: Harald Anlauf, Tobias Burnus, Tobias Burnus, gcc-patches, fortran

On 10/2/21 2:28 PM, Harald Anlauf wrote:
> Hi Tobias,
> 
> the corrected attached patch fixes the regression for testcase
> default_initialization_3.f90 for me now, and as a bonus matches
> the description.

Me too!  I'm also seeing clean test results now.

-Sandra

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-10-02 22:40 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-29  8:53 [Patch] Fortran: Avoid var initialization in interfaces [PR54753] Tobias Burnus
2021-10-02 18:01 ` Sandra Loosemore
2021-10-02 18:29   ` Tobias Burnus
2021-10-02 19:56     ` Tobias Burnus
2021-10-02 20:16       ` Harald Anlauf
2021-10-02 20:28       ` Harald Anlauf
2021-10-02 20:28         ` Harald Anlauf
2021-10-02 22:40         ` Sandra Loosemore

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).