aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPaul Thomas <pault@gcc.gnu.org>2021-01-21 10:00:00 +0000
committerPaul Thomas <pault@gcc.gnu.org>2021-01-21 10:00:49 +0000
commiteaf883710c0039eca5caea5115e848adb4ab67bd (patch)
treefc7baa465ca84ce19178b9c6657ed0932ea636fb /gcc
parentf46a40112caa7e039d949beda94386ff4e436a35 (diff)
downloadgcc-eaf883710c0039eca5caea5115e848adb4ab67bd.zip
gcc-eaf883710c0039eca5caea5115e848adb4ab67bd.tar.gz
gcc-eaf883710c0039eca5caea5115e848adb4ab67bd.tar.bz2
Fortran: This patch fixes comments 23 and 24 of PR96320.
2021-01-21 Paul Thomas <pault@gcc.gnu.org> gcc/fortran PR fortran/96320 * decl.c (gfc_match_modproc): It is not an error to find a module procedure declaration within a contains block. * expr.c (gfc_check_vardef_context): Pure procedure result is assignable. Change 'own_scope' accordingly. * resolve.c (resolve_typebound_procedure): A procedure that has the module procedure attribute is almost certainly a module procedure, whatever its interface. gcc/testsuite/ PR fortran/96320 * gfortran.dg/module_procedure_5.f90 : New test. * gfortran.dg/module_procedure_6.f90 : New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/fortran/decl.c3
-rw-r--r--gcc/fortran/expr.c3
-rw-r--r--gcc/fortran/resolve.c3
-rw-r--r--gcc/testsuite/gfortran.dg/module_procedure_5.f9031
-rw-r--r--gcc/testsuite/gfortran.dg/module_procedure_6.f9051
5 files changed, 89 insertions, 2 deletions
diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c
index 4771b59..7239158 100644
--- a/gcc/fortran/decl.c
+++ b/gcc/fortran/decl.c
@@ -9856,7 +9856,8 @@ gfc_match_modproc (void)
gfc_namespace *module_ns;
gfc_interface *old_interface_head, *interface;
- if (gfc_state_stack->state != COMP_INTERFACE
+ if ((gfc_state_stack->state != COMP_INTERFACE
+ && gfc_state_stack->state != COMP_CONTAINS)
|| gfc_state_stack->previous == NULL
|| current_interface.type == INTERFACE_NAMELESS
|| current_interface.type == INTERFACE_ABSTRACT)
diff --git a/gcc/fortran/expr.c b/gcc/fortran/expr.c
index 188e796..4f456fc 100644
--- a/gcc/fortran/expr.c
+++ b/gcc/fortran/expr.c
@@ -6243,6 +6243,9 @@ gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj,
/* Variable not assignable from a PURE procedure but appears in
variable definition context. */
+ own_scope = own_scope
+ || (sym->attr.result && sym->ns->proc_name
+ && sym == sym->ns->proc_name->result);
if (!pointer && !own_scope && gfc_pure (NULL) && gfc_impure_variable (sym))
{
if (context)
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index bb069ef..c075d0f 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -14025,7 +14025,8 @@ resolve_typebound_procedure (gfc_symtree* stree)
/* Check for F08:C465. */
if ((!proc->attr.subroutine && !proc->attr.function)
|| (proc->attr.proc != PROC_MODULE
- && proc->attr.if_source != IFSRC_IFBODY)
+ && proc->attr.if_source != IFSRC_IFBODY
+ && !proc->attr.module_procedure)
|| proc->attr.abstract)
{
gfc_error ("%qs must be a module procedure or an external "
diff --git a/gcc/testsuite/gfortran.dg/module_procedure_5.f90 b/gcc/testsuite/gfortran.dg/module_procedure_5.f90
new file mode 100644
index 0000000..3dafa06
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/module_procedure_5.f90
@@ -0,0 +1,31 @@
+! { dg-do compile }
+!
+! Test the fix for the testcase in comment 23 of PR96320, which used to
+! fail with the message: Variable ‘new_foo’ cannot appear in a variable
+! definition context.
+!
+! Contributed by Damian Rouson <damian@sourceryinstitute.org>
+!
+module foobar
+ implicit none
+
+ type foo
+ integer bar
+ end type
+
+ interface
+ pure module function create() result(new_foo)
+ implicit none
+ type(foo) new_foo
+ end function
+ end interface
+
+contains
+ module procedure create
+ new_foo%bar = 1 ! Error here
+ end procedure
+end module
+
+ use foobar
+ print *, create ()
+end
diff --git a/gcc/testsuite/gfortran.dg/module_procedure_6.f90 b/gcc/testsuite/gfortran.dg/module_procedure_6.f90
new file mode 100644
index 0000000..e642d52
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/module_procedure_6.f90
@@ -0,0 +1,51 @@
+! { dg-do run }
+!
+! Test the fix for the testcase in comment 24 of PR96320, which used to
+! fail with the message: ‘set_user_defined’ must be a module procedure or
+! an external procedure with an explicit interface at (1)
+!
+! Contributed by Damian Rouson <damian@sourceryinstitute.org>
+!
+module hole_interface
+ type hole_t
+ integer :: user_defined
+ real :: hole_diameter
+ contains
+ procedure set_user_defined
+ procedure set_diameter
+ end type
+
+ interface
+ module subroutine set_diameter (this, diameter)
+ class(hole_t) :: this
+ real :: diameter
+ end subroutine
+
+ module subroutine set_user_defined(this, user_defined)
+ class(hole_t) :: this
+ integer :: user_defined
+ end subroutine
+ end interface
+
+contains
+ module procedure set_user_defined
+ this%user_defined = user_defined
+ end procedure
+
+ module procedure set_diameter
+ this%hole_diameter = diameter
+ if (this%user_defined .lt. 0) then
+ call this%set_user_defined (0)
+ end if
+ end procedure
+end module
+
+ use hole_interface ! Error was here
+ type (hole_t) :: ht = hole_t (-1, 0.0)
+ call ht%set_diameter(1.0)
+ if ((ht%user_defined .ne. 0) .and. (ht%hole_diameter .ne. 1.0)) stop 1
+ call ht%set_user_defined (5)
+ if ((ht%user_defined .ne. 5) .and. (ht%hole_diameter .ne. 1.0)) stop 2
+ call ht%set_diameter(2.0)
+ if ((ht%user_defined .ne. 5) .and. (ht%hole_diameter .ne. 2.0)) stop 3
+end