public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Harald Anlauf <anlauf@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r10-10111] Fortran: Correct missing structure constructor comps. [PR97612].
Date: Fri, 10 Sep 2021 20:49:29 +0000 (GMT)	[thread overview]
Message-ID: <20210910204929.C2C19385842A@sourceware.org> (raw)

https://gcc.gnu.org/g:80b1492b2de153f4850a32cafcd8f4d37c2c84fc

commit r10-10111-g80b1492b2de153f4850a32cafcd8f4d37c2c84fc
Author: Paul Thomas <pault@gcc.gnu.org>
Date:   Tue Dec 29 17:44:48 2020 +0000

    Fortran: Correct missing structure constructor comps. [PR97612].
    
    2020-12-29  Paul Thomas  <pault@gcc.gnu.org>
    
    gcc/fortran
            PR fortran/97612
            * primary.c (build_actual_constructor): Missing allocatable
            components are set unallocated using EXPR_NULL. Then missing
            components are tested for a default initializer.
    
    gcc/testsuite/
            PR fortran/97612
            * gfortran.dg/structure_constructor_17.f90: New test.
    
    (cherry picked from commit eeb145317b42d5203056851435457d9189a7303d)

Diff:
---
 gcc/fortran/primary.c                              | 32 ++++++++++++++--------
 .../gfortran.dg/structure_constructor_17.f90       | 21 ++++++++++++++
 2 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/gcc/fortran/primary.c b/gcc/fortran/primary.c
index 8edc83a42c3..b03961a9981 100644
--- a/gcc/fortran/primary.c
+++ b/gcc/fortran/primary.c
@@ -3062,26 +3062,36 @@ build_actual_constructor (gfc_structure_ctor_component **comp_head,
 	  continue;
 	}
 
-      /* If it was not found, try the default initializer if there's any;
+      /* If it was not found, apply NULL expression to set the component as
+	 unallocated. Then try the default initializer if there's any;
 	 otherwise, it's an error unless this is a deferred parameter.  */
       if (!comp_iter)
 	{
-	  if (comp->initializer)
-	    {
-	      if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
-				   "with missing optional arguments at %C"))
-		return false;
-	      value = gfc_copy_expr (comp->initializer);
-	    }
-	  else if (comp->attr.allocatable
-		   || (comp->ts.type == BT_CLASS
-		       && CLASS_DATA (comp)->attr.allocatable))
+	  /* F2018 7.5.10: If an allocatable component has no corresponding
+	     component-data-source, then that component has an allocation
+	     status of unallocated....  */
+	  if (comp->attr.allocatable
+	      || (comp->ts.type == BT_CLASS
+		  && CLASS_DATA (comp)->attr.allocatable))
 	    {
 	      if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
 				   "allocatable component %qs given in the "
 				   "structure constructor at %C", comp->name))
 		return false;
+	      value = gfc_get_null_expr (&gfc_current_locus);
+	    }
+	  /* ....(Preceeding sentence) If a component with default
+	     initialization has no corresponding component-data-source, then
+	     the default initialization is applied to that component.  */
+	  else if (comp->initializer)
+	    {
+	      if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
+				   "with missing optional arguments at %C"))
+		return false;
+	      value = gfc_copy_expr (comp->initializer);
 	    }
+	  /* Do not trap components such as the string length for deferred
+	     length character components.  */
 	  else if (!comp->attr.artificial)
 	    {
 	      gfc_error ("No initializer for component %qs given in the"
diff --git a/gcc/testsuite/gfortran.dg/structure_constructor_17.f90 b/gcc/testsuite/gfortran.dg/structure_constructor_17.f90
new file mode 100644
index 00000000000..8b8230c6f7e
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/structure_constructor_17.f90
@@ -0,0 +1,21 @@
+! { dg-do compile }
+!
+! Test the fix for PR97612.
+!
+! Contributed by Martin Stein  <mscfd@gmx.net>
+!
+program constructor_allocatable
+  implicit none
+
+  type :: s
+    integer, dimension(:), allocatable :: u
+  end type s
+
+  type :: t
+    type(s), dimension(:), allocatable :: x
+  end type t
+
+  type(t) :: a = t()
+  if (allocated (a%x)) stop 1
+
+end program constructor_allocatable


                 reply	other threads:[~2021-09-10 20:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20210910204929.C2C19385842A@sourceware.org \
    --to=anlauf@gcc.gnu.org \
    --cc=gcc-cvs@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).