diff mbox series

Fortran: reject array constructor value of abstract type [PR114308]

Message ID trinity-4e1792a3-d2c6-4732-a157-c912f1ecbee2-1723569904318@3c-app-gmx-bs39
State New
Headers show
Series Fortran: reject array constructor value of abstract type [PR114308] | expand

Commit Message

Harald Anlauf Aug. 13, 2024, 5:25 p.m. UTC
Dear all,

the attached patch checks whether the declared type of an array constructor
value is abstract, which is forbidden by the standard.
Steve found the relevant constraint in F2023, but it exists already in F2018.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald

Comments

Harald Anlauf Aug. 13, 2024, 7:16 p.m. UTC | #1
Pushed after an OK by Steve in the PR as

r15-2902-g9988d7e004796ab531df7bcda45788a7aa9276d7

Am 13.08.24 um 19:25 schrieb Harald Anlauf:
> Dear all,
> 
> the attached patch checks whether the declared type of an array constructor
> value is abstract, which is forbidden by the standard.
> Steve found the relevant constraint in F2023, but it exists already in F2018.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 
> Thanks,
> Harald
> 

Cheers,
Harald
diff mbox series

Patch

From 9988d7e004796ab531df7bcda45788a7aa9276d7 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Tue, 13 Aug 2024 19:17:36 +0200
Subject: [PATCH] Fortran: reject array constructor value of abstract type
 [PR114308]

gcc/fortran/ChangeLog:

	PR fortran/114308
	* array.cc (resolve_array_list): Reject array constructor value if
	its declared type is abstract (F2018:C7114).

gcc/testsuite/ChangeLog:

	PR fortran/114308
	* gfortran.dg/abstract_type_10.f90: New test.

Co-Authored-By: Steven G. Kargl <kargl@gcc.gnu.org>
---
 gcc/fortran/array.cc                          | 13 ++++++++
 .../gfortran.dg/abstract_type_10.f90          | 30 +++++++++++++++++++
 2 files changed, 43 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/abstract_type_10.f90

diff --git a/gcc/fortran/array.cc b/gcc/fortran/array.cc
index 79c774d59a0..a5e94f1fa77 100644
--- a/gcc/fortran/array.cc
+++ b/gcc/fortran/array.cc
@@ -2127,6 +2127,19 @@  resolve_array_list (gfc_constructor_base base)
 		     "polymorphic [F2008: C4106]", &c->expr->where);
 	  t = false;
 	}
+
+      /* F2018:C7114 The declared type of an ac-value shall not be abstract.  */
+      if (c->expr->ts.type == BT_CLASS
+	  && c->expr->ts.u.derived
+	  && c->expr->ts.u.derived->attr.abstract
+	  && CLASS_DATA (c->expr))
+	{
+	  gfc_error ("Array constructor value %qs at %L is of the ABSTRACT "
+		     "type %qs", c->expr->symtree->name, &c->expr->where,
+		     CLASS_DATA (c->expr)->ts.u.derived->name);
+	  t = false;
+	}
+
     }

   return t;
diff --git a/gcc/testsuite/gfortran.dg/abstract_type_10.f90 b/gcc/testsuite/gfortran.dg/abstract_type_10.f90
new file mode 100644
index 00000000000..a4bf65d4e12
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/abstract_type_10.f90
@@ -0,0 +1,30 @@ 
+! { dg-do compile }
+!
+! PR fortran/114308 - reject array constructor value of abstract type
+
+module my_module
+  implicit none
+  private
+
+  type, abstract, public :: a
+  end type
+
+  type, extends(a), public :: b
+  end type
+end
+
+program main
+  use my_module
+  implicit none
+  type(b)               :: b_instance
+  class(a), allocatable :: a_array(:)
+  class(b), allocatable :: b_array(:)
+
+  a_array = [b_instance]
+  b_array = [b_instance]
+  a_array = [a_array]             ! { dg-error "is of the ABSTRACT type" }
+  a_array = [a_array(1)]          ! { dg-error "is of the ABSTRACT type" }
+  a_array = [a_array, b_instance] ! { dg-error "is of the ABSTRACT type" }
+  a_array = [b_instance, a_array] ! { dg-error "is of the ABSTRACT type" }
+  b_array = [b_array, a_array]    ! { dg-error "is of the ABSTRACT type" }
+end program
--
2.35.3